Skip to main content
nikos101
Inspiring
May 28, 2011
Question

XPath question

  • May 28, 2011
  • 1 reply
  • 800 views

     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

This topic has been closed for replies.

1 reply

Inspiring
May 28, 2011

     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

nikos101
nikos101Author
Inspiring
May 28, 2011

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.XmlAttributes,"BatchID",allOrders.XmlAttributes["BatchID"]);
            }
       
        }
                        writeDump(allOrders);
    }
   
</cfscript>

nikos101
nikos101Author
Inspiring
May 28, 2011

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.XmlAttributes, "BatchID",  allOrders.XmlAttributes["BatchID"]);
                          arrayAppend(final,allOrders.XmlChildren);
            }
        }
        writeDump(final );
    }
   
</cfscript>