de.enough.polish.xml
Class XmlDomParser

java.lang.Object
  extended by de.enough.polish.xml.XmlDomParser

public class XmlDomParser
extends Object

Parses XML documents in a single step.

This is more comfortable than using the XmlPullParser but uses somewhat more memory since the complete document is held memory.

Following example code demonstrates the usage:

        String document = "<rootelement><childnode x=\"10\">textvalue10</childnode><childnode x=\"20\">textvalue20</childnode></rootelement>";
        XmlDomNode root = XmlDomParser.parseTree(document);
        System.out.println("root " + root.getName() + ", has " + root.getChildCount() + " children");
        // indirect access:
        for (int i=0; i < root.getChildCount(); i++ ) {
                XmlDomNode child = root.getChild(i);
                System.out.println("child: " + child.getName() );
                System.out.println("child: " + child.getName() + ", x=" + child.getAttribute("x") + ", text=" + child.getText() );
        }
        // direct access:
        XmlDomNode child = root.getChild("childnode");                
        System.out.println("first child: " + child.getName() + ", x=" + child.getAttribute("x") + ", text=" + child.getText() );

 

Copyright Enough Software 2006 - 2009

 history
        Apr 8, 2006 - mkoch creation
 

Author:
Michael Koch, michael.koch@enough.de

Constructor Summary
XmlDomParser()
           
 
Method Summary
static XmlDomNode parseTree(InputStream in)
          Parses an XML document and returns it's root element as an XmlDomNode.
static XmlDomNode parseTree(InputStream in, String encoding)
          Parses an XML document and returns it's root element as an XmlDomNode.
static XmlDomNode parseTree(String document)
          Parses an XML document and returns it's root element as an XmlDomNode.
static XmlDomNode parseTree(String document, String encoding)
          Parses an XML document and returns it's root element as an XmlDomNode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlDomParser

public XmlDomParser()
Method Detail

parseTree

public static XmlDomNode parseTree(String document)
Parses an XML document and returns it's root element as an XmlDomNode. When the XML document has no root element, it will return an artificial root element that contains all root elements of the document.

Parameters:
document - the document.
Returns:
the root element of the document as XmlDomNode
Throws:
RuntimeException - when the parsing fails

parseTree

public static XmlDomNode parseTree(String document,
                                   String encoding)
                            throws UnsupportedEncodingException
Parses an XML document and returns it's root element as an XmlDomNode. When the XML document has no root element, it will return an artificial root element that contains all root elements of the document.

Parameters:
document - the document.
encoding - the encoding, e.g. "UTF-8"
Returns:
the root element of the document as XmlDomNode
Throws:
UnsupportedEncodingException
RuntimeException - when the parsing fails

parseTree

public static XmlDomNode parseTree(InputStream in)
Parses an XML document and returns it's root element as an XmlDomNode. When the XML document has no root element, it will return an artificial root element that contains all root elements of the document.

Parameters:
in - input stream of the document.
Returns:
the root element of the document as XmlDomNode
Throws:
RuntimeException - when the parsing fails

parseTree

public static XmlDomNode parseTree(InputStream in,
                                   String encoding)
                            throws UnsupportedEncodingException
Parses an XML document and returns it's root element as an XmlDomNode. When the XML document has no root element, it will return an artificial root element that contains all root elements of the document.

Parameters:
in - input stream of the document.
encoding - the encoding, e.g. "UTF-8"
Returns:
the root element of the document as XmlDomNode
Throws:
UnsupportedEncodingException
RuntimeException - when the parsing fails