Class XMLHelper


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

      • XMLHelper

        public XMLHelper()
    • Method Detail

      • addChildElement

        public static Element addChildElement​(Element parentElement,
                                              String elementName)
        Creates a new element called elementName and adds it to parentElement
        Parameters:
        parentElement -
        elementName -
        Returns:
        the new child element
      • selectParentElement

        public static Element selectParentElement​(Element element,
                                                  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 Element selectSingleElement​(Element element,
                                                  String xpathExpression)
                                           throws 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:
        XPathExpressionException
      • selectElements

        public static ArrayList<Element> selectElements​(Element element,
                                                        String xpathExpression)
                                                 throws 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:
        XPathExpressionException