Class XmlUtils

java.lang.Object
com.avoka.core.util.XmlUtils

public class XmlUtils extends Object
Provides XML utility methods.
  • Field Details

  • Constructor Details

    • XmlUtils

      public XmlUtils()
  • Method Details

    • appendElmAndValue

      public 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. 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

      public static Element appendElmAndCDATAValue(Element parent, String childName, String childText)
      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

      public static String escapeAmpValues(String source)
      Return the escaped Amp XML for the source string
      Parameters:
      source - the source XML to be translated
      Returns:
      the escaped amp XML
    • toString

      public static String toString(Node node)
      Return the string representation of the given XML node.
      Parameters:
      node - the XML node
      Returns:
      the string representation of the given XML node
    • toStringNoWhitespace

      public static String toStringNoWhitespace(Node node)
      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

      public static String toString(NodeList nodeList, boolean format)
      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

      public static boolean isTextNode(Node node)
      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

      public static String toFormattedString(Node node)
      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

      public static String toDisplayFormat(String input)
      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 string
      aNamespaceAware - a flag that determines whether the name space is aware
      aValidating - a flag that determines whether the XML is validated
      Returns:
      a document built from the xml string
      See Also:
    • parseDocumentFromString

      public static Document parseDocumentFromString(String aXML)
      Return a XML Document from a xml string.
      Parameters:
      aXML - a XML string
      Returns:
      a document built from the xml string
    • parseDocumentFromString

      public static Document parseDocumentFromString(String aXML, ErrorHandler eh)
      Return a XML Document from a xml string with Error handler provided.
      Parameters:
      aXML - a XML string
      eh - a provided Error handler
      Returns:
      a document built from the xml string
    • buildDocument

      public static Document buildDocument(InputStream inputStream, EntityResolver entityResolver)
      Return a XML Document giving an input stream and entity resolver.
      Parameters:
      inputStream - the input stream for the document
      entityResolver - the entity resolver for the document
      Returns:
      the result XML Document
    • buildDocument

      public static Document buildDocument(InputStream inputStream)
      Return a XML Document giving an input stream.
      Parameters:
      inputStream - the input stream for the document
      Returns:
      the result XML Document
    • buildDocument

      public static Document buildDocument(byte[] data)
      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

      public static List<Element> getChildren(Element element)
      Return a list of child elements of a particular XML element
      Parameters:
      element - an XML element
      Returns:
      a list of child elements
    • getChildren

      public static List<Element> getChildren(Node element, String name)
      Return a list of children elements with the name match for a particular XML element.
      Parameters:
      element - an XML element
      name - children's name
      Returns:
      a list of children elements
    • getChild

      public static Element getChild(Node element, String name)
      Return the first child element with the name match for a particular XML element.
      Parameters:
      element - an XML element
      name - child's name
      Returns:
      the child element
    • getLastChild

      public static Element getLastChild(Node element, String name)
      Return the last child element with the name match for a particular XML element.
      Parameters:
      element - an XML element
      name - child's name
      Returns:
      the child element
    • getChildValue

      public static String getChildValue(Node parent, String childName)
      Return the child value for a particular XML element.
      Parameters:
      parent - an XML element
      childName - child's name
      Returns:
      the child value
    • getChildTextContext

      public static String getChildTextContext(Node parent, String childName)
      Return the full text context value of a child of a particular XML element.
      Parameters:
      parent - an XML element
      childName - child's name
      Returns:
      the child text context value
      Since:
      21.05
    • getChildBooleanValue

      public static boolean getChildBooleanValue(Element parent, String childName)
      Return the child value for a particular XML element containing a boolean value.
      Parameters:
      parent - an XML element
      childName - child's name
      Returns:
      the child boolean value as determined by Boolean.parseBoolean(String)
    • getChildIntegerValue

      public static Integer getChildIntegerValue(Element parent, String childName)
      Return the child value for a particular XML element containing an integer value.
      Parameters:
      parent - an XML element
      childName - child's name
      Returns:
      the child Integer value as determined by Integer.parseInt(String) or null if the value is not set
    • getChildLongValue

      public static Long getChildLongValue(Element parent, String childName)
      Return the child value for a particular XML element containing a value of type long.
      Parameters:
      parent - an XML element
      childName - child's name
      Returns:
      the child Long value as determined by Long.parseLong(String) or null if the value is not set
    • getChildDatetimeValue

      public static Date getChildDatetimeValue(Element parent, String childName) throws ParseException
      Return the child value for a particular XML element containing a date/time value.
      Parameters:
      parent - an XML element
      childName - 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

      public static String getXPathValue(Document document, String expression)
      Return the first document node text value for the given XPath expression using Jaxen library.
      Parameters:
      document - the XML document
      expression - the XPath expression
      Returns:
      the first document node value for the given XPath expression
    • evaluateXPath

      public static String evaluateXPath(Document document, String expression)
      Evaluate the XPath expression and return the given string value.
      Parameters:
      document - the XML document
      expression - the XPath expression
      Returns:
      the first document node value for the given XPath expression
      Since:
      4.0.0
    • getXPathElement

      public static Element getXPathElement(Document document, String expression)
      Return the first document node value for the given XPath expression.
      Parameters:
      document - the XML document
      expression - the XPath expression
      Returns:
      the first document node value for the given XPath expression
    • getXPathValueList

      public static List<String> getXPathValueList(Document document, String expression)
      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

      public static List<Node> getXPathNodeList(Document document, String expression)
      Return the list of nodes for the given document and XPath expression.
      Parameters:
      document - the XML document
      expression - the XPath expression
      Returns:
      the nodes for the given XPath expression
    • getNodeXPathValue

      public static String getNodeXPathValue(Node node, String expression)
      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

      public static List<String> getNodeXPathValueList(Node node, String expression)
      Return the list of nodes for the given node and XPath expression.
      Parameters:
      node - the XML node
      expression - the XPath expression
      Returns:
      the node value list for the given XPath expression
      Since:
      5.0.0
    • getNodeXPathNodeList

      public static List<Node> getNodeXPathNodeList(Node node, String expression)
      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

      public static Element getOrCreateChild(Element parent, String childName)
      Get or create the child element.
      Parameters:
      parent - the parent element
      childName - child's name
      Returns:
      the child element if exists, otherwise create the child and return it
    • setChildElementValue

      public static void setChildElementValue(Element parent, String childName, String value)
      Set the child element value given the parent, child name and value.
      Parameters:
      parent - the parent element
      childName - child's name
      value - child value
    • removeNodeChildren

      public static boolean removeNodeChildren(Document document, String xpath)
      Remove the child nodes from the XML Document element specified by the XPath.
      Parameters:
      document - the document to remove the nodes from
      xpath - 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

      public static Document copyDocument(Document document)
      Create a copy of the given document.
      Parameters:
      document - the document to copy
      Returns:
      the new copied document
      Since:
      4.1.0
    • createChildElement

      public static Element createChildElement(Element parent, String childName)
      Create the child element.
      Parameters:
      parent - parent element
      childName - child name
      Returns:
      the created element
    • createChildElementWithValue

      public static void createChildElementWithValue(Element parent, String childName, Object value)
      Create a child element with specified value.
      Parameters:
      parent - the parent element
      childName - the child name
      value - the child value
    • escapeValues

      public static void escapeValues(Node n)
      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 (see JAXBContext.newInstance(String)
      elementToMarshal - the object to marshal
      createFragment - whether to generate an XML fragment or a full document (see Marshaller.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

      public static NodeList getNodeList(Node node, String xpath)
      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

      public static Map<String,String> getXmlDataMap(Document document, String jsonMap)
      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

      public static String stripUnparsableCharacters(String xmlString)
      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

      public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException
      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

      public static Document safeParseXmlData(String aXML)
      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

      public static Document safeParseXmlData(String aXML, boolean namespaceAware)
      Safely parse the given XML text into a Document object.
      Parameters:
      aXML - the XML text
      namespaceAware - specify whether the namespace aware
      Returns:
      parsed XML Document object
      Since:
      5.0.0
    • getNodeAsDocument

      public static Document getNodeAsDocument(Document document, String tagName)
      Return the content of the child as a XML Document object, or null if not defined
      Parameters:
      document - the containing Document
      tagName - the name (String) of the child node.
      Returns:
      the Document object, or null if not defined.