@ComponentProfile(summary="Extract data via XPath and store it", tag="service,xml") public class XPathService extends ServiceImp
This service allows you to configure an xpath expression which will be executed on source xml, the result of which can be saved to multiple locations.
To specify where the source xml, source xpath expression and the result of the xpath execution should be saved, you shoud use
DataInputParameter
or DataOutputParameter
.
For example you can specify the source xml can be found in the AdaptrisMessage
payload, by using
StringPayloadDataInputParameter
like this :
<xpath-service>
<xml-source class="string-payload-data-input-parameter"/>
...
And perhaps the source xpath expression will be configured directly in Interlok config, using
ConstantDataInputParameter
;
<xpath-service>
<xpath-execution>
<source class="constant-data-input-parameter">
<value>//my/xpath/expression</value>
</source>
...
And then maybe the result of the xpath execution is to be saved in AdaptrisMessage
metadata, using
MetadataDataOutputParameter
;
<xpath-service>
<xpath-execution>
<target class="metadata-data-output-parameter">
<metadata-key>targetMetadataKey</metadata-key>
</target>
...
While you may only specify a single source xml destination, you may if you wish apply multiple XPath expressions, each of which saves the result to a different location. To do this, simply configure multiple executions. Take the following example, where we specify the payload containing the source xml and 3 XPath expressions will be executed each of which will store the result in 3 different metadata items;
<xpath-service>
<xml-source class="string-payload-data-input-parameter"/>
<xpath-execution>
<source class="constant-data-input-parameter">
<value>//my/first/xpath/expression</value>
</source>
<target class="metadata-data-output-parameter">
<metadata-key>targetMetadataKey1</metadata-key>
</target>
</xpath-execution>
<xpath-execution>
<source class="constant-data-input-parameter">
<value>//my/second/xpath/expression</value>
</source>
<target class="metadata-data-output-parameter">
<metadata-key>targetMetadataKey2</metadata-key>
</target>
</xpath-execution>
<xpath-execution>
<source class="constant-data-input-parameter">
<value>//my/third/xpath/expression</value>
</source>
<target class="metadata-data-output-parameter">
<metadata-key>targetMetadataKey3</metadata-key>
</target>
</xpath-execution>
</xpath-service>
Should your source xml contain namespaces, you will need to configure the mappings in this service like this;
<xpath-service>
...
<namespace-context>
<key-value-pair>
<key>n1</key>
<value>http://adaptris.com/xml/namespace1</value>
</key-value-pair>
<key-value-pair>
<key>n2</key>
<value>http://adaptris.com/xml/namespace2</value>
</key-value-pair>
<key-value-pair>
<key>n3</key>
<value>http://adaptris.com/xml/namespace3</value>
</key-value-pair>
<key-value-pair>
<key>n4</key>
<value>http://adaptris.com/xml/namespace4</value>
</key-value-pair>
</namespace-context>
...
</xpath-service>
If the DocumentBuilderFactoryBuilder
has been explicitly set to be not namespace aware and the document does in fact
contain namespaces, then Saxon can cause merry havoc in the sense that //NonNamespaceXpath
doesn't work if the document
has namespaces in it. We have included a shim so that behaviour can be toggled based on what you have configured.
XPath.newXPathInstance(DocumentBuilderFactoryBuilder, NamespaceContext)
In the adapter configuration file this class is aliased as xpath-service which is the preferred alternative to the fully qualified classname when building your configuration.
log
CONFIRMATION_ID_KEY
Constructor and Description |
---|
XPathService() |
Modifier and Type | Method and Description |
---|---|
protected void |
closeService() |
void |
doService(AdaptrisMessage msg)
Apply the service to the message.
|
java.util.List<Execution> |
getExecutions() |
KeyValuePairSet |
getNamespaceContext() |
DocumentBuilderFactoryBuilder |
getXmlDocumentFactoryConfig() |
DataInputParameter<java.lang.String> |
getXmlSource() |
protected void |
initService() |
void |
prepare()
Prepare for initialisation.
|
void |
setExecutions(java.util.List<Execution> list) |
void |
setNamespaceContext(KeyValuePairSet namespaceContext) |
void |
setXmlDocumentFactoryConfig(DocumentBuilderFactoryBuilder xml) |
void |
setXmlSource(DataInputParameter<java.lang.String> sourceDestination) |
changeState, close, continueOnFailure, createName, createQualifier, getContinueOnFail, getIsTrackingEndpoint, getLookupName, getUniqueId, init, isBranching, isTrackingEndpoint, requestClose, requestInit, requestStart, requestStop, retrieveComponentState, setContinueOnFail, setIsTrackingEndpoint, setLookupName, setUniqueId, start, stop
public void doService(AdaptrisMessage msg) throws ServiceException
Service
Apply the service to the message.
msg
- the AdaptrisMessage
to processServiceException
- wrapping any underlying Exception
spublic void prepare() throws CoreException
ComponentLifecycleExtension
CoreException
protected void initService() throws CoreException
initService
in class ServiceImp
CoreException
protected void closeService()
closeService
in class ServiceImp
public DataInputParameter<java.lang.String> getXmlSource()
public void setXmlSource(DataInputParameter<java.lang.String> sourceDestination)
public java.util.List<Execution> getExecutions()
public void setExecutions(java.util.List<Execution> list)
public KeyValuePairSet getNamespaceContext()
public void setNamespaceContext(KeyValuePairSet namespaceContext)
public DocumentBuilderFactoryBuilder getXmlDocumentFactoryConfig()
public void setXmlDocumentFactoryConfig(DocumentBuilderFactoryBuilder xml)