Xml Dom Document to Formatted String
This is probably really easy but I can't see how to do it. I have a org.w3c.dom.Document and I want to output it's contents to a JTextArea,
for example:
<employee id="111">
<name>bob</name>
<age>34</age>
</employee>
Iterating through all the nodes and attributes would appear to be more work than I thought necessary.
Can any one point me in the right direction.
Re: Xml Dom Document to Formatted String
You have to read all tags and values and print them out
Re: Xml Dom Document to Formatted String
Not what I wanted to hear, but thanks all the same ComputerJy.
Re: Xml Dom Document to Formatted String
I came acroos this it does the trick.
Code:
public String toString() {
String output = "";
Transformer transformer;
try {
transformer = TransformerFactory.newInstance().newTransformer();
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
if (transformer != null) {
transformer.transform(new DOMSource(doc.getDocumentElement()),result);
output = sw.toString();
System.err.println("output: " + output);
}else{
System.err.println("No Transformer");
}
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
return output;
}
Re: Xml Dom Document to Formatted String
Also, in Java5 you should look into org.w3c.dom.ls, that's where DOM Level 3 Load & Save resides.
Re: Xml Dom Document to Formatted String
Thanks for the tip CornedBee