Page 1 of 1
Traversing XML with switchscript
Posted: Thu Aug 16, 2012 4:13 pm
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.
Traversing XML with switchscript
Posted: Fri Aug 17, 2012 8:23 am
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
Traversing XML with switchscript
Posted: Fri Aug 17, 2012 4:16 pm
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.
Traversing XML with switchscript
Posted: Fri Aug 17, 2012 6:08 pm
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
Traversing XML with switchscript
Posted: Sat Aug 18, 2012 12:30 am
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.
Traversing XML with switchscript
Posted: Mon Aug 20, 2012 3:48 pm
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