de.unidu.is.util
Class XMLUtilities

java.lang.Object
  extended byde.unidu.is.util.XMLUtilities

public class XMLUtilities
extends java.lang.Object

A class providing static utility methods for XML.

NOTE: This class currently reads XML documents only with the ISO-8859-1 encoding. This will change in the near future, after extended testing.

Since:
2002-09-25
Version:
$Revision: 1.15 $, $Date: 2005/03/19 17:13:19 $
Author:
Henrik Nottelmann

Constructor Summary
XMLUtilities()
           
 
Method Summary
protected static java.lang.String convert(java.lang.String str)
          Replaces all unicode entities "&#...;" by a space.
static java.lang.String extract(org.w3c.dom.Document document, java.lang.String xpath)
          Returns the value of the first node matching the specified XPATH expression.
static java.lang.String extract(org.w3c.dom.Element element, java.lang.String xpath)
          Returns the value of the first node matching the specified XPATH expression.
static void extract(org.w3c.dom.Element element, java.lang.StringBuffer buffer)
          Copies the text subnodes into the specified string buffer and appends two empty lines.
static double extractDouble(org.w3c.dom.Document document, java.lang.String xpath)
          Returns the value of the first node matching the specified XPATH expression.
static double extractDouble(org.w3c.dom.Element element, java.lang.String xpath)
          Returns the value of the first node matching the specified XPATH expression.
static int extractInt(org.w3c.dom.Document document, java.lang.String xpath)
          Returns the value of the first node matching the specified XPATH expression.
static int extractInt(org.w3c.dom.Element element, java.lang.String xpath)
          Returns the value of the first node matching the specified XPATH expression.
static java.lang.String getDefaultEncoding()
          Returns the default encoding.
static org.w3c.dom.Element getXHTML(java.io.InputStream source)
          Downloads an HTML file and converts it to XHTML.
static org.w3c.dom.Element getXHTML(java.lang.String url)
          Downloads a document given by the URL and converts it to XHTML.
static org.w3c.dom.Element getXHTML(java.net.URL url)
          Downloads a document given by the URLand converts it to XHTML.
static java.lang.String getXPath(org.w3c.dom.Node node)
          Returns a unique XPath expression (like /foo[1]/bar[42]) for the given node.
static boolean isNamespaceAware()
          Returns whether the parser is aware of namespaces.
static org.w3c.dom.Document parse(java.io.File file)
          Parses an XML file (default encoding) and returns a DOM document node.
static org.w3c.dom.Document parse(java.io.File file, java.lang.String encoding)
          Parses an XML file and returns a DOM document node.
static org.w3c.dom.Document parse(java.io.Reader reader)
          Parses an XML file and returns a DOM document node.
static org.w3c.dom.Document parse(java.lang.String filename)
          Parses an XML file (default encoding) and returns a DOM document node.
static org.w3c.dom.Document parse(java.lang.String filename, java.lang.String encoding)
          Parses an XML file and returns a DOM document node.
static org.w3c.dom.Document parse(java.net.URL url)
          Parses an XML file (default encoding) and returns a DOM document node.
static org.w3c.dom.Document parse(java.net.URL url, java.lang.String encoding)
          Parses an XML file and returns a DOM document node.
static org.w3c.dom.Document parseText(java.lang.String text)
          Parses an XML string and returns a DOM document node.
static void setDefaultEncoding(java.lang.String encoding)
          Sets the default encoding.
static void setNamespaceAware(boolean namespaceAware)
          Sets whether the parser is aware of namespaces.
static java.lang.String toString(org.w3c.dom.Document doc)
          Returns a string representation of the complete XML tree, properly formatted.
static java.lang.String toString(org.w3c.dom.Element element)
          Returns a string representation of the complete XML tree, properly formatted.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtilities

public XMLUtilities()
Method Detail

parse

public static org.w3c.dom.Document parse(java.net.URL url)
Parses an XML file (default encoding) and returns a DOM document node.

Parameters:
url - URL of the XML file
Returns:
DOM representation

parse

public static org.w3c.dom.Document parse(java.net.URL url,
                                         java.lang.String encoding)
Parses an XML file and returns a DOM document node.

Parameters:
url - URL of the XML file
encoding - file encoding
Returns:
DOM representation

parse

public static org.w3c.dom.Document parse(java.lang.String filename)
Parses an XML file (default encoding) and returns a DOM document node.

Parameters:
filename - name of the XML file
Returns:
DOM representation

parse

public static org.w3c.dom.Document parse(java.lang.String filename,
                                         java.lang.String encoding)
Parses an XML file and returns a DOM document node.

Parameters:
filename - name of the XML file
encoding - file encoding
Returns:
DOM representation

parse

