TUM CCSM Commons

edu.tum.cs.commons.xml
Class XMLUtils

java.lang.Object
  extended by edu.tum.cs.commons.xml.XMLUtils

public class XMLUtils
extends java.lang.Object

Collection of utility methods for XML.

Version:
$Rev: 26369 $
Author:
Florian Deissenboeck, $Author: hummelb $
Rating:
GREEN Hash: 3E4CDEC5A0EAAC290A6EDF7279A9C811

Constructor Summary
XMLUtils()
           
 
Method Summary
static org.w3c.dom.Document createEmptyDocument()
          Creates an empty XML document.
static java.util.List<org.w3c.dom.Element> elementNodes(org.w3c.dom.NodeList nodeList)
          Extracts all ElementNodes from a NodeList and returns the result as a list.
static int getElementPosition(org.w3c.dom.Element element)
          Determines the index (starting at 0) of the given element relative to other element nodes for the same parent.
static org.w3c.dom.Element getNamedChild(org.w3c.dom.Element element, java.lang.String name)
          Returns the first child of the given element which is an element named as specified.
static java.lang.String getNamedChildContent(org.w3c.dom.Element parent, java.lang.String name)
          Get the text content of the given element's first child that is an element named as specified.
static java.util.List<org.w3c.dom.Element> getNamedChildren(org.w3c.dom.Element element, java.lang.String elementNames)
          Returns all children of the given element which are element named as specified.
static java.util.List<org.w3c.dom.Element> leafElementNodes(org.w3c.dom.Element root)
          Get all leaf elements of an XML tree rooted at an element
static org.w3c.dom.Document parse(java.io.File file)
          Parse a file without validation.
static org.w3c.dom.Document parse(java.io.File file, java.io.File schema)
          Same as parse(File, URL) but with schema file.
static org.w3c.dom.Document parse(java.io.File file, java.net.URL schemaURL)
          Parse and validate file using schema.
static org.w3c.dom.Document parse(org.xml.sax.InputSource input)
          Parse an input source without validation.
static org.w3c.dom.Document parse(org.xml.sax.InputSource input, java.net.URL schemaURL)
          Parse and validate file using schema.
static void parseSAX(java.io.File file, org.xml.sax.helpers.DefaultHandler handler)
          Parse an input source using SAX without validation.
static void parseSAX(java.io.File file, java.net.URL schemaURL, org.xml.sax.helpers.DefaultHandler handler)
          Parse and validate file using SAX and schema.
static void parseSAX(org.xml.sax.InputSource input, org.xml.sax.helpers.DefaultHandler handler)
          Parse an input source using SAX without validation.
static void parseSAX(org.xml.sax.InputSource input, java.net.URL schemaURL, org.xml.sax.helpers.DefaultHandler handler)
          Parse and validate file using SAX and schema.
static java.lang.String prettyPrint(org.w3c.dom.Document doc)
          Returns a string representation of the given XML document, which is "pretty printed", i.e. the tags are indented.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtils

public XMLUtils()
Method Detail

parse

public static org.w3c.dom.Document parse(java.io.File file)
                                  throws org.xml.sax.SAXException,
                                         java.io.IOException
Parse a file without validation.

Parameters:
file - the file to parse.
Returns:
the DOM document.
Throws:
org.xml.sax.SAXException - if a parsing exception occurs, i.e. if the file is not well-formed.
java.io.IOException - if an IO exception occurs.

parse

public static org.w3c.dom.Document parse(org.xml.sax.InputSource input)
                                  throws org.xml.sax.SAXException,
                                         java.io.IOException
Parse an input source without validation.

Parameters:
input - the input source to parse
Returns:
the DOM document.
Throws:
org.xml.sax.SAXException - if a parsing exception occurs, i.e. if the file is not well-formed.
java.io.IOException - if an IO exception occurs.

parseSAX

public static void parseSAX(java.io.File file,
                            org.xml.sax.helpers.DefaultHandler handler)
                     throws org.xml.sax.SAXException,
                            java.io.IOException
Parse an input source using SAX without validation.

Throws:
org.xml.sax.SAXException - if a parsing exception occurs, i.e. if the file is not well-formed.
java.io.IOException - if an IO exception occurs.

parseSAX

public static void parseSAX(org.xml.sax.InputSource input,
                            org.xml.sax.helpers.DefaultHandler handler)
                     throws org.xml.sax.SAXException,
                            java.io.IOException
Parse an input source using SAX without validation.

Throws:
org.xml.sax.SAXException - if a parsing exception occurs, i.e. if the file is not well-formed.
java.io.IOException - if an IO exception occurs.

