|
-
Dec 9th, 2005, 11:08 AM
#1
Thread Starter
Frenzied Member
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.
Last edited by DeadEyes; Dec 12th, 2005 at 09:21 AM.
-
Dec 9th, 2005, 12:43 PM
#2
Re: Xml Dom Document to Formatted String
You have to read all tags and values and print them out
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Dec 12th, 2005, 04:07 AM
#3
Thread Starter
Frenzied Member
Re: Xml Dom Document to Formatted String
Not what I wanted to hear, but thanks all the same ComputerJy.
-
Dec 12th, 2005, 09:20 AM
#4
Thread Starter
Frenzied Member
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;
}
-
Dec 13th, 2005, 05:05 AM
#5
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 13th, 2005, 07:15 AM
#6
Thread Starter
Frenzied Member
Re: Xml Dom Document to Formatted String
Thanks for the tip CornedBee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|