Solved

DOM object from StringBuffer

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 81
MySQL  on Tomcat 8 45
Eclipse with gitlab 1 41
String array comparison 4 32
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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:

776 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