no.sintef.xml
Class XmlHelper

java.lang.Object
  extended byno.sintef.xml.XmlHelper

public final class XmlHelper
extends java.lang.Object

XML helper class.

Author:
Fredrik Vraalsen

Method Summary
static org.w3c.dom.Document createDocument(org.w3c.dom.Element elem)
          Create XML Document with the specified root element.
static org.w3c.dom.Document createDocument(java.lang.String namespaceUri, java.lang.String namespacePrefix, java.lang.String rootElement, org.w3c.dom.DocumentType doctype)
          Create XML Document of the specified DocumentType and with a root Element with the specified namespace, namespace prefix and name.
static java.lang.String genID()
          Generate unique identifier.
static org.w3c.dom.Document getDocument(java.net.URL url)
          Get XML Document from the specified URL.
static java.util.List getElements(org.w3c.dom.Element parent, java.lang.String namespaceURI, java.lang.String localName)
          Get child Elements matching the specified name and namespace.
static org.w3c.dom.Element getFirstElement(org.w3c.dom.Element parent, java.lang.String namespaceURI, java.lang.String localName)
          Get first child Element matching the specified name and namespace.
static javax.xml.transform.Transformer getIdentityTransformation()
          Get the identity (null) transformation.
static org.w3c.dom.Element getRootElement(org.w3c.dom.Node node)
          Get document root element if Node is Document, otherwise cast node to Element if appropriate.
static java.lang.String getText(org.w3c.dom.Element element)
          Get text content of element.
static java.lang.String getText(org.w3c.dom.Element parent, java.lang.String namespaceURI, java.lang.String localName)
          Get text content of first child element matching the specified name and namespace.
static javax.xml.transform.Transformer getTransformer(java.net.URL url, org.apache.log4j.Logger logger)
          Get the XSLT transformation from the specified URL.
static org.w3c.dom.Document parse(org.xml.sax.InputSource is)
          Parse XML Document from InputSource.
static byte[] xmlToUtf8(org.w3c.dom.Node node, boolean outputHeader)
          Serialize XML node to byte array in UTF-8 format.
static void xmlToUtf8(org.w3c.dom.Node node, java.io.OutputStream os, boolean outputHeader)
          Serialize XML node to specified output stream in UTF-8 format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getRootElement

public static org.w3c.dom.Element getRootElement(org.w3c.dom.Node node)
Get document root element if Node is Document, otherwise cast node to Element if appropriate. TODO rename to getElement?

Parameters:
node - node to get Element for
Returns:
Element corresponding to node, or null if node is not Document or Element instance

getFirstElement

public static org.w3c.dom.Element getFirstElement(org.w3c.dom.Element parent,
                                                  java.lang.String namespaceURI,
                                                  java.lang.String localName)
Get first child Element matching the specified name and namespace.

Parameters:
parent - parent Element
namespaceURI - the URI of the namespace to match
localName - the local name to match
Returns:
the first child Element matching local name and namespace, or null if none exists

getElements

public static java.util.List getElements(org.w3c.dom.Element parent,
                                         java.lang.String namespaceURI,
                                         java.lang.String localName)
Get child Elements matching the specified name and namespace.

Parameters:
parent - parent Element
namespaceURI - the URI of the namespace to match
localName - the local name to match
Returns:
List of matching child elements, possibly empty (never null)

getText

public static java.lang.String getText(org.w3c.dom.Element element)
Get text content of element.

Parameters:
element - the element
Returns:
the text content of element (possibly empty), or null if element is null

getText

public static java.lang.String getText(org.w3c.dom.Element parent,
                                       java.lang.String namespaceURI,
                                       java.lang.String localName)
Get text content of first child element matching the specified name and namespace.

Parameters:
parent - parent element
namespaceURI - the URI of the namespace to match
localName - the local name to match
Returns:
the text content of element, or null if no element matches or matching element is mixed or does not have text node child

parse

public static org.w3c.dom.Document parse(org.xml.sax.InputSource is)
                                  throws XmlException,
                                         java.io.IOException
Parse XML Document from InputSource.

Parameters:
is - the InputSource to parse from.
Returns:
resulting XML Document, never null
Throws:
XmlException - if XML document was malformed or other XML parsing error occured
java.io.IOException - if IO error occured reading from InputSource

genID

public static java.lang.String genID()
Generate unique identifier.

Returns:
the unique identifier

createDocument

public static org.w3c.dom.Document createDocument(java.lang.String namespaceUri,
                                                  java.lang.String namespacePrefix,
                                                  java.lang.String rootElement,
                                                  org.w3c.dom.DocumentType doctype)
Create XML Document of the specified DocumentType and with a root Element with the specified namespace, namespace prefix and name.

Parameters:
namespaceUri - URI of namespace for root element
namespacePrefix - prefix to use for root element namespace
rootElement - local name of root element
doctype - DocumentType of document
Returns:
the XML Document, or null if error occurs

createDocument

public static org.w3c.dom.Document createDocument(org.w3c.dom.Element elem)
Create XML Document with the specified root element.

Parameters:
elem - the root element
Returns:
the XML Document, or null if error occurs

getTransformer

public static javax.xml.transform.Transformer getTransformer(java.net.URL url,
                                                             org.apache.log4j.Logger logger)
                                                      throws XmlException
Get the XSLT transformation from the specified URL.

Parameters:
url - URL to get the XSLT transformation from, or null to get identity (null) transformation
logger - Logger to use for error messages
Returns:
the Transformer, never null
Throws:
XmlException - if error occurs

getIdentityTransformation

public static javax.xml.transform.Transformer getIdentityTransformation()
Get the identity (null) transformation.

Returns:
the identity (null) transformation

xmlToUtf8

public static byte[] xmlToUtf8(org.w3c.dom.Node node,
                               boolean outputHeader)
Serialize XML node to byte array in UTF-8 format.

Parameters:
node - the Node to serialize
outputHeader - true if XML header should be added, false otherwise
Returns:
byte array containing the UTF-8 bytes

xmlToUtf8

public static void xmlToUtf8(org.w3c.dom.Node node,
                             java.io.OutputStream os,
                             boolean outputHeader)
Serialize XML node to specified output stream in UTF-8 format.

Parameters:
node - the Node to serialize
os - the OutputStream to serialize to
outputHeader - true if XML header should be added, false otherwise

getDocument

public static org.w3c.dom.Document getDocument(java.net.URL url)
                                        throws XmlException
Get XML Document from the specified URL.

Parameters:
url - URL to parse from
Returns:
the XML Document
Throws:
XmlException - if error occurs


Copyright © 2003-2005 SINTEF. All Rights Reserved.