Class XMLHelper


  • @Immutable
    public final class XMLHelper
    extends Object
    This class contains multiple XML utility methods.
    Author:
    Philip Helger
    • Method Detail

      • getOwnerDocument

        @Nullable
        public static Document getOwnerDocument​(@Nullable
                                                Node aNode)
        Get the owner document of the passed node. If the node itself is a document, only a cast is performed.
        Parameters:
        aNode - The node to get the document from. May be null.
        Returns:
        null if the passed node was null.
      • hasNoNamespaceURI

        public static boolean hasNoNamespaceURI​(@Nonnull
                                                Node aNode)
      • isInlineNode

        public static boolean isInlineNode​(@Nullable
                                           Node aNode)
        Check if the passed node is a text node. This includes all nodes derived from Text (Text and CData) or EntityReference nodes.
        Parameters:
        aNode - The node to be checked.
        Returns:
        true if the passed node is a text node, false otherwise.
      • filterNodeIsElement

        @Nonnull
        public static com.helger.commons.functional.IPredicate<? super Node> filterNodeIsElement()
      • filterElementWithNamespace

        @Nonnull
        public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithNamespace()
      • filterElementWithoutNamespace

        @Nonnull
        public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithoutNamespace()
      • filterElementWithNamespace

        @Nonnull
        public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithNamespace​(@Nullable
                                                                                                           String sNamespaceURI)
      • filterElementWithNamespaceAndLocalName

        @Nonnull
        public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithNamespaceAndLocalName​(@Nullable
                                                                                                                       String sNamespaceURI,
                                                                                                                       @Nonnull @Nonempty
                                                                                                                       String sLocalName)
      • filterElementWithTagName

        @Nonnull
        public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithTagName​(@Nonnull @Nonempty
                                                                                                         String sTagName)
      • filterElementWithTagNameNoNS

        @Nonnull
        public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithTagNameNoNS​(@Nonnull @Nonempty
                                                                                                             String sTagName)
      • getFirstChildElement

        @Nullable
        public static Element getFirstChildElement​(@Nullable
                                                   Node aStartNode)
        Get the first direct child element of the passed element.
        Parameters:
        aStartNode - The element to start searching. May be null.
        Returns:
        null if the passed element does not have any direct child element.
      • hasChildElementNodes

        public static boolean hasChildElementNodes​(@Nullable
                                                   Node aStartNode)
        Check if the passed node has at least one direct child element or not.
        Parameters:
        aStartNode - The parent element to be searched. May be null.
        Returns:
        true if the passed node has at least one child element, false otherwise.
      • getFirstChildElementOfName

        @Nullable
        public static Element getFirstChildElementOfName​(@Nullable
                                                         Node aStartNode,
                                                         @Nonnull @Nonempty
                                                         String sTagName)
        Search all child nodes of the given for the first element that has the specified tag name.
        Parameters:
        aStartNode - The parent element to be searched. May be null.
        sTagName - The tag name to search.
        Returns:
        null if the parent element has no such child element.
      • getFirstChildElementOfName

        @Nullable
        public static Element getFirstChildElementOfName​(@Nullable
                                                         Node aStartNode,
                                                         @Nullable
                                                         String sNamespaceURI,
                                                         @Nonnull @Nonempty
                                                         String sLocalName)
        Search all child nodes of the given for the first element that has the specified tag name.
        Parameters:
        aStartNode - The parent element to be searched. May be null.
        sNamespaceURI - Namespace URI to search. May be null.
        sLocalName - The tag name to search.
        Returns:
        null if the parent element has no such child element.
      • getChildElementIterator

        @Nonnull
        public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIterator​(@Nullable
                                                                                                               Node aStartNode)
        Get an iterator over all child elements.
        Parameters:
        aStartNode - the parent element
        Returns:
        a non-null Iterator
      • getChildElementIteratorNoNS

        @Nonnull
        public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNoNS​(@Nullable
                                                                                                                   Node aStartNode)
        Get an iterator over all child elements that have no namespace.
        Parameters:
        aStartNode - the parent element
        Returns:
        a non-null Iterator
      • getChildElementIteratorNoNS

        @Nonnull
        public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNoNS​(@Nullable
                                                                                                                   Node aStartNode,
                                                                                                                   @Nonnull @Nonempty
                                                                                                                   String sTagName)
        Get an iterator over all child elements that have no namespace and the desired tag name.
        Parameters:
        aStartNode - the parent element
        sTagName - the name of the tag that is desired
        Returns:
        a non-null Iterator
        Throws:
        IllegalArgumentException - if the passed tag name is null or empty
      • getChildElementIterator

        @Nonnull
        public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIterator​(@Nullable
                                                                                                               Node aStartNode,
                                                                                                               @Nonnull @Nonempty
                                                                                                               String sTagName)
        Get an iterator over all child elements that have the desired tag name (but potentially a namespace URI).
        Parameters:
        aStartNode - the parent element
        sTagName - the name of the tag that is desired
        Returns:
        a non-null Iterator
        Throws:
        IllegalArgumentException - if the passed tag name is null or empty
      • getPathToNode

        @Nonnull
        public static String getPathToNode​(@Nonnull
                                           Node aNode,
                                           @Nonnull
                                           String sSep)
        Get the path from root node to the passed node. This includes all nodes up to the document node!
        Parameters:
        aNode - The node to start. May not be null.
        sSep - The separator string to use. May not be null.
        Returns:
        The path to the node.
      • getPathToNode2

        @Nonnull
        public static String getPathToNode2​(@Nonnull
                                            Node aNode,
                                            @Nonnull
                                            String sSep)
        Get the path from root node to the passed node. This includes all nodes but excluding the document node!
        Parameters:
        aNode - The node to start. May not be null.
        sSep - The separator string to use. May not be null.
        Returns:
        The path to the node.
      • removeAllChildElements

        public static void removeAllChildElements​(@Nonnull
                                                  Element aElement)
        Remove all child nodes of the given node.
        Parameters:
        aElement - The element whose children are to be removed.
      • getFirstChildText

        @Nullable
        public static String getFirstChildText​(@Nullable
                                               Node aStartNode)
        Get the content of the first Text child element of the passed element.
        Parameters:
        aStartNode - the element to scan for a TextNode child
        Returns:
        null if the element contains no text node as child
      • getAttributeValue

        @Nullable
        public static String getAttributeValue​(@Nonnull
                                               Element aElement,
                                               @Nonnull
                                               String sAttrName)
        The latest version of XercesJ 2.9 returns an empty string for non existing attributes. To differentiate between empty attributes and non-existing attributes, this method returns null for non existing attributes.
        Parameters:
        aElement - the source element to get the attribute from
        sAttrName - the name of the attribute to query
        Returns:
        null if the attribute does not exists, the string value otherwise
      • getAttributeValue

        @Nullable
        public static String getAttributeValue​(@Nonnull
                                               Element aElement,
                                               @Nonnull
                                               String sAttrName,
                                               @Nullable
                                               String sDefault)
        The latest version of XercesJ 2.9 returns an empty string for non existing attributes. To differentiate between empty attributes and non-existing attributes, this method returns a default value for non existing attributes.
        Parameters:
        aElement - the source element to get the attribute from. May not be null.
        sAttrName - the name of the attribute to query. May not be null.
        sDefault - the value to be returned if the attribute is not present.
        Returns:
        the default value if the attribute does not exists, the string value otherwise
      • getAttributeValueNS

        @Nullable
        public static String getAttributeValueNS​(@Nonnull
                                                 Element aElement,
                                                 @Nullable
                                                 String sNamespaceURI,
                                                 @Nonnull
                                                 String sAttrName)
        The latest version of XercesJ 2.9 returns an empty string for non existing attributes. To differentiate between empty attributes and non-existing attributes, this method returns null for non existing attributes.
        Parameters:
        aElement - the source element to get the attribute from
        sNamespaceURI - The namespace URI of the attribute to retrieve. May be null.
        sAttrName - the name of the attribute to query
        Returns:
        null if the attribute does not exists, the string value otherwise
      • getAttributeValueNS

        @Nullable
        public static String getAttributeValueNS​(@Nonnull
                                                 Element aElement,
                                                 @Nullable
                                                 String sNamespaceURI,
                                                 @Nonnull
                                                 String sAttrName,
                                                 @Nullable
                                                 String sDefault)
        The latest version of XercesJ 2.9 returns an empty string for non existing attributes. To differentiate between empty attributes and non-existing attributes, this method returns a default value for non existing attributes.
        Parameters:
        aElement - the source element to get the attribute from. May not be null.
        sNamespaceURI - The namespace URI of the attribute to retrieve. May be null.
        sAttrName - the name of the attribute to query. May not be null.
        sDefault - the value to be returned if the attribute is not present.
        Returns:
        the default value if the attribute does not exists, the string value otherwise
      • getAllAttributesAsList

        @Nonnull
        @ReturnsMutableCopy
        public static com.helger.commons.collection.impl.ICommonsList<Attr> getAllAttributesAsList​(@Nullable
                                                                                                   Element aSrcNode)
      • getAllAttributesAsMap

        @Nonnull
        @ReturnsMutableCopy
        public static com.helger.commons.collection.impl.ICommonsOrderedMap<String,​String> getAllAttributesAsMap​(@Nullable
                                                                                                                       Element aSrcNode)
      • getXMLNSAttrQName

        @Nonnull
        public static QName getXMLNSAttrQName​(@Nullable
                                              String sNSPrefix)
        Get the full qualified attribute name to use for the given namespace prefix. The result will e.g. be xmlns or {http://www.w3.org/2000/xmlns/}xmlns:foo.
        Parameters:
        sNSPrefix - The namespace prefix to build the attribute name from. May be null or empty.
        Returns:
        If the namespace prefix is empty (if it equals XMLConstants.DEFAULT_NS_PREFIX or null) than "xmlns" is returned, else "xmlns:prefix" is returned.
      • getQName

        @Nonnull
        public static QName getQName​(@Nonnull
                                     Element aElement)
        Get the QName of the passed element. If the passed element has no namespace URI, only the tag name is used. Otherwise namespace URI, local name and prefix are used.
        Parameters:
        aElement - The element to be used. May not be null.
        Returns:
        The created QName.
        Since:
        8.4.1