Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Flash / Data Integration / August 2006



Tip: Looking for answers? Try searching our database.

traversing the XML data tree

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Goo101 - 25 Aug 2006 11:19 GMT
I have the following layout to my XML file . . .

<?xml version="1.0" encoding="UTF-8" ?>
<VoucherNumbers>
    <GoodVoucher>20060820151601</GoodVoucher>
    <CancelVoucher>
        <CancelledVoucher>20060820091501</CancelledVoucher>
        <CancelledVoucher>20060820102201</CancelledVoucher>
        <CancelledVoucher>20060820105401</CancelledVoucher>
        <CancelledVoucher>20060820114201</CancelledVoucher>
    </CancelVoucher>
</VoucherNumbers>

I am trying to access the values in the various child nodes in the
'CancelledVoucher' field and the following code is returning the value of the
firstChild at that level (i.e. "20060820091501") . . .

function doAfterXmlDataIsLoaded(){
    //read the XML data
    var vnData:XMLNode = VoucherNumbers.firstChild;  //nodeType = 1
    var vnCancel:XMLNode = vnData.firstChild.nextSibling;  //nodeType = 1
    var vnCancelled:XMLNode = vnCancel.firstChild;  //nodeType = 1
    var vnCanVoucher:Array = vnCancelled.childNodes[0];  //nodeType = 3
    trace(vnCanVoucher);
}

I know this is the same as coding . . .

                     var vnCanVoucher:Array =
vnData.firstChild.nextSibling.firstChild.childNodes[0];  //nodeType = 3

But I wanted to split the levels of the data drill down so I'm more sure of my
XML data structure. But what I can't do to access the values in the remaining
childNodes . . . or even better to search through these childNodes to find the
right value as compared to the value stored in another variable in the code?

I have tried the following lines of code to access the values in the child
Nodes array but with little success . . .

    var vnCanVoucher:Array = vnCancelled.childNodes[1] //nodeType = 3 . . .
returns "undefined"
    var vnCanVoucher:Array = vnCancelled.childNodes[0].nextSibling;  //nodeType =
3 . . . returns "null"
    var vnCanVoucher:Array = vnCancelled.childNodes[0].firstChild;  //nodeType =
3 . . . returns "null"
    var vnCanVoucher:Array = vnCancelled.childNodes[0].lastChild;  //nodeType = 3
. . . returns "null"
    var vnCanVoucher:Array = vnCancelled.childNodes[0].childNodes[0];  //nodeType
= 3 . . . returns "undefined"

I can't quite understand where I'm going wrong here. I really thought
"vnCancelled.childNodes[0].nextSibling;" would work but obviously not. If
anyone can help point out where I'm going wrong that would be a great help.

Thank you!
Raymond Basque - 26 Aug 2006 11:30 GMT
<?xml version="1.0" encoding="UTF-8" ?>
<VoucherNumbers>
 <GoodVoucher>20060820151601</GoodVoucher>
 <CancelVoucher>
 <CancelledVoucher>20060820091501</CancelledVoucher>
 <CancelledVoucher>20060820102201</CancelledVoucher>
 <CancelledVoucher>20060820105401</CancelledVoucher>
 <CancelledVoucher>20060820114201</CancelledVoucher>
 </CancelVoucher>
</VoucherNumbers>

var vnData:XMLNode = VoucherNumbers.firstChild;
// returns the root node

var vnCancel:XMLNode = vnData.firstChild.nextSibling;
// returns the <CancelVoucher/> node
// this is OK if you are sure the node will always be
// after the <GoodVoucher/> node

var vnCancelled:XMLNode = vnCancel.firstChild;
// returns the first <CancelledVoucher/> node
// <CancelledVoucher>20060820091501</CancelledVoucher>

var vnCanVoucher:XMLNode = vnCancelled.childNodes[0];
// note: in the Flash XML world, all text is wrapped in
// an invisible XMLNode (type 3)
// returns the type 3 node in the
// first <CancelledVoucher/> node
// <CancelledVoucher>
// <invisibleTextNode>20060820091501</invisibleTextNode>
// </CancelledVoucher>
// note: this is an XMLNode -- not an array

var vnCanVoucher:Array = vnCancelled.childNodes[1]
// you are trying to get the second child node in the
// first <CancelledVoucher/> node -- it does not exist
// returns "undefined"

var vnCanVoucher:Array = vnCancelled.childNodes[0].nextSibling;
// you are trying to get the second child node in the
// first <CancelledVoucher/> node -- it does not exist
// returns "null"

var vnCanVoucher:Array = vnCancelled.childNodes[0].firstChild;
// you are trying to get the first child node in the
// text node-- it does not exist
// returns "null"

var vnCanVoucher:Array = vnCancelled.childNodes[0].lastChild;
// you are trying to get the last child node in the
// text node-- it does not exist
// returns "null"

var vnCanVoucher:Array = vnCancelled.childNodes[0].childNodes[0];
// you are trying to get the first child node in the
// text node-- it does not exist
// returns "null"

you could use this to iterate the <CancelledVoucher/> nodes:

for (var j=0; j<vnCancel.childNodes.length; j++){
   var vnCancelled:XMLNode = vnCancel.childNodes[j];
   var vnCanVoucher:XMLNode = vnCancelled.childNodes[0];
   trace(vnCanVoucher.nodeValue;
}

I threw a bit of code together that might get you on the right track.
Note: I use mx.xpath.XPathAPI for most of my XML work. It's not a very
robust XPath API, but it serves its purpose.

import mx.xpath.XPathAPI;

// this is a recursive utility function to retrieve
// an XMLNode.nodeValue
function getNodeValue(n:XMLNode){
if (n==undefined){return "";}
if (n.nodeValue){return n.nodeValue;}
// the node doesn't have a value so find a child node that does.
return getNodeValue(n.firstChild);
}

var vnData:XMLNode = VoucherNumbers.firstChild;  //nodeType = 1
var vnGood:XMLNode = vnData.firstChild;
var vnCancel:XMLNode = vnData.firstChild.nextSibling;  //nodeType = 1

// OR USE XPath
var vnGood:XMLNode = XPathAPI.selectSingleNode(vnData,"*/GoodVoucher");
var vnCancel:XMLNode = XPathAPI.selectSingleNode(vnData,"*/CancelVoucher");

function getCancelledVoucherNodes(useXPath:Boolean){
if (useXPath){
 return XPathAPI.selectNodeList(vnData,"*/CancelVoucher/CancelledVoucher");
}
else{
 return vnCancel.childNodes;
}
}
function getCancelledVoucherNumbers(useXPath:Boolean){
var result= new Array();
result.length=0;
var list = getCancelledVoucherNodes(useXPath);
for(var i=0; i<list.length; i++){
 result.push(getNodeValue(list[i]));
}
return result;
}
trace("\nGood voucher node: " + vnGood);
trace("\nGood voucher number: " + getNodeValue(vnGood));
trace("\nCancel voucher node: " + vnCancel);
trace("\nCancelledVoucher voucher nodes: " +
getCancelledVoucherNodes(true));
trace("\nCancelledVoucher voucher numbers: " +
getCancelledVoucherNumbers(true));

Hope this helps.

Cheers!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.