Use the XPath API to achieve this. Convert the doc to an XML string and then use an xpath to get the value for a particular field in the xml.
Inputs:
xpathExpression
xmlString
Outputs:
fieldValue
error
Imports:
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
Java Service
IDataCursor pipelineCursor = pipeline.getCursor();
String xpathExpression = IDataUtil.getString( pipelineCursor, "xpathExpression" );
String xmlString = IDataUtil.getString( pipelineCursor, "xmlString" );
pipelineCursor.destroy();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
try {
InputStream is=new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(is);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr= xpath.compile(xpathExpression);
//refine this part of the code
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
IDataUtil.put(pipelineCursor, "fieldValue", nodes.item(i).getNodeValue());
}
//end
} catch (ParserConfigurationException e) {
e.printStackTrace();
IDataUtil.put(pipelineCursor, "error", e);
} catch (SAXException e) {
e.printStackTrace();
IDataUtil.put(pipelineCursor, "error", e);
} catch (IOException e) {
e.printStackTrace();
IDataUtil.put(pipelineCursor, "error", e);
} catch (XPathExpressionException e) {
e.printStackTrace();
IDataUtil.put(pipelineCursor, "error", e);
}
pipelineCursor.destroy();
Sample input:
/inputDoc/infoDoc/field1/text()
<?xml version="1.0"?>
<inputDoc>
<infoDoc>
<field1>abc</field1>
<field2>123</field2>
</infoDoc>
</inputDoc>
Output:
abc
#webMethods#Integration-Server-and-ESB#Flow-and-Java-services