Traversing XML with switchscript

Post Reply
sgeller
Newbie
Posts: 7
Joined: Wed Jan 18, 2012 1:53 am

Traversing XML with switchscript

Post by sgeller »

I have some XML processing i'm trying to automate using switch to pickup the XML as an asset then write some of the relevant data out.



Is there a way to have it count the occurrence of a node and walk through each?



Example.

I have an XML with an unknown number of 'member' records.



I'd like to have it iterate over each member occurrence and write out a name and ID number from within each member record.



I'm good with writing the the output file from within the switchscript.



I'm lacking in my scripting for dealing with XML, getting counts of the nodes and walking through them.



Any help or good references are appreciated.
rzacherl
Member
Posts: 36
Joined: Mon Mar 21, 2011 3:29 pm

Traversing XML with switchscript

Post by rzacherl »

Hi sgeller,



you have to make use of the methods of the scripting API's XML module.

To get a set of nodes you would use the evalToNodes() method with a suitable XPath expression which selects the nodes of interest and returns a NodeList object.

After that you can traverse through this NodeList via a for/next loop by accessing the node elements via the getItem() method of the NodeList class.



Here's a stripped down code example:



var xml = new Document( "<path to my xml file>" );

var nodeList = xml.evalToNodes( "//member", null );

var node, name, id;



for ( var i = 0; i < nodeList.length; i++ )

{

node = nodeList.getItem(i);

name = node.evalToString( "./@name", null );

id = node.evalToString( "./@id", null );

...

}



Hope that helps.





Regards,



Robert
sgeller
Newbie
Posts: 7
Joined: Wed Jan 18, 2012 1:53 am

Traversing XML with switchscript

Post by sgeller »

Thanks Robert!



I was sort of heading that way with some further tinkering.

I think i'm having trouble with supplying the path, but not sure.



So i'm using

var theWorkingPath = s.getPropertyValue("FileLocation", job);

var xml = new Document( theWorkingPath );





Where FileLocation is being passed through a flow element property assigned to [Job.Path]



In my output file (for testing) i'm writing out the path i passed and a count for the elements.



I'm getting a path /SwitchServerData/backing/XMLtoXLS/automanaged/XMLs/_0G1VX_TestFile.xml

An a length of 0 instead of seeing the expected 30.



i'm thinking var xml = new Document( theWorkingPath ); isn't working somehow, or i'm just going about passing the path incorrectly.



All this is running on a Mac.


dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Traversing XML with switchscript

Post by dkelly »

The variable [Job.Path] is the same as the Javascript API call job.getPath().



If you want to enumerate all of the nodes in the XML you can simply do:



var theXML = new Document(job.getPath());

var nodeList = theXML.getDocumentElement().getChildNodes();

for (var i=0; i < nodeList.length; i++) {

var theNode = nodeList.getItem(i);

// do something with node

}



Dwight Kelly

Apago, Inc.

dkelly@apago.com
sgeller
Newbie
Posts: 7
Joined: Wed Jan 18, 2012 1:53 am

Traversing XML with switchscript

Post by sgeller »

Thanks Dwight!



Thats got me a lot closer to what i'm looking for.





<?xml version="1.0" standalone="yes"?>

<xsdPrintOrder xmlns="http://tempuri.org/xsdPrintOrder.xsd">

<PromotionSummary PromotionID="135" PromotionName="Test Promo">

<PromotionScheduleTypeID>1</PromotionScheduleTypeID>

<SalesCode>QD</SalesCode>

</PromotionSummary>

<Member>

<MemberNumber>0109</MemberNumber>

<MemberName>Ima Member</MemberName>

</Member>

<Member>

<MemberNumber>0129</MemberNumber>

<MemberName>Sum Guy</MemberName>

</Member>

</xsdPrintOrder>





Thats an abbreviated snip of the xml i'm working with.



I'm able to get the base name of each theNode.getBaseName()



But i seem to be missing something on getting the value.

Using theNode.getValue() produces and error : TypeError. 'getValue' undefined or not a function



I think i'm missing something simple.
dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Traversing XML with switchscript

Post by dkelly »

Use getValue() to get the text value of an element, eg. TextHere. Use getAttributeValue() to get the value of an attribute, eg.



var nodeList = theXML.getDocumentElement().getChildNodes();

for (var i=0; i < nodeList.length; i++) {

var theNode = nodeList.getItem(i);

if (theNode.getBaseName() == "PromotionSummary") {

var id = theNode.getAttributeValue("PromotionID",null);

var name = theNode.getAttributeValue("PromotionName",null);

job.log(1, "found PromotionSummary id="+id+" name="+name);

var resourceNodeList = theNode.getChildNodes();

for (var ii=0; ii < resourceNodeList.length; ii++) {

var theNode = resourceNodeList.getItem(ii);

if (theNode.getBaseName() == "PromotionScheduleTypeID") {

var typeId = theNode.getValue();

job.log(1, "found PromotionScheduleTypeID id="+typeId);

}

}

}

}



Learn advanced Javascript for Switch

Full day seminar during Graph Expo in Chicago, Oct 8th, 2012

http://www.brownpapertickets.com/event/264833



Dwight Kelly

Apago, Inc.

dkelly@apago.com
Post Reply