Parse a XML element with AJAX

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Sep 2006
Posts: 26
Reputation: Alexandro is an unknown quantity at this point 
Solved Threads: 0
Alexandro Alexandro is offline Offline
Light Poster

Parse a XML element with AJAX

 
0
  #1
Jul 18th, 2007
I need to parse a element from a XML file:

<a>
<b>45</b>
<c>67</c>
</a>

<a>
<b>98</b>
<c>89</c>
</a>

So I try to read and put in a html element the first value of b with the next code:


JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.getElementById("apDiv3").innerHTML=xmlDoc.getElementsByTagName("b")[0].data;
  2.  
  3. or
  4.  
  5. document.getElementById("apDiv3").innerHTML=xmlDoc.getElementsByTagName("b")[0].value;

and I get a undefined message.Where is the problem?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,608
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 463
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Parse a XML element with AJAX

 
0
  #2
Jul 18th, 2007
Your XML file is not proper, there can be only one root element. Also since you have not posted the entire code, I would give you a small example on how to go about things:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // Javascript file
  2.  
  3. <html>
  4. <head>
  5. <title>XML DOM</title>
  6. <script type="text/javascript">
  7.  
  8. var xmlDoc;
  9. function parse()
  10. {
  11. alert('in parse');
  12. if(window.ActiveXObject)
  13. {
  14. xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  15. xmlDoc.async = false;
  16. xmlDoc.load("notes.xml");
  17. display();
  18. }
  19. else if(document.implementation.createDocument)
  20. {
  21. xmlDoc = document.implementation.createDocument("", "", null);
  22. xmlDoc.load("notes.xml");
  23. xmlDoc.onload = display;
  24. }
  25. else
  26. {
  27. alert("Browser doesn't support XML DOM manipulation");
  28. }
  29. alert('out parse');
  30. }
  31. function display()
  32. {
  33. alert('In display');
  34. document.getElementById('b1').innerHTML = xmlDoc.getElementsByTagName("b")[0].childNodes[0].nodeValue;
  35. document.getElementById('c1').innerHTML = xmlDoc.getElementsByTagName("c")[0].childNodes[0].nodeValue;
  36. document.getElementById('b2').innerHTML = xmlDoc.getElementsByTagName("b")[1].childNodes[0].nodeValue;
  37. document.getElementById('c2').innerHTML = xmlDoc.getElementsByTagName("c")[1].childNodes[0].nodeValue;
  38. }
  39. </script>
  40. </head>
  41. <body onload="parse();">
  42. <p>b1: <span id="b1"></span></p>
  43. <p>c1: <span id="c1"></span></p>
  44. <p>b2: <span id="b2"></span></p>
  45. <p>c2: <span id="c2"></span></p>
  46. </body>
  47. </html>

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // XML Document
  2.  
  3. <root>
  4. <a>
  5. <b>100</b>
  6. <c>200</c>
  7. </a>
  8. <a>
  9. <b>300</b>
  10. <c>400</c>
  11. </a>
  12. </root>

In case of any queries, do ask again.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: Alexandro is an unknown quantity at this point 
Solved Threads: 0
Alexandro Alexandro is offline Offline
Light Poster

Re: Parse a XML element with AJAX

 
0
  #3
Jul 18th, 2007
thanks you , your example help me.
Yes I know about only one root element.The xml code it was just a example inside the root elemet to understand what I want.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: Alexandro is an unknown quantity at this point 
Solved Threads: 0
Alexandro Alexandro is offline Offline
Light Poster

Re: Parse a XML element with AJAX

 
0
  #4
Jul 18th, 2007
I am back with an other question.

I want to put the xml elements values using a for cycle in input fields
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. for(i=2,i<13)
  2. input_element.value=xmlDoc.getElementsByTagName("c")[i].childNodes[0].nodeValue;

I want that input_element to be a variable that depend on i, and in function of i to represent a real html input element value.
Can you give me a solution?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC