Copy link to clipboard
Copied
I know how to do this using for loops but was wondering if it was possible with Xpath.
My xml isl like that:
<ROOT>
<Batch BatchID="19" >
<Order Swift="AUD" />
<Order Swift="USD" />
</Batch>
<Batch BatchID="22" ">
<Order Swift="AUD" />
</Batch>
</ROOT>
I want to return an array like that:
<Order Swift="AUD" BatchID="19" />
<Order Swift="USD" BatchID="19" />
<Order Swift="AUD" BatchID="22" />
thanks gurus
Copy link to clipboard
Copied
I know how to do this using for loops but was wondering if it was possible with Xpath.
Yep. What have you done by way of finding out how to do it?
For starters, there are some easy to follow tutorials here:
http://www.zvon.org/comp/r/tut-XPath_1.html
--
Adam
Copy link to clipboard
Copied
thanks,Ive doen research, this is my working code, works but is clunky
<cfscript>
getData(xmlFeed);
function GetData(xml)
{
allOrders = XmlSearch(xmlFeed, "ROOT/Batch");
var i = 0;
for(i = 1; i <= arraylen(allOrders); i = i + 1)
{
var children = allOrders.XmlChildren;
for(var j = 1; j <= arraylen(children); j = j + 1)
{
structInsert(allOrders.XmlChildren
}
}
writeDump(allOrders);
}
</cfscript>
Copy link to clipboard
Copied
here is my code ,fixed
<cfhttp method="get" url="http:/52.xml"
result="xmlFeed"/>
<cfset xmlFeed = xmlParse(xmlFeed.FileContent)>
<cfscript>
getData(xmlFeed);
function GetData(xml)
{
allOrders = XmlSearch(xmlFeed, "ROOT/Batch");
var final = arrayNew(1);
for(i = 1; i <= arraylen(allOrders); i = i + 1)
{
var children = allOrders.XmlChildren;
for(var j = 1; j <= arraylen(children); j = j + 1)
{
structInsert(allOrders.XmlChildren
arrayAppend(final,allOrders.XmlChildren
}
}
writeDump(final );
}
</cfscript>
Copy link to clipboard
Copied
You can simplify that a bit. You're after the orders but you're only xpathing as far as the batch. You can path it all the way to the orders: /ROOT/batch/order. That saves some horsing around.
Also don't forget to always VAR •all• your variables inside functions. Not just some of them.
--
Adam
Copy link to clipboard
Copied
thanks
I know about
/ROOT/batch/order
but that doesn't get the outer Batch ID in each of the corresponding children, I think my loops are the only way for that?
Copy link to clipboard
Copied
Oops... forgot to reply. Yeah, you're right. Sorry: didn't notice you needed stuff from the parent nodes too.
--
Adam
Copy link to clipboard
Copied
Have you considered applying an XSL transformation instead of trying to parse/write the data in CF?