Server Help

Trash Talk - AJAX (JavaScript Question)

CypherJF - Fri Sep 09, 2005 7:41 pm
Post subject: AJAX (JavaScript Question)
I've been working off the following URL:

http://www.phpbuilder.com/columns/kassemi20050606.php3

Although, this tutorial is pretty much hacked up, several key things are missing from it. But in particular, I did get the majority of the idea behind the tutorial down and working. The only issue is right now I'm trying to parse XML data with Javascript, and that, isn't working right.

Code: Show/Hide
function processReqChange() {
    // only if req shows "loaded"
    if (http.readyState == 4) {
        if (http.status == 200) {
       document.submitLink.categories.disabled = false;

            alert("We're good to go...");
       var XMLResponse = http.responseXML;
      
       var product_list = XMLResponse.getElementsByTagName("list");
       var product_id   = XMLResponse.getElementsByTagName("category");

       alert("SIZE: " + product_list.length + "x" + product_id.length); // returns: SIZE: 1 x 3

       var select = document.submitLink.categories;
       //clearTopicList(select);

       for (var i = 0; i < product_id.length; i++) {
      alert("i = " + i + " " + product_id[i].getAttribute('id') + " " + product_id[i].getAttributeNode('name'));
       }

       alert('DONE.');
      
        } else {
            alert("There was a problem retrieving the XML data:\n" +
               http.statusText);
        }
    }
}


In the tutorial on the website has the following line:
var product_list = XMLResponse.getElementByTagName("list");

When Firefox says 'getElementByTagName' is not a function. So I made it getElementsByTagName - it appears to work, sort of. The code:

product_id[i].getAttribute('id')

works as well;

Essentially, I need to know how I can grab the 'name' tags in the XMl sheet from below (the ID attribute is read OK)...

Code: Show/Hide
<list>
  <category id="1">
     <name>History</name>
  </category>
  <category id="2">
     <name>Art</name>
  </category>
  <category id="3">
     <name>CSC</name>
  </category>
</list>


Perhaps this isn't valid XML so it's not being parsed correctly?

Any ideas, comments, suggestions? Please keep the ability to go off-track down to a minimum. Laterly, there's more than enough junk being posted then what's needed.
Maverick - Sat Sep 10, 2005 3:24 am
Post subject:
It should be valid XML , apart from that ';' .
There is a method like getChildNode, that should get you your 'name' node.

http://www.xmlfiles.com/dom/dom_access.asp

Code: Show/Hide
product_id[i].getAttribute('id').childNodes.item(0)


I suppose its something like that.
CypherJF - Sat Sep 10, 2005 9:02 am
Post subject:
That above didn't work right but when I do do this:

product_id[i].childNodes.item(0) - it returns [object Text].

So I tried

product_id[i].childNodes.item(0).data - which returned a new line from what it appears but no data.

Yeah that ';' wasn't supposed to be there (it doesn't actually exist in the XML).
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group