Class XmlUtil


  • public class XmlUtil
    extends java.lang.Object
    XML utility
    Since:
    3.8.9
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  XmlUtil.ElementVisitor
      XML element visitor.
    • Constructor Summary

      Constructors 
      Constructor Description
      XmlUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addAttributes​(org.w3c.dom.Element elem, java.lang.String[] attributes)
      Add the attributes to the element.
      static org.w3c.dom.Element addChildElementTextValue​(org.w3c.dom.Element element, java.lang.String childElementTagName, java.lang.String textValue)
      Add a child element with the text content.
      static org.w3c.dom.Element addChildElementTextValue​(org.w3c.dom.Element element, java.lang.String childElementTagName, java.lang.String textValue, boolean cdata)
      Add a child element with the text content.
      static org.w3c.dom.Element addChildElementTextValue​(org.w3c.dom.Element element, java.lang.String childElementTagName, java.lang.String textValue, boolean cdata, boolean ignoreIfNullValue)
      Add a child element with the text content.
      static boolean containsChildElements​(org.w3c.dom.Element element)
      Does the element contain any child elements?
      static org.w3c.dom.Element createChildElement​(org.w3c.dom.Element elem, java.lang.String childElemTagName, java.lang.String[] attributes)
      Create and append the child element.
      static org.w3c.dom.Element createElement​(org.w3c.dom.Document document, java.lang.String tagName, java.lang.String[] attributes)
      Create element.
      static org.w3c.dom.Element createElementFromMap​(org.w3c.dom.Document document, java.lang.String elementName, java.util.Map<java.lang.String,​java.lang.Object> map)
      Create element(subtree) from a map that can contains maps.
      static java.util.Map<java.lang.String,​java.lang.Object> createMapFromElement​(org.w3c.dom.Element element)
      Create a map from element(subtree).
      static java.lang.String elementToString​(org.w3c.dom.Element element)
      Get the element as string including tagName and attributes and excluding descendants.
      static java.lang.Object evaluateXPath​(org.w3c.dom.Document doc, java.lang.String xpath, javax.xml.namespace.QName returnType)
      Evaluate an XPath.
      static java.lang.Boolean getAttributeValueBoolean​(org.w3c.dom.Element element, java.lang.String attrName, java.lang.Boolean defaultValue)
      Get attribute value converted to Boolean.
      static java.lang.Class getAttributeValueClass​(org.w3c.dom.Element element, java.lang.String attrName, java.lang.Class defaultClass)
      Get attribute value as Class.
      static java.util.Date getAttributeValueDate​(org.w3c.dom.Element element, java.lang.String attrName, java.util.Date defaultValue)
      Get attribute value converted to Date.
      static java.lang.Integer getAttributeValueInteger​(org.w3c.dom.Element element, java.lang.String attrName, java.lang.Integer defaultValue)
      Get attribute value converted to Integer.
      static java.lang.String getAttributeValueString​(org.w3c.dom.Element element, java.lang.String attrName, java.lang.String defaultValue)
      Get attribute value.
      static java.util.List<org.w3c.dom.Element> getChildElements​(org.w3c.dom.Element element)
      Get child elements of the element, excluding all the nodes that are not elements.
      static java.util.List<org.w3c.dom.Element> getChildElements​(org.w3c.dom.Element element, java.lang.String... tagNames)
      Get the child elements with the tag names.
      static java.util.List<java.lang.String> getChildElementTagNames​(org.w3c.dom.Element element)
      Get the list of child element tag names.
      static java.lang.String getChildElementTextContent​(org.w3c.dom.Element element, java.lang.String childElementTagName)
      Get text content of the first child element with the tag name.
      static java.util.List<java.lang.String> getChildElementTextList​(org.w3c.dom.Element element, java.lang.String childElemTagName)
      Get the list of child element text contents.
      static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
      Get DocumentBuilder.
      static javax.xml.parsers.DocumentBuilder getDocumentBuilder​(boolean ignoreNamespace, boolean validate, boolean ignoreWhitespace)
      Get DocumentBuilder.
      static org.w3c.dom.Element getExactOneChildElement​(org.w3c.dom.Element element)
      Get the child element of the specified element.
      static org.w3c.dom.Element getFirstChildElement​(org.w3c.dom.Element element, java.lang.String childElementTagName)
      Get the first child element with the tag name.
      static boolean hasChildElements​(org.w3c.dom.Element element)
      Whether the element has any child elements?
      static org.w3c.dom.Document parse​(java.io.File xmlFile)
      Parse the xml file.
      static org.w3c.dom.Document parse​(java.io.InputStream xmlInput)
      Parse the xml from input stream.
      static void setAttribute​(org.w3c.dom.Element element, java.lang.String attrName, java.lang.String attrValue)
      Set the attribute of the XML element.
      static org.w3c.dom.Element setChildElementTextValue​(org.w3c.dom.Element element, java.lang.String childElementTagName, java.lang.String textValue)
      Set child element text value.
      static org.w3c.dom.Element setChildElementTextValue​(org.w3c.dom.Element element, java.lang.String childElementTagName, java.lang.String textValue, boolean cdata)
      Set child element text value.
      static boolean visitElementRecursive​(org.w3c.dom.Element element, XmlUtil.ElementVisitor visitor)
      Visit element tree recursively in parent-first order, calling the visitor.
      static void writeXmlDocument​(org.w3c.dom.Node node, boolean indent, boolean omitXmlDeclaration, java.io.Writer writer)
      Write the node including its descendant nodes to the writer.
      static java.lang.String xmlDocumentToString​(org.w3c.dom.Node node, boolean indent, boolean omitXmlDeclaration)
      Get the string representation of the XML node including its subtree.
      static java.lang.String xmlDocumentToString​(org.w3c.dom.Node node, boolean indent, boolean omitXmlDeclaration, boolean excludeNodeItself, boolean removeCDATAWrapper)
      Get the string representation of the XML node including its subtree.
      static org.w3c.dom.Document xmlStringToDocument​(java.lang.String xmlString)
      Parse the text to XML document
      static org.w3c.dom.DocumentFragment xmlStringToDocumentFragment​(java.lang.String xmlFragment)
      Parse the string to DocumentFragment
      • Methods inherited from class java.lang.Object

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

      • XmlUtil

        public XmlUtil()
    • Method Detail

      • getDocumentBuilder

        public static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
        Get DocumentBuilder. shorthand for getDocumentBuilder(false, true, true)
        Returns:
        DocumentBuilder instance.
      • getDocumentBuilder

        public static javax.xml.parsers.DocumentBuilder getDocumentBuilder​(boolean ignoreNamespace,
                                                                           boolean validate,
                                                                           boolean ignoreWhitespace)
        Get DocumentBuilder.
        Parameters:
        ignoreNamespace - whether to ignore namespace
        validate - whether to validate with DTD
        ignoreWhitespace - whether to ignore whitespace
        Returns:
        DocumentBuilder instance.
      • parse

        public static org.w3c.dom.Document parse​(java.io.File xmlFile)
                                          throws ExecException
        Parse the xml file.
        Parameters:
        xmlFile - XML file
        Returns:
        XML document
        Throws:
        ExecException - throw if any IO or parsing exception occurs
      • parse

        public static org.w3c.dom.Document parse​(java.io.InputStream xmlInput)
                                          throws ExecException
        Parse the xml from input stream.
        Parameters:
        xmlInput - XML input stream
        Returns:
        XML document
        Throws:
        ExecException - throw if any IO or parsing exception occurs
      • createElementFromMap

        public static org.w3c.dom.Element createElementFromMap​(org.w3c.dom.Document document,
                                                               java.lang.String elementName,
                                                               java.util.Map<java.lang.String,​java.lang.Object> map)
        Create element(subtree) from a map that can contains maps. value.toString() is used to convert value to a string.
         <map1>
           <key1>value</key1>
           
           <map2>
             <key2>value2</key2>
           </map2>
         </map1>
         
        Parameters:
        document - XML document
        elementName - root element name
        map - a map
        Returns:
        element
      • createMapFromElement

        public static java.util.Map<java.lang.String,​java.lang.Object> createMapFromElement​(org.w3c.dom.Element element)
        Create a map from element(subtree). key is element name, value is text or nested map.
         <map1>
           <key1>value</key1>
           
           <map2>
             <key2>value2</key2>
           </map2>
         </map1>
         
        Parameters:
        element - root element
        Returns:
        map
      • xmlStringToDocumentFragment

        public static org.w3c.dom.DocumentFragment xmlStringToDocumentFragment​(java.lang.String xmlFragment)
                                                                        throws ExecException
        Parse the string to DocumentFragment
        Parameters:
        xmlFragment - string of xml fragment that can contain more than one root elements.
        Returns:
        DocumentFragment
        Throws:
        ExecException - throw if any IO or parsing exception occurs
      • writeXmlDocument

        public static void writeXmlDocument​(org.w3c.dom.Node node,
                                            boolean indent,
                                            boolean omitXmlDeclaration,
                                            java.io.Writer writer)
                                     throws ExecException
        Write the node including its descendant nodes to the writer.
        Parameters:
        node - the root node to write
        indent - whether to indent child nodes
        omitXmlDeclaration - whether to omit xml declaration
        writer - the writer to write the node to
        Throws:
        ExecException - throw if any IO error occurs
      • xmlDocumentToString

        public static java.lang.String xmlDocumentToString​(org.w3c.dom.Node node,
                                                           boolean indent,
                                                           boolean omitXmlDeclaration)
                                                    throws ExecException
        Get the string representation of the XML node including its subtree.
        Parameters:
        node - the root node
        indent - whether to indent child nodes
        omitXmlDeclaration - whether to omit XML declaration
        Returns:
        XML text document
        Throws:
        ExecException - throw if any exception occurs during transforming the XML node subtree to a text
      • xmlDocumentToString

        public static java.lang.String xmlDocumentToString​(org.w3c.dom.Node node,
                                                           boolean indent,
                                                           boolean omitXmlDeclaration,
                                                           boolean excludeNodeItself,
                                                           boolean removeCDATAWrapper)
                                                    throws ExecException
        Get the string representation of the XML node including its subtree.
        Parameters:
        node - the root node
        indent - whether to indent child nodes
        omitXmlDeclaration - whether to omit XML declaration
        excludeNodeItself - whether to exclude the node itself. True for children only.
        removeCDATAWrapper - whether to remove CDATA wrapper for root node. If excludeNodeItself is true, all its children will become root nodes.
        Returns:
        XML text document
        Throws:
        ExecException - throw if any exception occurs during transforming the XML node subtree to a text
        Since:
        6.0
      • xmlStringToDocument

        public static org.w3c.dom.Document xmlStringToDocument​(java.lang.String xmlString)
                                                        throws ExecException
        Parse the text to XML document
        Parameters:
        xmlString - xml text
        Returns:
        XML document for the text
        Throws:
        ExecException - throw if any parsing error occurs
      • setAttribute

        public static void setAttribute​(org.w3c.dom.Element element,
                                        java.lang.String attrName,
                                        java.lang.String attrValue)
        Set the attribute of the XML element. Remove the attribute if value is null.
        Parameters:
        element - XML element
        attrName - attribute name
        attrValue - attribute value
      • hasChildElements

        public static boolean hasChildElements​(org.w3c.dom.Element element)
        Whether the element has any child elements?
        Parameters:
        element - the element to check
        Returns:
        true if the element has child elements
      • getChildElementTextContent

        public static java.lang.String getChildElementTextContent​(org.w3c.dom.Element element,
                                                                  java.lang.String childElementTagName)
        Get text content of the first child element with the tag name. The text value will be trimmed, and return null if the trimmed value is empty. <pre> <element> <childElementTagName>TextValue</childElementTagName> </element> </pre>
        Parameters:
        element - parent element
        childElementTagName - child element tag name
        Returns:
        the text content of the child element. return null if the child element is not found.
      • getChildElementTagNames

        public static java.util.List<java.lang.String> getChildElementTagNames​(org.w3c.dom.Element element)
        Get the list of child element tag names. <pre> <element> <childElem1></childElem1> <childElem2></childElem1> <childElem3></childElem1> </element> </pre>
        Parameters:
        element - a parent element
        Returns:
        a list of child tag names. return empty list if there are no child elements.
        Since:
        5.13
      • getChildElementTextList

        public static java.util.List<java.lang.String> getChildElementTextList​(org.w3c.dom.Element element,
                                                                               java.lang.String childElemTagName)
        Get the list of child element text contents. <pre> <element> <childElem>Text1</childElem> <childElem>Text2</childElem> <childElem>Text3</childElem> </element> </pre>
        Parameters:
        element - a parent element
        childElemTagName - child element tag name
        Returns:
        a list of child element text content. return empty list if there are no such child elements.
        Since:
        5.13
      • containsChildElements

        public static boolean containsChildElements​(org.w3c.dom.Element element)
        Does the element contain any child elements?
        Parameters:
        element - an XML element
        Returns:
        true if the element has any child elements, false otherwise
        Since:
        6.0
      • getExactOneChildElement

        public static org.w3c.dom.Element getExactOneChildElement​(org.w3c.dom.Element element)
        Get the child element of the specified element. Exactly one child element is expected.
        Parameters:
        element - an XML element
        Returns:
        child element
        Throws:
        java.lang.IllegalArgumentException - throw if none or more than one child element is found
        Since:
        6.0
      • getFirstChildElement

        public static org.w3c.dom.Element getFirstChildElement​(org.w3c.dom.Element element,
                                                               java.lang.String childElementTagName)
        Get the first child element with the tag name.
        Parameters:
        element - parent element
        childElementTagName - child element tag name, null for any element
        Returns:
        the first child element with the tag name.
      • setChildElementTextValue

        public static org.w3c.dom.Element setChildElementTextValue​(org.w3c.dom.Element element,
                                                                   java.lang.String childElementTagName,
                                                                   java.lang.String textValue)
        Set child element text value. Create the child element if not exist. <pre> <element> <childElementTagName>TextValue</childElementTagName> </element> </pre>
        Parameters:
        element - XML element
        childElementTagName - child element tag name
        textValue - child element text content
        Returns:
        existing or created child element
      • setChildElementTextValue

        public static org.w3c.dom.Element setChildElementTextValue​(org.w3c.dom.Element element,
                                                                   java.lang.String childElementTagName,
                                                                   java.lang.String textValue,
                                                                   boolean cdata)
        Set child element text value. Create the child element if not exist. The first matching child element will be updated, supporting CDATA. <pre> <element> <childElementTagName>TextValue</childElementTagName> </element> </pre>
        Parameters:
        element - XML element
        childElementTagName - child element tag name
        textValue - child element text content
        cdata - whether the text is inserted as CDATA
        Returns:
        existing or created child element
      • addChildElementTextValue

        public static org.w3c.dom.Element addChildElementTextValue​(org.w3c.dom.Element element,
                                                                   java.lang.String childElementTagName,
                                                                   java.lang.String textValue)
        Add a child element with the text content. Null text value will be ignored.
        Parameters:
        element - parent element
        childElementTagName - child element tag name
        textValue - the text content of the child element to be created (not CDATA).
        Returns:
        the created child element. return null if textValue is null.
      • addChildElementTextValue

        public static org.w3c.dom.Element addChildElementTextValue​(org.w3c.dom.Element element,
                                                                   java.lang.String childElementTagName,
                                                                   java.lang.String textValue,
                                                                   boolean cdata)
        Add a child element with the text content. Null text value will be ignored.
        Parameters:
        element - parent element
        childElementTagName - child element tag name
        textValue - the text content of the child element to be created
        cdata - whether the text is a CDATA
        Returns:
        the created child element. return null if textValue is null.
      • addChildElementTextValue

        public static org.w3c.dom.Element addChildElementTextValue​(org.w3c.dom.Element element,
                                                                   java.lang.String childElementTagName,
                                                                   java.lang.String textValue,
                                                                   boolean cdata,
                                                                   boolean ignoreIfNullValue)
        Add a child element with the text content.
        Parameters:
        element - parent element
        childElementTagName - child element tag name
        textValue - the text content of the child element to be created
        cdata - whether the text is a CDATA
        ignoreIfNullValue - whether to ignore if the textValue is null.
        Returns:
        the created child element. return null if valueText is null and ignored
      • getAttributeValueString

        public static java.lang.String getAttributeValueString​(org.w3c.dom.Element element,
                                                               java.lang.String attrName,
                                                               java.lang.String defaultValue)
        Get attribute value. If the attribute value is empty string, return default value.
        Parameters:
        element - XML element
        attrName - attribute name
        defaultValue - default value
        Returns:
        attribute value
      • getAttributeValueBoolean

        public static java.lang.Boolean getAttributeValueBoolean​(org.w3c.dom.Element element,
                                                                 java.lang.String attrName,
                                                                 java.lang.Boolean defaultValue)
        Get attribute value converted to Boolean. If the attribute value is empty string, return default value.
        Parameters:
        element - XML element
        attrName - attribute name
        defaultValue - default value
        Returns:
        attribute value as Boolean
      • getAttributeValueInteger

        public static java.lang.Integer getAttributeValueInteger​(org.w3c.dom.Element element,
                                                                 java.lang.String attrName,
                                                                 java.lang.Integer defaultValue)
        Get attribute value converted to Integer. If the attribute value is empty string, return default value.
        Parameters:
        element - XML element
        attrName - attribute name
        defaultValue - default value
        Returns:
        attribute value as Integer
      • getAttributeValueDate

        public static java.util.Date getAttributeValueDate​(org.w3c.dom.Element element,
                                                           java.lang.String attrName,
                                                           java.util.Date defaultValue)
        Get attribute value converted to Date. If the attribute value is empty string, return default value.
        Parameters:
        element - XML element
        attrName - attribute name
        defaultValue - default value
        Returns:
        attribute value as Date
      • getAttributeValueClass

        public static java.lang.Class getAttributeValueClass​(org.w3c.dom.Element element,
                                                             java.lang.String attrName,
                                                             java.lang.Class defaultClass)
        Get attribute value as Class. If the attribute value is empty string, return default class.
        Parameters:
        element - XML element
        attrName - attribute name
        defaultClass - default class
        Returns:
        attribute value as
        Throws:
        java.lang.IllegalArgumentException - throw if the class not found
        Since:
        5.14
      • getChildElements

        public static java.util.List<org.w3c.dom.Element> getChildElements​(org.w3c.dom.Element element)
        Get child elements of the element, excluding all the nodes that are not elements.
        Parameters:
        element - parent element
        Returns:
        a list of child elements
      • getChildElements

        public static java.util.List<org.w3c.dom.Element> getChildElements​(org.w3c.dom.Element element,
                                                                           java.lang.String... tagNames)
        Get the child elements with the tag names. If tagNames is null, get all the child elements.
        Parameters:
        element - parent element
        tagNames - child element tag names
        Returns:
        a list of child elements
      • evaluateXPath

        public static java.lang.Object evaluateXPath​(org.w3c.dom.Document doc,
                                                     java.lang.String xpath,
                                                     javax.xml.namespace.QName returnType)
                                              throws ExecException
        Evaluate an XPath.
        Parameters:
        doc - an XML document
        xpath - XPath expression
        returnType - return type. see constants XPathConstants
        Returns:
        xpath value that is converted to the return type
        Throws:
        ExecException - throw if the xpath can not be evaluated
      • createElement

        public static org.w3c.dom.Element createElement​(org.w3c.dom.Document document,
                                                        java.lang.String tagName,
                                                        java.lang.String[] attributes)
        Create element.
        Parameters:
        document - owner document
        tagName - element tag name
        attributes - child element attributes that is a list of name/value pairs: [name1, value2, name2, value2, ...]. Ignore if it is null or empty.
        Returns:
        created child element
        Since:
        6.0
      • createChildElement

        public static org.w3c.dom.Element createChildElement​(org.w3c.dom.Element elem,
                                                             java.lang.String childElemTagName,
                                                             java.lang.String[] attributes)
        Create and append the child element.
        Parameters:
        elem - parent element
        childElemTagName - child element tag name
        attributes - child element attributes that is a list of name/value pairs: [name1, value2, name2, value2, ...]. Ignore if it is null or empty.
        Returns:
        created child element
        Since:
        6.0
      • addAttributes

        public static void addAttributes​(org.w3c.dom.Element elem,
                                         java.lang.String[] attributes)
        Add the attributes to the element.
        Parameters:
        elem - an XML element
        attributes - a list of name/value pairs: [name1, value2, name2, value2, ...]
      • elementToString

        public static java.lang.String elementToString​(org.w3c.dom.Element element)
        Get the element as string including tagName and attributes and excluding descendants. It can be used for logging an element.
        Parameters:
        element - an xml element
        Returns:
        string value
        Since:
        6.0
      • visitElementRecursive

        public static boolean visitElementRecursive​(org.w3c.dom.Element element,
                                                    XmlUtil.ElementVisitor visitor)
                                             throws ExecException
        Visit element tree recursively in parent-first order, calling the visitor.
        Parameters:
        element - the root element
        visitor - element visitor
        Returns:
        true if stopping visiting the rest of the tree, return false to continue
        Throws:
        ExecException - throw if an error occurs
        Since:
        6.0