Results 1 to 6 of 6

Thread: Xml Dom Document to Formatted String

  1. #1

    Thread Starter
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Resolved 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.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3

    Thread Starter
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Xml Dom Document to Formatted String

    Not what I wanted to hear, but thanks all the same ComputerJy.

  4. #4

    Thread Starter
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    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;
        }

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  6. #6

    Thread Starter
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    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
  •  



Click Here to Expand Forum to Full Width