How to Manipulate XML Data using Groovy

Document created by Adam Arrowsmith Employee on Oct 26, 2011
Version 1Show Document
  • View in full screen mode
You want to arbitrarily parse and/or manipulate your Document's XML structure to simplify processing later in the Process. For example, perhaps you want to remove or rearrange certain sections of the original XML data to make the data more congruent with AtomSphere mapping and looping rules.
Use a Data Process step with Custom Scripting. Replace the sample script with the code below and modify to suit your needs. Boomi uses the JDOM XML library. For a full list of available objects, methods, and properties, please see http://www.jdom.org/docs/apidocs/. It may be helpful to do some additional research on working with XML in Java; here's a good tutorial to start: http://www.ibm.com/developerworks/java/library/j-jdom.

Also check out http://www.w3schools.com for XML and XPath reference.


Code:

// This script manipulates an XML Document's structure.
     
     import org.jdom.input.SAXBuilder;
     import org.jdom.Document;
     import org.jdom.Element;
     import org.jdom.xpath.XPath;
     import org.jdom.output.XMLOutputter;
     
     // Loop through the Process Documents
     for ( int i = 0; i < dataContext.getDataCount(); i++ ) {
     
          InputStream is = dataContext.getStream(i);
          Properties props = dataContext.getProperties(i);
     
          // Build XML Document
          SAXBuilder builder = new SAXBuilder();
          Document doc = builder.build(is);
     
          // Here's how to grab values from the current data
     
          // Create an XPath statement to search for the element or elements you care about:
          XPath x;
          x = XPath.newInstance("//MyElement");
     
          // Select a single node and grab the actual value
          eleMyElement = x.selectSingleNode(doc);
     
          if (eleMyElement != null) {
              myValue = eleMyElement.getText();
          }
     
     
          // OR multiple nodes (if multiple nodes you will need to loop through them)
          myElements = x.selectNodes(doc);
     
          for (Element myElement : myElements) {
             ...
          }
     
     
          // Here's how to change the value of an existing element
     
          // First, find the element using XPath as illustrated above, then just the value:
          myElement.setText("new value");
     
     
     
          // Here's how to create a brand new element
     
          // First, create the new element object with the value...
          myNewElement = new Element("MyElement").addContent("new value");
     
          // ...Then grab the appropriate parent element to attach your new element to:
          x = XPath.newInstance("//MyParent");
          eleParentElement = x.selectSingleNode(doc);
     
          if (eleParentElement != null) {
              eleParentElement.addContent(myNewElement);
          }
     
     
          // When you're finished manipulating the XML data, you'll need to output the Document's data to an InputStream to pass to the next step in your Process work flow.
     
          XMLOutputter outputter = new XMLOutputter();
          is = new ByteArrayInputStream(outputter.outputString(doc).getBytes("UTF-8"));
     
          dataContext.storeStream(is, props);
     
     }

1 person found this helpful

Attachments

    Outcomes