Solved

DOM object from StringBuffer

Posted on 2008-10-20
3
540 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

705 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