Package com.znize.platform.util
Class XmlUtil
- java.lang.Object
-
- com.znize.platform.util.XmlUtil
-
public class XmlUtil extends java.lang.ObjectXML utility- Since:
- 3.8.9
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceXmlUtil.ElementVisitorXML element visitor.
-
Constructor Summary
Constructors Constructor Description XmlUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddAttributes(org.w3c.dom.Element elem, java.lang.String[] attributes)Add the attributes to the element.static org.w3c.dom.ElementaddChildElementTextValue(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.ElementaddChildElementTextValue(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.ElementaddChildElementTextValue(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 booleancontainsChildElements(org.w3c.dom.Element element)Does the element contain any child elements?static org.w3c.dom.ElementcreateChildElement(org.w3c.dom.Element elem, java.lang.String childElemTagName, java.lang.String[] attributes)Create and append the child element.static org.w3c.dom.ElementcreateElement(org.w3c.dom.Document document, java.lang.String tagName, java.lang.String[] attributes)Create element.static org.w3c.dom.ElementcreateElementFromMap(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.StringelementToString(org.w3c.dom.Element element)Get the element as string including tagName and attributes and excluding descendants.static java.lang.ObjectevaluateXPath(org.w3c.dom.Document doc, java.lang.String xpath, javax.xml.namespace.QName returnType)Evaluate an XPath.static java.lang.BooleangetAttributeValueBoolean(org.w3c.dom.Element element, java.lang.String attrName, java.lang.Boolean defaultValue)Get attribute value converted to Boolean.static java.lang.ClassgetAttributeValueClass(org.w3c.dom.Element element, java.lang.String attrName, java.lang.Class defaultClass)Get attribute value as Class.static java.util.DategetAttributeValueDate(org.w3c.dom.Element element, java.lang.String attrName, java.util.Date defaultValue)Get attribute value converted to Date.static java.lang.IntegergetAttributeValueInteger(org.w3c.dom.Element element, java.lang.String attrName, java.lang.Integer defaultValue)Get attribute value converted to Integer.static java.lang.StringgetAttributeValueString(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.StringgetChildElementTextContent(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.DocumentBuildergetDocumentBuilder()Get DocumentBuilder.static javax.xml.parsers.DocumentBuildergetDocumentBuilder(boolean ignoreNamespace, boolean validate, boolean ignoreWhitespace)Get DocumentBuilder.static org.w3c.dom.ElementgetExactOneChildElement(org.w3c.dom.Element element)Get the child element of the specified element.static org.w3c.dom.ElementgetFirstChildElement(org.w3c.dom.Element element, java.lang.String childElementTagName)Get the first child element with the tag name.static booleanhasChildElements(org.w3c.dom.Element element)Whether the element has any child elements?static org.w3c.dom.Documentparse(java.io.File xmlFile)Parse the xml file.static org.w3c.dom.Documentparse(java.io.InputStream xmlInput)Parse the xml from input stream.static voidsetAttribute(org.w3c.dom.Element element, java.lang.String attrName, java.lang.String attrValue)Set the attribute of the XML element.static org.w3c.dom.ElementsetChildElementTextValue(org.w3c.dom.Element element, java.lang.String childElementTagName, java.lang.String textValue)Set child element text value.static org.w3c.dom.ElementsetChildElementTextValue(org.w3c.dom.Element element, java.lang.String childElementTagName, java.lang.String textValue, boolean cdata)Set child element text value.static booleanvisitElementRecursive(org.w3c.dom.Element element, XmlUtil.ElementVisitor visitor)Visit element tree recursively in parent-first order, calling the visitor.static voidwriteXmlDocument(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.StringxmlDocumentToString(org.w3c.dom.Node node, boolean indent, boolean omitXmlDeclaration)Get the string representation of the XML node including its subtree.static java.lang.StringxmlDocumentToString(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.DocumentxmlStringToDocument(java.lang.String xmlString)Parse the text to XML documentstatic org.w3c.dom.DocumentFragmentxmlStringToDocumentFragment(java.lang.String xmlFragment)Parse the string to DocumentFragment
-
-
-
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 namespacevalidate- whether to validate with DTDignoreWhitespace- whether to ignore whitespace- Returns:
- DocumentBuilder instance.
-
parse
public static org.w3c.dom.Document parse(java.io.File xmlFile) throws ExecExceptionParse 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 ExecExceptionParse 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 documentelementName- root element namemap- 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 ExecExceptionParse 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 ExecExceptionWrite the node including its descendant nodes to the writer.- Parameters:
node- the root node to writeindent- whether to indent child nodesomitXmlDeclaration- whether to omit xml declarationwriter- 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 ExecExceptionGet the string representation of the XML node including its subtree.- Parameters:
node- the root nodeindent- whether to indent child nodesomitXmlDeclaration- 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 ExecExceptionGet the string representation of the XML node including its subtree.- Parameters:
node- the root nodeindent- whether to indent child nodesomitXmlDeclaration- whether to omit XML declarationexcludeNodeItself- 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 ExecExceptionParse 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 elementattrName- attribute nameattrValue- 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 elementchildElementTagName- 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 elementchildElemTagName- 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 elementchildElementTagName- 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 elementchildElementTagName- child element tag nametextValue- 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 elementchildElementTagName- child element tag nametextValue- child element text contentcdata- 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 elementchildElementTagName- child element tag nametextValue- 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 elementchildElementTagName- child element tag nametextValue- the text content of the child element to be createdcdata- 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 elementchildElementTagName- child element tag nametextValue- the text content of the child element to be createdcdata- whether the text is a CDATAignoreIfNullValue- 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 elementattrName- attribute namedefaultValue- 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 elementattrName- attribute namedefaultValue- 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 elementattrName- attribute namedefaultValue- 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 elementattrName- attribute namedefaultValue- 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 elementattrName- attribute namedefaultClass- 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 elementtagNames- 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 ExecExceptionEvaluate an XPath.- Parameters:
doc- an XML documentxpath- XPath expressionreturnType- 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 documenttagName- element tag nameattributes- 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 elementchildElemTagName- child element tag nameattributes- 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 elementattributes- 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 ExecExceptionVisit element tree recursively in parent-first order, calling the visitor.- Parameters:
element- the root elementvisitor- 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
-
-