Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    problem parsing xml, simple question (resolved)

    Greetings folks,

    I've attempting to parse in the contents of an xml file and l'm running into a bit of a wall... I'm trying to read data from that is a nodeType of 3 (text) ex: <name>textual value</name> but no matter what l do, actionscript thinks that the nodeType is 1 ex: <name src="textual value" />...

    I've watered everythign down as much as possible to make it easy for anyone willing to take a look.

    Here's my xml file:
    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <list>
    	<property>
    		<title>title here</title>
    		<price>$200,000</price>
    		<desc>descripotion here.. descripotion here... descripotion here... descripotion here... </desc>
    		<img src="pic1.jpg" />
    	</property>
    </list>
    And here's the Actionscript l'm using:
    Code:
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("feed.xml");
    function loadXML(loaded) {
    	if (loaded) {
    		xmlNode = this.firstChild;
    		total = xmlNode.childNodes.length;
    		for (i=0; i<total; i++) {
    			trace(xmlNode.childNodes[i].childNodes[0].nodeValue);
    			trace(xmlNode.childNodes[i].childNodes[1].nodeValue);
    			trace(xmlNode.childNodes[i].childNodes[2].nodeValue);
    			trace(xmlNode.childNodes[i].childNodes[3].attributes.src);
    		}
    	} else {
    		trace('could not load xml file!');
    	}
    }
    I would expect the trace commands to output:

    Code:
    title here
    $200,000
    descripotion here.. descripotion here... descripotion here... descripotion here... 
    pic1.jpg
    in this example, however the following is what l get:

    Code:
    null
    null
    null
    pic1.jpg
    Upon closer inspection, the actionscript thinks that the ladder three nodes are type 1, and note type 3 (text)... any idea how to fix this?
    If someone could let me know what l'm doing wrong l would really appreciate it, l'm sure it's a mundane oversight on my part... :S
    Last edited by Coastal Web; 02-09-2008 at 11:28 PM. Reason: issue resolved

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    xmlNode.childNodes[0].childNodes[0] reaches down to the title element and any element node's nodeValue is null. only text nodes have nodeValue defined so, changing to xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue should solve your problem
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    Coastal Web (02-09-2008)

  • #3
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Awesome, shyam ...

    did the trick perfectly... +karma and +thanks...

    Warm regards,


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •