Class XmlUtils


  • public final class XmlUtils
    extends java.lang.Object

    Utility class for simplifying parsing of xml documents. Documents are not validated, and loading of external files (xinclude, external entities, DTDs, etc.) are disabled.

    Since:
    3.0
    Version:
    $$
    Author:
    Claude Brisson
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static javax.xml.parsers.DocumentBuilderFactory createDocumentBuilderFactory()  
      static java.lang.String getAttribute​(org.w3c.dom.Node node, java.lang.String attr)  
      static java.lang.String getAttribute​(org.w3c.dom.Node node, java.lang.String attr, java.lang.String def)
      Extracts an attribute from a node.
      static boolean getBoolAttribute​(org.w3c.dom.Node node, java.lang.String attr)  
      static boolean getBoolAttribute​(org.w3c.dom.Node node, java.lang.String attr, boolean def)
      Retrieves an attribute as a boolean.
      static java.util.List<org.w3c.dom.Element> getElements​(java.lang.String xpath, org.w3c.dom.Node context)
      Search for elements using an XPath expression
      static int getIntAttribute​(org.w3c.dom.Node node, java.lang.String attr)  
      static int getIntAttribute​(org.w3c.dom.Node node, java.lang.String attr, int def)  
      static java.util.List<org.w3c.dom.Node> getNodes​(java.lang.String xpath, org.w3c.dom.Node context)
      Search for nodes using an XPath expression
      static boolean isXmlMimeType​(java.lang.String mimeType)
      Checkes whether the given mime type is an XML format
      static java.lang.String nodePath​(org.w3c.dom.Node n)
      Builds the xpath expression for a node (tries to use id/name nodes when possible to get a unique path)
      static java.lang.String nodeToString​(org.w3c.dom.Node node)
      XML Node to string
      static org.w3c.dom.Element parse​(java.io.Reader xml)
      Attempts to parse the input xml into a single element.
      static org.w3c.dom.Element parse​(java.lang.String xml)
      Attempts to parse the input xml into a single element.
      static org.w3c.dom.NodeList search​(java.lang.String xpath, org.w3c.dom.Node context)
      Search for nodes using an XPath expression
      • Methods inherited from class java.lang.Object

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

      • createDocumentBuilderFactory

        public static final javax.xml.parsers.DocumentBuilderFactory createDocumentBuilderFactory()
      • getAttribute

        public static java.lang.String getAttribute​(org.w3c.dom.Node node,
                                                    java.lang.String attr,
                                                    java.lang.String def)
        Extracts an attribute from a node.
        Parameters:
        node - target node
        attr - attribute name
        def - default value
        Returns:
        The value of the attribute, or def
      • getAttribute

        public static java.lang.String getAttribute​(org.w3c.dom.Node node,
                                                    java.lang.String attr)
        Parameters:
        node - target node
        attr - attribute name
        Returns:
        The value of the given attribute, or null if not present.
      • getBoolAttribute

        public static boolean getBoolAttribute​(org.w3c.dom.Node node,
                                               java.lang.String attr,
                                               boolean def)
        Retrieves an attribute as a boolean.
        Parameters:
        node - target node
        attr - attribute name
        def - default value
        Returns:
        True if the attribute exists and is not equal to "false" false if equal to "false", and def if not present.
      • getBoolAttribute

        public static boolean getBoolAttribute​(org.w3c.dom.Node node,
                                               java.lang.String attr)
        Parameters:
        node - target node
        attr - attribute name
        Returns:
        True if the attribute exists and is not equal to "false" false otherwise.
      • getIntAttribute

        public static int getIntAttribute​(org.w3c.dom.Node node,
                                          java.lang.String attr,
                                          int def)
        Parameters:
        node - target node
        attr - attribute name
        def - default value
        Returns:
        An attribute coerced to an integer.
      • getIntAttribute

        public static int getIntAttribute​(org.w3c.dom.Node node,
                                          java.lang.String attr)
        Parameters:
        node - target node
        attr - attribute name
        Returns:
        An attribute coerced to an integer.
      • parse

        public static org.w3c.dom.Element parse​(java.io.Reader xml)
        Attempts to parse the input xml into a single element.
        Parameters:
        xml - xml stream reader
        Returns:
        The document object
      • parse

        public static org.w3c.dom.Element parse​(java.lang.String xml)
        Attempts to parse the input xml into a single element.
        Parameters:
        xml - xml string
        Returns:
        The document object
      • search

        public static org.w3c.dom.NodeList search​(java.lang.String xpath,
                                                  org.w3c.dom.Node context)
                                           throws javax.xml.xpath.XPathExpressionException
        Search for nodes using an XPath expression
        Parameters:
        xpath - XPath expression
        context - evaluation context
        Returns:
        org.w3c.NodeList of found nodes
        Throws:
        javax.xml.xpath.XPathExpressionException
      • getNodes

        public static java.util.List<org.w3c.dom.Node> getNodes​(java.lang.String xpath,
                                                                org.w3c.dom.Node context)
                                                         throws javax.xml.xpath.XPathExpressionException
        Search for nodes using an XPath expression
        Parameters:
        xpath - XPath expression
        context - evaluation context
        Returns:
        List of found nodes
        Throws:
        javax.xml.xpath.XPathExpressionException
      • getElements

        public static java.util.List<org.w3c.dom.Element> getElements​(java.lang.String xpath,
                                                                      org.w3c.dom.Node context)
                                                               throws javax.xml.xpath.XPathExpressionException
        Search for elements using an XPath expression
        Parameters:
        xpath - XPath expression
        context - evaluation context
        Returns:
        List of found elements
        Throws:
        javax.xml.xpath.XPathExpressionException
      • nodePath

        public static java.lang.String nodePath​(org.w3c.dom.Node n)

        Builds the xpath expression for a node (tries to use id/name nodes when possible to get a unique path)

        Parameters:
        n - target node
        Returns:
        node xpath
      • nodeToString

        public static java.lang.String nodeToString​(org.w3c.dom.Node node)
        XML Node to string
        Parameters:
        node - XML node
        Returns:
        XML node string representation
      • isXmlMimeType

        public static boolean isXmlMimeType​(java.lang.String mimeType)
        Checkes whether the given mime type is an XML format
        Parameters:
        mimeType - mime type
        Returns:
        true if this mime type is an XML format