uk.apl.jxc
Class XMLUtil

java.lang.Object
  extended by uk.apl.jxc.XMLUtil

public final class XMLUtil
extends java.lang.Object

XML handling utilities and helper methods.

Author:
Andrew Leigh

Method Summary
static void duplicateFirstElementByName(org.w3c.dom.Document doc, java.lang.String elementName)
          Modifies the document by adding a copy of the first node identified by element name.
static void duplicateNthElementByName(org.w3c.dom.Document doc, java.lang.String elementName, int nodeIndex)
          Modifies the document by adding a copy of the nth node identified by element name.
static java.lang.String firstXPathFunctionParameterValue(java.lang.String xPathFunctionStr)
          Returns the first parameter value from an XPath function call.
static java.lang.String getElementNameFromPath(java.lang.String path)
          Returns the element name from an XPath String.
static boolean isDocumentValid(org.w3c.dom.Document doc, java.io.File schemaFile)
          Determines an XML document validates against a specified schema.
static java.util.List<java.lang.String[]> parseXPathExpression(java.lang.String xPathExpression)
          Parses an XPath expression into a list of its constituent parts.
static org.w3c.dom.Document toXMLDocument(java.io.File xmlFile)
          Returns an XML Document read from an XML file.
static org.w3c.dom.Document toXMLDocument(java.io.File xmlFile, boolean namespaceAware)
          Returns a Document formed from an XML file.
static org.w3c.dom.Document toXMLDocument(java.lang.String xmlString)
          Returns a Document formed from an XML String.
static org.w3c.dom.Document toXMLDocument(java.lang.String xmlString, boolean namespaceAware)
          Returns a Document formed from an XML String.
static java.lang.String toXMLString(java.io.File xmlFile)
          Returns a String representation of an XML files content.
static java.lang.String toXMLString(org.w3c.dom.Node node)
          Returns a String representation of an XML Node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toXMLString

public static java.lang.String toXMLString(java.io.File xmlFile)
                                    throws javax.xml.parsers.ParserConfigurationException,
                                           org.xml.sax.SAXException,
                                           java.io.IOException,
                                           javax.xml.transform.TransformerConfigurationException,
                                           javax.xml.transform.TransformerException
Returns a String representation of an XML files content.

Parameters:
xmlFile - the XML file
Returns:
String representation of the XML file
Throws:
javax.xml.parsers.ParserConfigurationException - when an exception occurs configuring the parser
org.xml.sax.SAXException - when an exception occurs parsing the XML file
java.io.IOException - when an exception occurs reading the XML file
javax.xml.transform.TransformerConfigurationException - when an exception occurs configuring the transformet
javax.xml.transform.TransformerException - when an exception occurs during transformation

toXMLString

public static java.lang.String toXMLString(org.w3c.dom.Node node)
                                    throws javax.xml.transform.TransformerConfigurationException,
                                           javax.xml.transform.TransformerException
Returns a String representation of an XML Node.

Parameters:
node - the XML Node
Returns:
String representation of an XML Node
Throws:
javax.xml.transform.TransformerConfigurationException - when an exception occurs configuring the transformet
javax.xml.transform.TransformerException - when an exception occurs during transformation

toXMLDocument

public static org.w3c.dom.Document toXMLDocument(java.io.File xmlFile)
                                          throws java.io.IOException,
                                                 javax.xml.parsers.ParserConfigurationException,
                                                 org.xml.sax.SAXException
Returns an XML Document read from an XML file.

Parameters:
xmlFile - the XML file to read
Returns:
Document formed from an XML file
Throws:
java.io.IOException - when an exception occurs reading the XML file
javax.xml.transform.TransformerConfigurationException - when an exception occurs configuring the transformet
javax.xml.transform.TransformerException - when an exception occurs during transformation
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException

toXMLDocument

public static org.w3c.dom.Document toXMLDocument(java.io.File xmlFile,
                                                 boolean namespaceAware)
                                          throws java.io.IOException,
                                                 javax.xml.parsers.ParserConfigurationException,
                                                 org.xml.sax.SAXException
