Class XmlDoc


  • public class XmlDoc
    extends java.lang.Object

    Provides XML Document class for use in Groovy scripts. An XmlDoc object wraps an underlying XML Document object and provides convenience methods for performing access and modification operations.

    Examples

    Please find the XML Document examples below.

    XML Read Example

    This Groovy example creates an XmlDoc from a delivery formXml string value, and then reads out a values and puts them in a map.

     import com.avoka.tm.util.*
    
     XmlDoc xmlDoc = new XmlDoc(formXml)
    
     Map contact = [:]
     contact.firstName = xmlDoc.getText('//YourDetails/FirstName')
     contact.lastName = xmlDoc.getText('//YourDetails/LastName')
     contact.email = xmlDoc.getText('//YourDetails/Email')
     contact.state = xmlDoc.getText('//Location/State') 

    Please see the W3C Schools XPath Syntax for more examples.

    XML Write Example

    This Groovy example writes form prefill data into a formXml XML Document.

     import com.avoka.tm.util.*
    
     XmlDoc xmlDoc = new XmlDoc(formXml)
    
     def userDetails = ...
    
     xmlDoc.setText('/AvokaSmartForm/YourDetails/FirstName', userDetails.firstName)
     xmlDoc.setText('/AvokaSmartForm/YourDetails/LastName', userDetails.lastName)
     xmlDoc.setText('/AvokaSmartForm/YourDetails/Email', userDetails.email) 

    You can also add Nodes or a list of Nodes to an XML Document using the addNode(String, Node) or addNodeList(String, List) methods. See the Composer Cascading DD List Example later to see this in action.

    Adding XML Content as Text Example

    When creating large amounts of XML it is often easer to use string templating techniques to create the XML content and then add this to the target XML document.

    The example below is using the addContent(String, String, String) method to add the '/ItemList/Item' elements from the source prefillDataContext XML text to the xmlDoc object under the node '/AvokaSmartForm/Accounts'.

     import com.avoka.tm.util.*
    
     // Text prefill XML content
     String prefillDataContent = '<ItemList> <Item>A1</Item> <Item>B2</Item> </ItemList>'
    
     // Target document to add prefill content to
     XmlDoc xmlDoc = new XmlDoc(formXml)
     xmlDoc.addContent(prefillDataContent, '/ItemList/Item', '/AvokaSmartForm/Accounts') 

    If you need to replace existing content in an XML Document rather than adding to it, then use the removeNodes(String) method first before adding the content. For example:

     import com.avoka.tm.util.*
    
     String prefillDataContent = '<ItemList> <Item>A1</Item> <Item>B2</Item> </ItemList>'
    
     XmlDoc xmlDoc = new XmlDoc(formXml)
    
     // Remove any existing data
     xmlDoc.removeNodes('/AvokaSmartForm/Accounts')
    
     xmlDoc.addContent(prefillDataContent, '/ItemList/Item', '/AvokaSmartForm/Accounts') 

    See http://en.wikibooks.org/wiki/XQuery for information on how to use XQuery.

    Since:
    5.0.0
    • Constructor Summary

      Constructors 
      Constructor Description
      XmlDoc​(java.lang.Object source)
      Create an XmlDoc object from the given XML source, either String, Document, InputStream or byte[].
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.w3c.dom.Node addContent​(java.lang.String sourceContent, java.lang.String sourceXPath, java.lang.String targetXPath)
      Add the given XML content from the source XPath to this XML Document object at the specified target XPath.
      org.w3c.dom.Node addNode​(java.lang.String xpath, org.w3c.dom.Node node)
      Add the given Node to the XML Document object at to specified XPath.
      void addNodeList​(java.lang.String xpath, java.util.List<org.w3c.dom.Node> nodeList)
      Add the given Node list to the XML Document object at to specified XPath.
      java.lang.String evalXPath​(java.lang.String xpath)
      Evaluate the XPath expression and return the given string value.
      java.util.Map<java.lang.String,​java.lang.String> getDataMap​(java.lang.String jsonMap)
      Return a map of XML data element values from the document using the given jsonMap definition.
      org.w3c.dom.Document getDocument()
      Return the underlying XML Document object.
      org.w3c.dom.Node getNode​(java.lang.String xpath)
      Return the XML node for the given XPath.
      java.util.List<org.w3c.dom.Node> getNodeList​(java.lang.String xpath)
      Return the list of XML Nodes for the given XPath expression.
      java.lang.String getText​(java.lang.String xpath)
      Return the text value for the given XPath or null if not found.
      boolean removeNodeChildren​(java.lang.String xpath)
      Remove the child nodes from the XML Document element specified by the XPath.
      boolean removeNodes​(java.lang.String xpath)
      Remove the nodes from the XML Document object specified by the XPath.
      org.w3c.dom.Node setNode​(java.lang.String xpath)
      Create the XML elements specified by the XPath expression if they don't exist, and return the leaf node.
      org.w3c.dom.Node setText​(java.lang.String xpath, java.lang.String textContent)
      Set the content text on the XML element specified by the XPath expression.
      java.lang.String toFormattedString()
      Return the formatted string representation of the Document.
      java.lang.String toString()
      Return the string representation of the Document.
      • Methods inherited from class java.lang.Object

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

      • XmlDoc

        public XmlDoc​(java.lang.Object source)
        Create an XmlDoc object from the given XML source, either String, Document, InputStream or byte[].
        Parameters:
        source - the source XML, either String, Document, InputStream or byte[].
    • Method Detail

      • addContent

        public org.w3c.dom.Node addContent​(java.lang.String sourceContent,
                                           java.lang.String sourceXPath,
                                           java.lang.String targetXPath)
        Add the given XML content from the source XPath to this XML Document object at the specified target XPath.
        Parameters:
        sourceContent - the source XML content to add to the XML Document
        sourceXPath - the XPath to the node within the source XML content to add
        targetXPath - the XPath to add the content to in this XML Document
        Returns:
        the Node of the target XPath
      • addNode

        public org.w3c.dom.Node addNode​(java.lang.String xpath,
                                        org.w3c.dom.Node node)
        Add the given Node to the XML Document object at to specified XPath. If the XPath nodes do not exist in the document they will be created. This method will return a deep cloned copy of the given Node which was added to the XML Document at the specified path. XPath expressions must use an absolute path to avoid ambiguity when creating XML path elements, using // search criteria is not permitted.
        Parameters:
        xpath - the XPath location of where to add the node
        node - the Node to add to the XML Document
        Returns:
        the cloned node added to the XML Document at the specified XPath.
      • addNodeList

        public void addNodeList​(java.lang.String xpath,
                                java.util.List<org.w3c.dom.Node> nodeList)
        Add the given Node list to the XML Document object at to specified XPath. If the XPath nodes do not exist in the document they will be created. This method will return a deep cloned copy of the given list of Node which was added to the XML Document at the specified path. XPath expressions must use an absolute path to avoid ambiguity when creating XML path elements, using // search criteria is not permitted.
        Parameters:
        xpath - the XPath location of where to add the nodes
        nodeList - the list of Nodes to add to the XML Document
      • getDocument

        public org.w3c.dom.Document getDocument()
        Return the underlying XML Document object.
        Returns:
        the underlying XML Document object.
      • getNode

        public org.w3c.dom.Node getNode​(java.lang.String xpath)
        Return the XML node for the given XPath.
        Parameters:
        xpath - the XPath expression of the node
        Returns:
        the node for the given xpath
      • getNodeList

        public java.util.List<org.w3c.dom.Node> getNodeList​(java.lang.String xpath)
        Return the list of XML Nodes for the given XPath expression.
        Parameters:
        xpath - the XPath expression
        Returns:
        the list of XML Nodes for the given XPath expression
      • getText

        public java.lang.String getText​(java.lang.String xpath)
        Return the text value for the given XPath or null if not found.
         def xmlDoc = new com.avoka.component.xml.XmlDoc(submissionXml)
        
         def contact = [:]
         contact.firstName = xmlDoc.getText('//YourDetails/FirstName')
         contact.lastName = xmlDoc.getText('//YourDetails/LastName')
         contact.email = xmlDoc.getText('//YourDetails/Email')
         contact.state = xmlDoc.getText('//Location/State') 
        Parameters:
        xpath - the XPath to search
        Returns:
        the text value for the given XPath or null if not found
      • setNode

        public org.w3c.dom.Node setNode​(java.lang.String xpath)

        Create the XML elements specified by the XPath expression if they don't exist, and return the leaf node.

        XPath expressions must use an absolute path to avoid ambiguity when creating XML path elements, using // search criteria is not permitted.

         def line1 = xmlDoc.set('/AvokaSmartForm/YourDetails/Address/Line1") 
        Parameters:
        xpath - the XML elements to create
        Returns:
        the node specified by the XPath if found or the created XML node
      • setText

        public org.w3c.dom.Node setText​(java.lang.String xpath,
                                        java.lang.String textContent)

        Set the content text on the XML element specified by the XPath expression. This method will create the elements specified by the path if they don't exist.

        XPath expressions must use an absolute path to avoid ambiguity when creating XML path elements, using // search criteria is not permitted.

         xmlDoc.setText('/AvokaSmartForm/YourDetails/Address/Line1", "30 George Street") 

        Example

         def xmlDoc = new com.avoka.component.xml.XmlDoc(schemaSeed)
        
         def userDetails = ...
        
         xmlDoc.setText('/AvokaSmartForm/YourDetails/FirstName', userDetails.firstName)
         xmlDoc.setText('/AvokaSmartForm/YourDetails/LastName', userDetails.lastName)
         xmlDoc.setText('/AvokaSmartForm/YourDetails/Email', userDetails.email) 
        Parameters:
        xpath - the XML element to set the text on, this path will be created if it does not exist
        textContent - the element content text value to set
        Returns:
        the node specified by the XPath if found or the created XML node
      • evalXPath

        public java.lang.String evalXPath​(java.lang.String xpath)
        Evaluate the XPath expression and return the given string value.

        Example

         def xmlDoc = new XmlDoc(schemaSeed)
        
         def fullname = xmlDoc.evalXPath("concat(//FirstName, ' ',//LastName)") 
        Parameters:
        xpath - the XPath expression (required)
        Returns:
        the evaluated XPath expression as a string value
        Since:
        4.0.0
      • removeNodes

        public boolean removeNodes​(java.lang.String xpath)
        Remove the nodes from the XML Document object specified by the XPath.
        Parameters:
        xpath - the XPath of the nodes to remove
        Returns:
        true if a one or more nodes were removed from the XML Document object
      • removeNodeChildren

        public boolean removeNodeChildren​(java.lang.String xpath)
        Remove the child nodes from the XML Document element specified by the XPath.
        Parameters:
        xpath - the XPath of the parent node whose children should be removed (required)
        Returns:
        true if a one or more nodes were removed from the XML Document object
      • getDataMap

        public java.util.Map<java.lang.String,​java.lang.String> getDataMap​(java.lang.String jsonMap)
        Return a map of XML data element values from the document using the given jsonMap definition.

        Example JSON mapping format:

         {
            "firstName": "//SmartForm/FormData/Contact/FirstName",
            "lastName": "//SmartForm/FormData/Contact/LastName",
            "email": "//SmartForm/FormData/Contact/Email"
         }
         
        This method provides a convenience wrapper around XmlUtils.getXmlDataMap(Document, String).
        Parameters:
        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
      • toFormattedString

        public java.lang.String toFormattedString()
        Return the formatted string representation of the Document.
        Returns:
        the formatted string representation of the Document
      • toString

        public java.lang.String toString()
        Return the string representation of the Document.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the string representation of the Document