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
Solved

DOM object from StringBuffer

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Custom Java Application connects to database: Which one of the 20? 4 35
ejb on wildfly 5 30
ejb entity bean example 2 7
Selenium docs api java index 3 48
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

789 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