nl.gx.webmanager.handler.util
Class XMLUtil

java.lang.Object
  extended by nl.gx.webmanager.handler.util.XMLUtil

public class XMLUtil
extends java.lang.Object

Various xml manipulation methods.


Method Summary
static org.w3c.dom.Document createXmlDoc(java.lang.String xml)
          Creates a document for xml input.
static java.lang.String doc2Xml(org.w3c.dom.Document doc)
          Converts a document to xml.
static java.lang.String escapeToXML(java.lang.String value)
          Escapes special xml characters.
static java.lang.String getAttribute(org.w3c.dom.Node node, java.lang.String attributeName)
          Return the attribute with a specified name from a node.
static java.lang.String getChildContents(org.w3c.dom.Node node, java.lang.String childName)
          Return the text representation of the first child of the node with the specified name.
static org.w3c.dom.Node getChildNode(org.w3c.dom.Node parent, java.lang.String childName)
          Return the first child of the node with the specified name.
static org.w3c.dom.Node[] getChildNodes(org.w3c.dom.Node parent, java.lang.String childName)
          Return the children of the node with the specified name.
static java.lang.String getFileContents(java.lang.String fileName)
          Extracts the contents of a file.
static java.lang.String getTextContents(org.w3c.dom.Node node)
          Extracts the text content of a node.
static void main(java.lang.String[] args)
          Main method that reads an xsl and xml file and returns the transformation result.
static java.lang.String node2Xml(org.w3c.dom.Node node)
          Converts a node to xml.
static java.lang.String normalize(java.lang.String xml)
          Normalizes an xml string.
static void transform(java.lang.String stylesheet, java.io.Reader in, java.io.Writer out)
          Transforms xml from the input reader and writes the result to the writer.
static java.lang.String transform(java.lang.String stylesheet, java.lang.String xmlData)
          Transform xml with a stylesheet and return the result.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getChildContents

public static java.lang.String getChildContents(org.w3c.dom.Node node,
                                                java.lang.String childName)
Return the text representation of the first child of the node with the specified name.

Parameters:
node - The parent node
childName - The name of the child of which the text should be returned.
Returns:
The first child node represetation with the specified name or null if such a child could not be found.

getChildNode

public static org.w3c.dom.Node getChildNode(org.w3c.dom.Node parent,
                                            java.lang.String childName)
Return the first child of the node with the specified name.

Parameters:
node - The parent node
childName - The name of the child that should be returned.
Returns:
The first child node with the specified name or null if such a child could not be found.

getChildNodes

public static org.w3c.dom.Node[] getChildNodes(org.w3c.dom.Node parent,
                                               java.lang.String childName)
Return the children of the node with the specified name.

Parameters:
node - The parent node
childName - The name of the children that should be returned.
Returns:
The child nodes with the specified name.

escapeToXML

public static java.lang.String escapeToXML(java.lang.String value)
Escapes special xml characters.

Parameters:
value - The string that should be escaped.
Returns:
The escapes xml representation of the value value argument.

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Node node,
                                            java.lang.String attributeName)
Return the attribute with a specified name from a node.

Parameters:
node - The parent node of which the attribute should be returned.
attributeName - The name of the attribute that should be matched.
Returns:
The string contents of the matching attribute, or null if the attribute could not be found

getTextContents

public static java.lang.String getTextContents(org.w3c.dom.Node node)
Extracts the text content of a node.

Parameters:
node - The node of which the text representation should be returned.
Returns:
The text representation of the node or null if the argument is invalid.

createXmlDoc

public static org.w3c.dom.Document createXmlDoc(java.lang.String xml)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                org.xml.sax.SAXException,
                                                java.io.IOException
Creates a document for xml input.

Parameters:
xml - The xml that should be parsed and converted to a document.
Returns:
The document representation of the xml input.
Throws:
javax.xml.parsers.ParserConfigurationException - if a DocumentBuilder cannot be created which satisfies the configuration requested.
org.xml.sax.SAXException - If the source could not be parsed.
java.io.IOException - If an IO error occurs.

normalize

public static java.lang.String normalize(java.lang.String xml)
                                  throws org.xml.sax.SAXException
Normalizes an xml string.

Parameters:
xml - The xml that should be normalized
Returns:
The normalized equivalent of the xml.
Throws:
org.xml.sax.SAXException - if the source could not be parsed.

node2Xml

public static java.lang.String node2Xml(org.w3c.dom.Node node)
                                 throws javax.xml.transform.TransformerException
Converts a node to xml.

Parameters:
node - The node for which the xml representation should be returned.
Returns:
The xml source representation of the node.
Throws:
javax.xml.transform.TransformerException - if the contents of the node could not be normalized.

doc2Xml

public static java.lang.String doc2Xml(org.w3c.dom.Document doc)
                                throws javax.xml.transform.TransformerException
Converts a document to xml.

Parameters:
node - The document for which the xml representation should be returned.
Returns:
The xml source representation of the document.
Throws:
javax.xml.transform.TransformerException - if the contents of the document could not be normalized.

transform

public static java.lang.String transform(java.lang.String stylesheet,
                                         java.lang.String xmlData)
                                  throws javax.xml.transform.TransformerException
Transform xml with a stylesheet and return the result. The transformer is obtained from a pool so if you do multiple transformations with the same stylesheet, the transformer is reused from a pool.

Parameters:
stylesheet - the xsl stylesheet
xmlData - the xml
Returns:
result of transformation.
Throws:
javax.xml.transform.TransformerException - if something goes wrong during the transformation

transform

public static void transform(java.lang.String stylesheet,
                             java.io.Reader in,
                             java.io.Writer out)
                      throws javax.xml.transform.TransformerException
Transforms xml from the input reader and writes the result to the writer.

Parameters:
stylesheet - the xsl stylesheet
in - the xml input reader
out - the writer for the transformation result
Throws:
javax.xml.transform.TransformerException - if something goes wrong during the transformation

main

public static void main(java.lang.String[] args)
Main method that reads an xsl and xml file and returns the transformation result.

Parameters:
args - The file arguments

getFileContents

public static java.lang.String getFileContents(java.lang.String fileName)
                                        throws java.io.IOException
Extracts the contents of a file.

Parameters:
fileName - The absolute path to the file.
Returns:
The string contents of the file.
Throws:
java.io.IOException - if the file could not be read.


Copyright © 2007-2011 GX Software BV. All Rights Reserved.