Returns a Document formed from an XML file.

Parameters:
xmlFile - the XML file
namespaceAware - true if the document should be namespace aware
Returns:
Document formed from an XML file
Throws:
java.io.IOException - when an exception occurs reading the XML file
javax.xml.transform.TransformerConfigurationException - when an exception occurs configuring the transformet
javax.xml.transform.TransformerException - when an exception occurs during transformation
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException

toXMLDocument

public static org.w3c.dom.Document toXMLDocument(java.lang.String xmlString)
                                          throws javax.xml.parsers.ParserConfigurationException,
                                                 org.xml.sax.SAXException,
                                                 java.io.IOException
Returns a Document formed from an XML String.

Parameters:
xmlString - the XML String
Returns:
Document formed from an XML String
Throws:
javax.xml.parsers.ParserConfigurationException - when an exception occurs configuring the parser
org.xml.sax.SAXException - when an exception occurs parsing the XML file
java.io.IOException - when an exception occurs reading the XML file

toXMLDocument

public static org.w3c.dom.Document toXMLDocument(java.lang.String xmlString,
                                                 boolean namespaceAware)
                                          throws javax.xml.parsers.ParserConfigurationException,
                                                 org.xml.sax.SAXException,
                                                 java.io.IOException
Returns a Document formed from an XML String.

Parameters:
xmlString - the XML String
namespaceAware - true if the document should be namespace aware
Returns:
Document formed from an XML String
Throws:
javax.xml.parsers.ParserConfigurationException - when an exception occurs configuring the parser
org.xml.sax.SAXException - when an exception occurs parsing the XML file
java.io.IOException - when an exception occurs reading the XML file

getElementNameFromPath

public static java.lang.String getElementNameFromPath(java.lang.String path)
Returns the element name from an XPath String. I.e. the last element name found in the path.

Parameters:
path - the XPath String
Returns:
the last element name found in the path

isDocumentValid

public static boolean isDocumentValid(org.w3c.dom.Document doc,
                                      java.io.File schemaFile)
                               throws java.io.IOException
Determines an XML document validates against a specified schema.

Parameters:
doc - the document to be validated
schemaFile - the XSD to validate the document against
Returns:
true if the document is value, false otherwise
Throws:
java.io.IOException - if the schema cannot be loaded

duplicateFirstElementByName

public static void duplicateFirstElementByName(org.w3c.dom.Document doc,
                                               java.lang.String elementName)
Modifies the document by adding a copy of the first node identified by element name.

Parameters:
doc - the document to be modified
elementName - name of the element to be duplicated

duplicateNthElementByName

public static void duplicateNthElementByName(org.w3c.dom.Document doc,
                                             java.lang.String elementName,
                                             int nodeIndex)
Modifies the document by adding a copy of the nth node identified by element name.

Parameters:
doc - the document to be modified
elementName - name of the element to be duplicated
nodeIndex - occurence of the element to be duplicated

firstXPathFunctionParameterValue

public static java.lang.String firstXPathFunctionParameterValue(java.lang.String xPathFunctionStr)
Returns the first parameter value from an XPath function call. For example, the input: contains(elementName, 'blah') would return elementName.

Parameters:
xPathFunctionStr - string representation of an XPath function call
Returns:
the first parameter value found in the call string or the unaltered string if no parameters are found

parseXPathExpression

public static java.util.List<java.lang.String[]> parseXPathExpression(java.lang.String xPathExpression)
Parses an XPath expression into a list of its constituent parts. For example the following query input:
contains(element1, 'blah') = true() and element2 > 1 or element3 < number('23')
would return a list containing rge following contents:
List IndexArray IndexContents
00
01contains(element1, 'blah')
02=
03true()
10and
11element2
12>
131
20or
21element3
22<
23number('23')

Parameters:
xPathExpression - the XPath expression to be parsed
Returns:
a list containing the expressions constituent parts


Copyright 2010 Andrew Leigh. All Rights Reserved.