parse

public static org.w3c.dom.Document parse(java.io.File file,
                                         java.net.URL schemaURL)
                                  throws org.xml.sax.SAXException,
                                         java.io.IOException
Parse and validate file using schema. This implements a custom error handler to avoid different behaviour between the JAXP implementations shipping with Java 1.5 and Java 1.6.

Parameters:
file - the file to parse.
schemaURL - URL point to schema, may not be null
Returns:
the DOM document.
Throws:
org.xml.sax.SAXException - if a parsing exception occurs, i.e. if the file is not well-formed or not valid
java.io.IOException - if an IO exception occurs.

parse

public static org.w3c.dom.Document parse(org.xml.sax.InputSource input,
                                         java.net.URL schemaURL)
                                  throws org.xml.sax.SAXException,
                                         java.io.IOException
Parse and validate file using schema. This implements a custom error handler to avoid different behaviour between the JAXP implementations shipping with Java 1.5 and Java 1.6.

Parameters:
input - the input to parse.
schemaURL - URL point to schema, may not be null
Returns:
the DOM document.
Throws:
org.xml.sax.SAXException - if a parsing exception occurs, i.e. if the file is not well-formed or not valid
java.io.IOException - if an IO exception occurs.

parseSAX

public static void parseSAX(java.io.File file,
                            java.net.URL schemaURL,
                            org.xml.sax.helpers.DefaultHandler handler)
                     throws org.xml.sax.SAXException,
                            java.io.IOException
Parse and validate file using SAX and schema.

Parameters:
file - the file to parse.
schemaURL - URL point to schema, may not be null
Throws:
org.xml.sax.SAXException - if a parsing exception occurs, i.e. if the file is not well-formed or not valid
java.io.IOException - if an IO exception occurs.

parseSAX

public static void parseSAX(org.xml.sax.InputSource input,
                            java.net.URL schemaURL,
                            org.xml.sax.helpers.DefaultHandler handler)
                     throws org.xml.sax.SAXException,
                            java.io.IOException
Parse and validate file using SAX and schema.

Parameters:
input - the input to parse.
schemaURL - URL point to schema, may not be null
Throws:
org.xml.sax.SAXException - if a parsing exception occurs, i.e. if the file is not well-formed or not valid
java.io.IOException - if an IO exception occurs.

parse

public static org.w3c.dom.Document parse(java.io.File file,
                                         java.io.File schema)
                                  throws org.xml.sax.SAXException,
                                         java.io.IOException
Same as parse(File, URL) but with schema file.

Throws:
java.lang.IllegalArgumentException - if the schema file could not be converted to an URL
org.xml.sax.SAXException
java.io.IOException

prettyPrint

public static java.lang.String prettyPrint(org.w3c.dom.Document doc)
                                    throws javax.xml.transform.TransformerException
Returns a string representation of the given XML document, which is "pretty printed", i.e. the tags are indented.

Throws:
javax.xml.transform.TransformerException

getElementPosition

public static int getElementPosition(org.w3c.dom.Element element)
Determines the index (starting at 0) of the given element relative to other element nodes for the same parent.


getNamedChildren

public static java.util.List<org.w3c.dom.Element> getNamedChildren(org.w3c.dom.Element element,
                                                                   java.lang.String elementNames)
Returns all children of the given element which are element named as specified.


getNamedChild

public static org.w3c.dom.Element getNamedChild(org.w3c.dom.Element element,
                                                java.lang.String name)
Returns the first child of the given element which is an element named as specified. Returns null if none are found.


getNamedChildContent

public static java.lang.String getNamedChildContent(org.w3c.dom.Element parent,
                                                    java.lang.String name)
Get the text content of the given element's first child that is an element named as specified. If none is found, the empty string is returned.


elementNodes

public static java.util.List<org.w3c.dom.Element> elementNodes(org.w3c.dom.NodeList nodeList)
Extracts all ElementNodes from a NodeList and returns the result as a list.

Parameters:
nodeList - the NodeList to be searched for ElementNodes.
Returns:
an array containing all ElementNodes stored in the given node list or null if the input has been null.

leafElementNodes

public static java.util.List<org.w3c.dom.Element> leafElementNodes(org.w3c.dom.Element root)
Get all leaf elements of an XML tree rooted at an element

Parameters:
root - The root element
Returns:
List of all leaf elements

createEmptyDocument

public static org.w3c.dom.Document createEmptyDocument()
Creates an empty XML document.


TUM CCSM Commons

TUM CCSM Commons - 2.7