public static org.w3c.dom.Document parse(java.io.File file)
Parses an XML file (default encoding) and returns a DOM document node.

Parameters:
file - XML file
Returns:
DOM representation

parse

public static org.w3c.dom.Document parse(java.io.File file,
                                         java.lang.String encoding)
Parses an XML file and returns a DOM document node.

Parameters:
file - XML file
encoding - file encoding
Returns:
DOM representation

parse

public static org.w3c.dom.Document parse(java.io.Reader reader)
Parses an XML file and returns a DOM document node.

Parameters:
reader - reader for the XML file
Returns:
DOM representation

parseText

public static org.w3c.dom.Document parseText(java.lang.String text)
Parses an XML string and returns a DOM document node.

Parameters:
text - XML string
Returns:
DOM representation

toString

public static java.lang.String toString(org.w3c.dom.Element element)
Returns a string representation of the complete XML tree, properly formatted.

Parameters:
element - XML tree
Returns:
string representation of the specified XML tree

toString

public static java.lang.String toString(org.w3c.dom.Document doc)
Returns a string representation of the complete XML tree, properly formatted.

Parameters:
doc - XML tree
Returns:
string representation of the specified XML tree

getXHTML

public static org.w3c.dom.Element getXHTML(java.lang.String url)
Downloads a document given by the URL and converts it to XHTML.

Parameters:
url - URL of the document
Returns:
root element of the X(HT)ML file

getXHTML

public static org.w3c.dom.Element getXHTML(java.net.URL url)
Downloads a document given by the URLand converts it to XHTML.

Parameters:
url - URL of the document
Returns:
root element of the X(HT)ML file

getXHTML

public static org.w3c.dom.Element getXHTML(java.io.InputStream source)
Downloads an HTML file and converts it to XHTML.

Parameters:
source - HTML source stream
Returns:
root element of the X(HT)ML file

convert

protected static java.lang.String convert(java.lang.String str)
Replaces all unicode entities "&#...;" by a space.

Parameters:
str - string to convert
Returns:
converted string

extract

public static java.lang.String extract(org.w3c.dom.Document document,
                                       java.lang.String xpath)
Returns the value of the first node matching the specified XPATH expression.

Parameters:
document - XML document from which the search should start
xpath - XPATH expression
Returns:
value of the first matching node

extract

public static java.lang.String extract(org.w3c.dom.Element element,
                                       java.lang.String xpath)
Returns the value of the first node matching the specified XPATH expression.

Parameters:
element - XML element from which the search should start
xpath - XPATH expression
Returns:
value of the first matching node

extractDouble

public static double extractDouble(org.w3c.dom.Document document,
                                   java.lang.String xpath)
Returns the value of the first node matching the specified XPATH expression.

Parameters:
document - XML document from which the search should start
xpath - XPATH expression
Returns:
value of the first matching node as a double

extractDouble

public static double extractDouble(org.w3c.dom.Element element,
                                   java.lang.String xpath)
Returns the value of the first node matching the specified XPATH expression.

Parameters:
element - XML element from which the search should start
xpath - XPATH expression
Returns:
value of the first matching node as a double

extractInt

public static int extractInt(org.w3c.dom.Document document,
                             java.lang.String xpath)
Returns the value of the first node matching the specified XPATH expression.

Parameters:
document - XML document from which the search should start
xpath - XPATH expression
Returns:
value of the first matching node as an int

extractInt

public static int extractInt(org.w3c.dom.Element element,
                             java.lang.String xpath)
Returns the value of the first node matching the specified XPATH expression.

Parameters:
element - XML element from which the search should start
xpath - XPATH expression
Returns:
value of the first matching node as an int

extract

public static void extract(org.w3c.dom.Element element,
                           java.lang.StringBuffer buffer)
Copies the text subnodes into the specified string buffer and appends two empty lines.

Parameters:
element - XML element whose text subnodes have to be added
buffer - string buffer for the result

getDefaultEncoding

public static java.lang.String getDefaultEncoding()
Returns the default encoding.

Returns:
default encoding

setDefaultEncoding

public static void setDefaultEncoding(java.lang.String encoding)
Sets the default encoding.

Parameters:
encoding - default encoding

getXPath

public static java.lang.String getXPath(org.w3c.dom.Node node)
Returns a unique XPath expression (like /foo[1]/bar[42]) for the given node.

Parameters:
node - XML node
Returns:
unique XPath expression

isNamespaceAware

public static boolean isNamespaceAware()
Returns whether the parser is aware of namespaces.

Returns:
true iff the parser cares about namespaces

setNamespaceAware

public static void setNamespaceAware(boolean namespaceAware)
Sets whether the parser is aware of namespaces.

Parameters:
namespaceAware - is true, the parser cares about namespaces