xmlDOMGet
Version 24.2.9039
Version 24.2.9039
xmlDOMGet
Get values from an XML document.
Required Parameters
- map:*: A set of one or more inputs where the name of the mapped parameter precedes the colon (for example,
map:foo
), and the value is the xpath to the desired xml element in the document. See Examples for more information.
Optional Parameters
- uri: An XML file URI. This can be a dynamic file reference such as
FilePath
for the input file, a static filepath on disk, or a public URL for an XML file. - handle: A readable handle reference to the XML data. This handle is created by the xmlOpen operation and is only necessary when the target XML is not an input file. See xmlOpen for details and examples.
- repeatelement#: The xpath to an element that is repeated in the document.
- repeatmode: Defines the way in which the
repeatelement#
values are stored on the output item. The allowed values areITEM
andARRAY
. When set toITEM
(the default), the values found from the repeat element are stored as individual attributes on each output item the operation produces. When set toARRAY
, the values found from the repeat element are stored as array attributes on a single output item and need to be accessed via their 1-based index (for example,[result.color#2]
would refer to the second value in thecolor
attribute of the result item).
Output Attributes
- *: The output values from the XML elements mapped using the map parameter. These are accessed by referencing the output item and the name of the mapped input item (for example,
[results.foo]
). The name of the output item (results
in this case) is determined by the user. See Examples for additional context.
Examples
Retrieve Values from an XML Document
Consider the XML data below, which is passed as input data to a Script Connector in CData Arc:
<Items>
<Car>
<Make>Subaru</Make>
<Model>WRX</Model>
</Car>
</Items>
In the Script connector, you can invoke xmlDOMGet in ArcScript to retrieve the make
and model
of the car in this data and add them as headers to a message in Arc. The ArcScript code below shows an example of how to do this.
<!-- Setting the input uri and map parameters -->
<arc:set attr="xml.uri" value="[FilePath]" />
<arc:set attr="xml.map:make" value="/Items/car/make" />
<arc:set attr="xml.map:model" value="/Items/car/model" />
<!-- Calling the operation, passing in the xml item and creating a "result" output item -->
<arc:call op="xmlDOMGet" in="xml" out="result">
<!-- result.make and result.model has the value of the xpath because map:make and map:model was
set in the input to the op, and result is the name of the output item. Once this
op closes, result falls out of scope, so use your value inside the arc:call block -->
<!-- Setting the values for make and model as headers on the message -->
<arc:set attr="output.header:carmake" value="[result.make]" />
<arc:set attr="output.header:carmodel" value="[result.model]" />
</arc:call>
<!-- Setting the output file and pushing the file out -->
<arc:set attr="output.filepath" value="[FilePath]" />
<arc:push item="output" />
After this code executes, Arc pushes the file with the newly added headers as output down the flow.
Access a Specific Repeated XML Element Using its Index
Users often need to access a specific repeated XML Element. For example, you might want to access the id
of the second order
present in the following XML, which is passed as an input file to a Script connector.
<Items>
<orders>
<order>
<date>02/27/2023</date>
<id>1234</id>
</order>
<order>
<date>02/28/2023</date>
<id>5678</id>
</order>
<order>
<date>03/01/2023</date>
<id>9876</id>
</order>
</orders>
</Items>
In the Script connector, you can invoke xmlDOMGet in ArcScript to access the id
of a specific order using the 1-based xpath to an element present in one of the repeated <orders>
elements. The following ArcScript code shows an example of how to do this.
<!-- Setting the input uri and map parameters -->
<arc:set attr="xml.uri" value="[FilePath]" />
<arc:set attr="xml.map:orderid" value="/Items/orders/order\[2\]/id" />
<!-- Calling the operation, passing in the xml item and creating a "result" output item -->
<arc:call op="xmlDOMGet" in="xml" out="result">
<!-- Setting the result from the mapped id parameter as a header on the message -->
<arc:set attr="output.header:orderid" value="[result.orderid]" />
</arc:call>
<!-- Setting the output file and pushing the file out -->
<arc:set attr="output.filepath" value="[FilePath]" />
<arc:push item="output" />
Use the repeatelement Parameter
Consider the XML data below, which is passed as an input file to a Script Connector:
<Items>
<hello>world</hello>
<colors>
<color>yellow</color>
<example>banana</example>
</colors>
<colors>
<color>red</color>
<example>apple</example>
</colors>
<colors>
<color>orange</color>
<example>orange</example>
</colors>
</Items>
Inside the Script Connector, the following code is executed against the input file. The repeatelement#
parameter is used to define the element that has repeating values. In this example the color
and example
values are repeated in the /Items/colors
XML elements. When you set repeatelement#
, the xmlDOMGet operation loops over the values that are present in that element.
The repeatmode
parameter defines the way in which the repeatelement#
values are stored as output. When set to ITEM
(the default), the values found by the repeat element are stored as individual attributes on the output item. When set to ARRAY
, the values found by the repeat element are stored as array attributes on the output item and need to be accessed via their 1-based index (for example, [result.color#2]
would resolve to red
and result.example#2
would resolve to apple
).
<!-- Initializing the output data -->
<arc:set attr="out.data" />
<!-- Setting the input uri, repeatmode and map parameters -->
<arc:set attr="xml.uri" value="[FilePath]" />
<arc:set attr="xml.repeatelement#" value="/Items/colors" />
<arc:set attr="xml.repeatmode" value="ITEM" />
<!-- Note, the values for the map are relative to the repeatelement path -->
<arc:set attr="xml.map:color" value="color" />
<arc:set attr="xml.map:example" value="example" />
<!-- Calling the operation, passing in the xml item and creating a "result" output item -->
<arc:call op="xmlDOMGet" in="xml" out="result">
<!-- Setting the output data to the enumerated results of the call -->
<arc:set attr="out.data">[out.data]
<arc:enum list="color,example" separator=",">[_value]: [result.[_value]]\n
</arc:enum>
</arc:set>
</arc:call>
<!-- Setting the output filename and pushing the file out -->
<arc:set attr="out.filename" value="data.txt" />
<arc:push item="out" />
Output file:
color: yellow
example: banana
color: red
example: apple
color: orange
example: orange