Solved

DOM object from StringBuffer

Posted on 2008-10-20
3
500 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
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
manuel_m earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
In my code, you will need to replace "xml" with sb.toString().
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stringclean challenge 26 53
topping3 challenge 14 48
Java Message handling in Service Layer 3 37
maven example error 3 34
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now