?
Solved

DOM object from StringBuffer

Posted on 2008-10-20
3
Medium Priority
?
548 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 400 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 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 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