IMPORTANT ANNOUNCEMENT

These forums were permanently set to read-only mode on July 20, 2022. From that day onwards, no new posting or comment is allowed on the site, but the historical content remains intact and searchable.

A new location for posting questions about PlanetPress Suite is now available:

OL Learn - PlanetPress Classic (opens in new tab)

Topic Options
#47741 - 05/06/14 11:48 AM How do I Read XML Nodes in a Script job
Kap Offline
OL User

Registered: 10/20/10
Posts: 80
Hi
How can I count the nodes within an XML file from a script (vbscript) in a workflow?

I know the basic structure of my XML file but I've got a variable number of nodes a few levels in which eventually I'd like to read from as well but I'm not really sure how to access the xml file in this way (using a script) from the workflow.

Thanks

Top
#47764 - 05/07/14 10:01 AM Re: How do I Read XML Nodes in a Script job [Re: Kap]
Raphael Lalonde Lefebvre Offline
OL Expert

Registered: 10/14/05
Posts: 4956
Loc: Objectif Lune Montreal
Hi Kap,

It's doable by calling the XML DOM object. Here's an example of script:

Code:
dim objXMLDoc
dim NodeList

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.load(Watch.GetJobFilename)

Set NodeList = objXMLDoc.documentElement.selectNodes("items/item")
Watch.Log NodeList.length, 1



If used with a sample xml data like this:
Code:
<root>
  <items>
    <item>
      <id>1</id>
      <name>John</name>
    </item>
    <item>
      <id>2</id>
      <name>Jack</name>
    </item>
    <item>
      <id>3</id>
      <name>Joe</name>
    </item>
    <item>
      <id>4</id>
      <name>James</name>
    </item>
    <item>
      <id>5</id>
      <name>Jimmy</name>
    </item>
  </items>
</root>




It will count the number of "item" nodes, which in this case is 5 and display it in the log. Feel free to use "NodeList.length" for other operations in the script, you could also use Watch.SetJobInfo or Watch.SetVariable to store that value into a job info or variable, and reuse it later in your process.

EDIT:

Here's another sample that will actually loop through the nodes, and display their value in the log:

Code:
dim objXMLDoc
dim NodeList

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.load(Watch.GetJobFilename)

Set NodeList = objXMLDoc.documentElement.selectNodes("items/item")
for i = 0 to (NodeList.length-1)
  set ChildNodes = NodeList.item(i).childNodes
  Watch.Log "ID: " + ChildNodes.item(0).Text, 1
  Watch.Log "NAME: " + ChildNodes.item(1).Text, 1
next



Once again, feel free to do whatever else you want with the "ChildNodes.item(0).Text" properties. This is just an example to get you started.

Hope that helps!

Regards,
Raphaël Lalonde Lefebvre


Edited by Raphael Lalonde Lefebvre (05/07/14 10:21 AM)

Top
#47768 - 05/07/14 12:35 PM Re: How do I Read XML Nodes in a Script job [Re: Kap]
Kap Offline
OL User

Registered: 10/20/10
Posts: 80
Perfect thanks a lot!
Its all falling into place in my brain now.

Top