Class XMLHelper


  • public class XMLHelper
    extends java.lang.Object
    Helper methods to simplify boilerplate XML parsing code for org.w3c.dom XML objects
    Author:
    Scooter
    • Constructor Summary

      Constructors 
      Constructor Description
      XMLHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.w3c.dom.Element addChildElement​(org.w3c.dom.Element parentElement, java.lang.String elementName)
      Creates a new element called elementName and adds it to parentElement
      static org.w3c.dom.Document getNewDocument()
      Create a new, empty org.w3c.dom.Document
      static org.w3c.dom.Document inputStreamToDocument​(java.io.InputStream inputStream)
      Creates an org.w3c.dom.Document from the content of the inputStream
      static org.w3c.dom.Document loadXML​(java.lang.String fileName)
      Given a path to an XML file, parses into an org.w3c.dom.Document
      static void outputToStream​(org.w3c.dom.Document document, java.io.OutputStream outputStream)
      Given an org.w3c.dom.Document, writes it to the given outputStream
      static java.util.ArrayList<org.w3c.dom.Element> selectElements​(org.w3c.dom.Element element, java.lang.String xpathExpression)
      Gets a list of elements matching xpathExpression.
      static org.w3c.dom.Element selectParentElement​(org.w3c.dom.Element element, java.lang.String parentName)
      Given an element, searches upwards through ancestor Elements till the first Element matching the requests parentName is found.
      static org.w3c.dom.Element selectSingleElement​(org.w3c.dom.Element element, java.lang.String xpathExpression)
      If xpathExpression is a plain string with no '/' characterr, this is interpreted as a child element name to search for.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • XMLHelper

        public XMLHelper()
    • Method Detail

      • addChildElement

        public static org.w3c.dom.Element addChildElement​(org.w3c.dom.Element parentElement,
                                                          java.lang.String elementName)
        Creates a new element called elementName and adds it to parentElement
        Parameters:
        parentElement -
        elementName -
        Returns:
        the new child element
      • getNewDocument

        public static org.w3c.dom.Document getNewDocument()
                                                   throws javax.xml.parsers.ParserConfigurationException
        Create a new, empty org.w3c.dom.Document
        Returns:
        a new org.w3c.dom.Document
        Throws:
        javax.xml.parsers.ParserConfigurationException
      • loadXML

        public static org.w3c.dom.Document loadXML​(java.lang.String fileName)
                                            throws org.xml.sax.SAXException,
                                                   java.io.IOException,
                                                   javax.xml.parsers.ParserConfigurationException
        Given a path to an XML file, parses into an org.w3c.dom.Document
        Parameters:
        fileName - path to a readable XML file
        Returns:
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
        javax.xml.parsers.ParserConfigurationException
      • inputStreamToDocument

        public static org.w3c.dom.Document inputStreamToDocument​(java.io.InputStream inputStream)
                                                          throws org.xml.sax.SAXException,
                                                                 java.io.IOException,
                                                                 javax.xml.parsers.ParserConfigurationException
        Creates an org.w3c.dom.Document from the content of the inputStream
        Parameters:
        inputStream -
        Returns:
        a Document
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
        javax.xml.parsers.ParserConfigurationException
      • outputToStream

        public static void outputToStream​(org.w3c.dom.Document document,
                                          java.io.OutputStream outputStream)
                                   throws javax.xml.transform.TransformerException
        Given an org.w3c.dom.Document, writes it to the given outputStream
        Parameters:
        document -
        outputStream -
        Throws:
        javax.xml.transform.TransformerException
      • selectParentElement

        public static org.w3c.dom.Element selectParentElement​(org.w3c.dom.Element element,
                                                              java.lang.String parentName)
        Given an element, searches upwards through ancestor Elements till the first Element matching the requests parentName is found.
        Parameters:
        element - The starting element
        parentName - The tag name of the requested Element.
        Returns:
        The found element, or null if no matching element is found,
      • selectSingleElement

        public static org.w3c.dom.Element selectSingleElement​(org.w3c.dom.Element element,
                                                              java.lang.String xpathExpression)
                                                       throws javax.xml.xpath.XPathExpressionException
        If xpathExpression is a plain string with no '/' characterr, this is interpreted as a child element name to search for. If xpathExpression is an XPath expression, this is evaluated and is assumed to identify a single element.
        Parameters:
        element -
        xpathExpression -
        Returns:
        A single element or null if no match or the 1st match if matches more than 1
        Throws:
        javax.xml.xpath.XPathExpressionException
      • selectElements

        public static java.util.ArrayList<org.w3c.dom.Element> selectElements​(org.w3c.dom.Element element,
                                                                              java.lang.String xpathExpression)
                                                                       throws javax.xml.xpath.XPathExpressionException
        Gets a list of elements matching xpathExpression. If xpathExpression lacks a '/' character, only immediate children o element are searched over.
        If xpathExpression contains an '/' character, a full XPath search is made
        Parameters:
        element -
        xpathExpression -
        Returns:
        A possibly empty but non-null ArrayList
        Throws:
        javax.xml.xpath.XPathExpressionException