Solved

DOM object from StringBuffer

Posted on 2008-10-20
3
530 Views
Last Modified: 2013-11-23
I have an XML in StringBuffer, say

StringBuffer sb = new StringBuffer()
sb.append("<tag1><tag2>33</tag2><tag2>33</tag2></tag1>");

Is it possible to create a DOM object from this StringBuffer so that I can iterate over the XML using the DOM object.
0
Comment
Question by:ryna_s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
manuel_m earned 100 total points
ID: 22765145
You can create an org.w3c.dom.Document object with the following code.
try {
  // create a new DocumentBuilder object
  javax.xml.parsers.DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
  org.w3c.dom.Document document = builder.parse(new org.xml.sax.InputSource(new StringReader(sb.toString())));
} catch (org.xml.sax.SAXException e) {
  e.printStackTrace(); // or throw a Exception
} catch (java.io.IOException e) {
  e.printStackTrace(); // or throw a Exception
} catch (javax.xml.parsers.ParserConfigurationException e) {
            e.printStackTrace(); // or throw a Exception
        }

Open in new window

0
 
LVL 5

Assisted Solution

by:muktajindal
muktajindal earned 25 total points
ID: 22765905
Similarly, you may also use the attached snippet where you can specify encoding also, if required.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
Document dom = builder.parse(is);

Open in new window

0
 
LVL 5

Expert Comment

by:muktajindal
ID: 22765921
In my code, you will need to replace "xml" with sb.toString().
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 49
servlet example 17 59
Select statement in @Query (JPA Repository) - SpringMVC 2 37
jmeter usage 4 35
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question