Package com.avoka.core.util
Class XmlUtils
java.lang.Object
com.avoka.core.util.XmlUtils
Provides XML utility methods.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Element
appendElmAndCDATAValue
(Element parent, String childName, String childText) Append a new element to the parent with the given child name and child text value.static Element
appendElmAndValue
(Element parent, String childName, String value) Append a new element to the parent with the given child name and child text value.static Document
buildDocument
(byte[] data) Return a XML Document giving an UTF-8 encoded byte array.static Document
buildDocument
(InputStream inputStream) Return a XML Document giving an input stream.static Document
buildDocument
(InputStream inputStream, EntityResolver entityResolver) Return a XML Document giving an input stream and entity resolver.static Document
copyDocument
(Document document) Create a copy of the given document.static Element
createChildElement
(Element parent, String childName) Create the child element.static void
createChildElementWithValue
(Element parent, String childName, Object value) Create a child element with specified value.static String
escapeAmpValues
(String source) Return the escaped Amp XML for the source stringstatic void
escapeValues
(Node n) Escape all values for a Nodestatic String
evaluateXPath
(Document document, String expression) Evaluate the XPath expression and return the given string value.static Element
Return the first child element with the name match for a particular XML element.static boolean
getChildBooleanValue
(Element parent, String childName) Return the child value for a particular XML element containing a boolean value.static Date
getChildDatetimeValue
(Element parent, String childName) Return the child value for a particular XML element containing a date/time value.static Integer
getChildIntegerValue
(Element parent, String childName) Return the child value for a particular XML element containing an integer value.static Long
getChildLongValue
(Element parent, String childName) Return the child value for a particular XML element containing a value of type long.getChildren
(Element element) Return a list of child elements of a particular XML elementgetChildren
(Node element, String name) Return a list of children elements with the name match for a particular XML element.static String
getChildTextContext
(Node parent, String childName) Return the full text context value of a child of a particular XML element.static String
getChildValue
(Node parent, String childName) Return the child value for a particular XML element.static Element
getLastChild
(Node element, String name) Return the last child element with the name match for a particular XML element.static Document
getNodeAsDocument
(Document document, String tagName) Return the content of the child as a XML Document object, or null if not definedstatic NodeList
getNodeList
(Node node, String xpath) Get the W3C NodeList instance associated with the XPath selection supplied.getNodeXPathNodeList
(Node node, String expression) Return the list of nodes for the given node and XPath expression.static String
getNodeXPathValue
(Node node, String expression) Return the first node text value for the given XPath expression using Jaxen library.getNodeXPathValueList
(Node node, String expression) Return the list of nodes for the given node and XPath expression.static Element
getOrCreateChild
(Element parent, String childName) Get or create the child element.getXmlDataMap
(Document document, String jsonMap) Return a map of XML data element values from the given document using the jsonMap definition.static Element
getXPathElement
(Document document, String expression) Return the first document node value for the given XPath expression.getXPathNodeList
(Document document, String expression) Return the list of nodes for the given document and XPath expression.static String
getXPathValue
(Document document, String expression) Return the first document node text value for the given XPath expression using Jaxen library.getXPathValueList
(Document document, String expression) Return the list of nodes for the given document and XPath expression.static boolean
isTextNode
(Node node) Return whether the supplied W3C DOM Node is a text node.static boolean
isValidDocument
(byte[] data) Return true if UTF-8 encoded data is a valid XML Document.static String
marshalToXml
(String packageName, Object elementToMarshal, boolean createFragment) Marshals a JAXB object to an XML stringstatic DocumentBuilder
Create a new DocumentBuilder instance without namespace aware configured, or validation configured, and with the system security policies configured.static Document
Return a XML Document from a xml string.static Document
parseDocumentFromString
(String aXML, boolean aNamespaceAware, boolean aValidating) Return a XML Document from a xml string.static Document
parseDocumentFromString
(String aXML, ErrorHandler eh) Return a XML Document from a xml string with Error handler provided.static boolean
removeNodeChildren
(Document document, String xpath) Remove the child nodes from the XML Document element specified by the XPath.static Document
safeParseXmlData
(String aXML) Safely parse the given XML text into a Document object.static Document
safeParseXmlData
(String aXML, boolean namespaceAware) Safely parse the given XML text into a Document object.static void
setChildElementValue
(Element parent, String childName, String value) Set the child element value given the parent, child name and value.static String
stripUnparsableCharacters
(String xmlString) Strip characters that would later fail XML parsing from the given string.static String
toDisplayFormat
(String input) Return the display formatted string representation of the XML string.static String
toFormattedString
(Node node) Return the formatted string representation of the given XML node.static String
Return the string representation of the given XML node.static String
Serialise the supplied W3C DOM subtree.static String
toStringNoWhitespace
(Node node) Return the string representation of the given XML node with no whitespace.
-
Field Details
-
XML_PROLOG
The XML version 1.0 UTF-8 prolog.- See Also:
-
-
Constructor Details
-
XmlUtils
public XmlUtils()
-
-
Method Details
-
appendElmAndValue
Append a new element to the parent with the given child name and child text value. This method will escape any XML characters, but if the childText starts with a URL it will- Parameters:
parent
- the parent element (required)childName
- the child element name (required)value
- the child element text content- Returns:
- newly created child element
- Since:
- 4.2.0
-
appendElmAndCDATAValue
Append a new element to the parent with the given child name and child text value.- Parameters:
parent
- the parent element (required)childName
- the child element name (required)childText
- the child element text content- Returns:
- newly created child element
- Since:
- 4.2.0
-
escapeAmpValues
Return the escaped Amp XML for the source string- Parameters:
source
- the source XML to be translated- Returns:
- the escaped amp XML
-
toString
Return the string representation of the given XML node.- Parameters:
node
- the XML node- Returns:
- the string representation of the given XML node
-
toStringNoWhitespace
Return the string representation of the given XML node with no whitespace.- Parameters:
node
- the XML node- Returns:
- the string representation of the given XML node with no whitespace
-
toString
Serialise the supplied W3C DOM subtree.- Parameters:
nodeList
- The DOM subtree as a NodeList.format
- Format the output.- Returns:
- the string representation for the node.
- Throws:
DOMException
- Unable to serialise the DOM.
-
isTextNode
Return whether the supplied W3C DOM Node is a text node.- Parameters:
node
- The node to be tested- Returns:
- true if the node is a text node, otherwise false
-
toFormattedString
Return the formatted string representation of the given XML node.- Parameters:
node
- the XML node- Returns:
- the string representation of the given XML node
-
toDisplayFormat
Return the display formatted string representation of the XML string.- Parameters:
input
- the input XML- Returns:
- the string representation of the given XML node
- Since:
- 3.6.0
-
parseDocumentFromString
public static Document parseDocumentFromString(String aXML, boolean aNamespaceAware, boolean aValidating) Return a XML Document from a xml string.- Parameters:
aXML
- a xml stringaNamespaceAware
- a flag that determines whether the name space is awareaValidating
- a flag that determines whether the XML is validated- Returns:
- a document built from the xml string
- See Also:
-
parseDocumentFromString
Return a XML Document from a xml string.- Parameters:
aXML
- a XML string- Returns:
- a document built from the xml string
-
parseDocumentFromString
Return a XML Document from a xml string with Error handler provided.- Parameters:
aXML
- a XML stringeh
- a provided Error handler- Returns:
- a document built from the xml string
-
buildDocument
Return a XML Document giving an input stream and entity resolver.- Parameters:
inputStream
- the input stream for the documententityResolver
- the entity resolver for the document- Returns:
- the result XML Document
-
buildDocument
Return a XML Document giving an input stream.- Parameters:
inputStream
- the input stream for the document- Returns:
- the result XML Document
-
buildDocument
Return a XML Document giving an UTF-8 encoded byte array.- Parameters:
data
- the an UTF-8 encoded byte array.- Returns:
- the result XML Document
- Since:
- 4.2.0
-
isValidDocument
public static boolean isValidDocument(byte[] data) Return true if UTF-8 encoded data is a valid XML Document.- Parameters:
data
- the an UTF-8 encoded byte array to test- Returns:
- true if a valid XML Document
- Since:
- 17.10.4
-
getChildren
Return a list of child elements of a particular XML element- Parameters:
element
- an XML element- Returns:
- a list of child elements
-
getChildren
Return a list of children elements with the name match for a particular XML element.- Parameters:
element
- an XML elementname
- children's name- Returns:
- a list of children elements
-
getChild
Return the first child element with the name match for a particular XML element.- Parameters:
element
- an XML elementname
- child's name- Returns:
- the child element
-
getLastChild
Return the last child element with the name match for a particular XML element.- Parameters:
element
- an XML elementname
- child's name- Returns:
- the child element
-
getChildValue
Return the child value for a particular XML element.- Parameters:
parent
- an XML elementchildName
- child's name- Returns:
- the child value
-
getChildTextContext
Return the full text context value of a child of a particular XML element.- Parameters:
parent
- an XML elementchildName
- child's name- Returns:
- the child text context value
- Since:
- 21.05
-
getChildBooleanValue
Return the child value for a particular XML element containing a boolean value.- Parameters:
parent
- an XML elementchildName
- child's name- Returns:
- the child boolean value as determined by
Boolean.parseBoolean(String)
-
getChildIntegerValue
Return the child value for a particular XML element containing an integer value.- Parameters:
parent
- an XML elementchildName
- child's name- Returns:
- the child Integer value as determined by
Integer.parseInt(String)
or null if the value is not set
-
getChildLongValue
Return the child value for a particular XML element containing a value of type long.- Parameters:
parent
- an XML elementchildName
- child's name- Returns:
- the child Long value as determined by
Long.parseLong(String)
or null if the value is not set
-
getChildDatetimeValue
Return the child value for a particular XML element containing a date/time value.- Parameters:
parent
- an XML elementchildName
- child's name- Returns:
- the child date value, or null if the value is not set
- Throws:
ParseException
- if the value could not be parsed as a date time value
-
getXPathValue
Return the first document node text value for the given XPath expression using Jaxen library.- Parameters:
document
- the XML documentexpression
- the XPath expression- Returns:
- the first document node value for the given XPath expression
-
evaluateXPath
Evaluate the XPath expression and return the given string value.- Parameters:
document
- the XML documentexpression
- the XPath expression- Returns:
- the first document node value for the given XPath expression
- Since:
- 4.0.0
-
getXPathElement
Return the first document node value for the given XPath expression.- Parameters:
document
- the XML documentexpression
- the XPath expression- Returns:
- the first document node value for the given XPath expression
-
getXPathValueList
Return the list of nodes for the given document and XPath expression.- Parameters:
document
- the XML document (required)expression
- the XPath expression (required)- Returns:
- the node value list for the given XPath expression
- Since:
- 4.1.0
-
getXPathNodeList
Return the list of nodes for the given document and XPath expression.- Parameters:
document
- the XML documentexpression
- the XPath expression- Returns:
- the nodes for the given XPath expression
-
getNodeXPathValue
Return the first node text value for the given XPath expression using Jaxen library.- Parameters:
node
- the XML node (required)expression
- the XPath expression (required)- Returns:
- the first document node value for the given XPath expression
- Since:
- 5.0.0
-
getNodeXPathValueList
Return the list of nodes for the given node and XPath expression.- Parameters:
node
- the XML nodeexpression
- the XPath expression- Returns:
- the node value list for the given XPath expression
- Since:
- 5.0.0
-
getNodeXPathNodeList
Return the list of nodes for the given node and XPath expression.- Parameters:
node
- the XML node (required)expression
- the XPath expression (required)- Returns:
- the nodes for the given XPath expression
- Since:
- 5.0.0
-
getOrCreateChild
Get or create the child element.- Parameters:
parent
- the parent elementchildName
- child's name- Returns:
- the child element if exists, otherwise create the child and return it
-
setChildElementValue
Set the child element value given the parent, child name and value.- Parameters:
parent
- the parent elementchildName
- child's namevalue
- child value
-
removeNodeChildren
Remove the child nodes from the XML Document element specified by the XPath.- Parameters:
document
- the document to remove the nodes fromxpath
- the XPath of the parent element- Returns:
- true if a one or more nodes were removed from the XML Document object
- Since:
- 4.1.5
-
copyDocument
Create a copy of the given document.- Parameters:
document
- the document to copy- Returns:
- the new copied document
- Since:
- 4.1.0
-
createChildElement
Create the child element.- Parameters:
parent
- parent elementchildName
- child name- Returns:
- the created element
-
createChildElementWithValue
Create a child element with specified value.- Parameters:
parent
- the parent elementchildName
- the child namevalue
- the child value
-
escapeValues
Escape all values for a Node- Parameters:
n
- a node
-
marshalToXml
public static String marshalToXml(String packageName, Object elementToMarshal, boolean createFragment) throws jakarta.xml.bind.JAXBException Marshals a JAXB object to an XML string- Parameters:
packageName
- a colon separated list of package names containing the object factory and classes related to the object being marshalled (seeJAXBContext.newInstance(String)
elementToMarshal
- the object to marshalcreateFragment
- whether to generate an XML fragment or a full document (seeMarshaller.JAXB_FRAGMENT
)- Returns:
- a formatted XML string containing a representation of the object and its contents
- Throws:
jakarta.xml.bind.JAXBException
- if a marshalling exception occurs
-
getNodeList
Get the W3C NodeList instance associated with the XPath selection supplied.- Parameters:
node
- The document node to be searched.xpath
- The XPath String to be used in the selection.- Returns:
- The W3C NodeList instance at the specified location in the document, or null.
-
getXmlDataMap
Return a map of XML data element values from the given document using the jsonMap definition. Note any values which are not XSS safe will be excluded from the map.{ "firstName": "//SmartForm/FormData/Contact/FirstName", "lastName": "//SmartForm/FormData/Contact/LastName", "email": "//SmartForm/FormData/Contact/Email" }
- Parameters:
document
- the XML document (required)jsonMap
- the JSON format name to XPath mapping definition (required)- Returns:
- a map of XML data element values from the given document using the jsonMap definition
- Since:
- 4.0.0
-
stripUnparsableCharacters
Strip characters that would later fail XML parsing from the given string. Note: Character in Java is defined as two bytes. This is not enough for some modern encodings (e.g. UTF-8 uses 1-4 bytes per character), and so Java offers code points (= one or more Java characters in a String representing a single character, e.g. a 3 byte character in UTF-8). Therefore we cannot safely operate over the characters in the string but must use codepoints instead. This is also being done by the Xerces parser code. If we did use characters instead, we might not strip out multi-character codepoints that later cause errors, and we might also inadvertently strip out "invalid" characters that are really part of a multi-character codepoint.- Parameters:
xmlString
- the string to be stripped- Returns:
- a new string containing only codepoints that are considered to be valid by the XML parser
- Since:
- 4.1.0
-
newDocumentBuilder
Create a new DocumentBuilder instance without namespace aware configured, or validation configured, and with the system security policies configured.- Returns:
- a new DocumentBuilder instance
- Throws:
ParserConfigurationException
- if an error occurs initializing the parser- Since:
- 4.2.0
-
safeParseXmlData
Safely parse the given XML text into a Document object.- Parameters:
aXML
- the XML text- Returns:
- parsed XML Document object
- Since:
- 4.1.15
-
safeParseXmlData
Safely parse the given XML text into a Document object.- Parameters:
aXML
- the XML textnamespaceAware
- specify whether the namespace aware- Returns:
- parsed XML Document object
- Since:
- 5.0.0
-
getNodeAsDocument
Return the content of the child as a XML Document object, or null if not defined- Parameters:
document
- the containing DocumenttagName
- the name (String) of the child node.- Returns:
- the Document object, or null if not defined.
-