User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 426,477 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,242 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 3022 | Replies: 3
Reply
Join Date: Sep 2006
Posts: 26
Reputation: Alexandro is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Alexandro Alexandro is offline Offline
Light Poster

Parse a XML element with AJAX

  #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:


document.getElementById("apDiv3").innerHTML=xmlDoc.getElementsByTagName("b")[0].data;

or

document.getElementById("apDiv3").innerHTML=xmlDoc.getElementsByTagName("b")[0].value;

and I get a undefined message.Where is the problem?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Parse a XML element with AJAX

  #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 file

<html>
<head>
    <title>XML DOM</title>
    <script type="text/javascript">
    
    var xmlDoc;
    function parse()
    {
        alert('in parse');
        if(window.ActiveXObject)
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.load("notes.xml");
            display();
        }
        else if(document.implementation.createDocument)
        {
            xmlDoc = document.implementation.createDocument("", "", null);
            xmlDoc.load("notes.xml");
            xmlDoc.onload = display;
        }
        else
        {
            alert("Browser doesn't support XML DOM manipulation");
        }
        alert('out parse');
    }
    function display()
    {
        alert('In display');
        document.getElementById('b1').innerHTML = xmlDoc.getElementsByTagName("b")[0].childNodes[0].nodeValue;
        document.getElementById('c1').innerHTML = xmlDoc.getElementsByTagName("c")[0].childNodes[0].nodeValue;
        document.getElementById('b2').innerHTML = xmlDoc.getElementsByTagName("b")[1].childNodes[0].nodeValue;
        document.getElementById('c2').innerHTML = xmlDoc.getElementsByTagName("c")[1].childNodes[0].nodeValue;
    }
    </script>
</head>
<body onload="parse();">
    <p>b1: <span id="b1"></span></p>
    <p>c1: <span id="c1"></span></p>
    <p>b2: <span id="b2"></span></p>
    <p>c2: <span id="c2"></span></p>
</body>
</html>

// XML Document

<root>
    <a>
        <b>100</b>
        <c>200</c>
    </a>
    <a>
        <b>300</b>
        <c>400</c>
    </a>
</root>

In case of any queries, do ask again.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Sep 2006
Posts: 26
Reputation: Alexandro is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Alexandro Alexandro is offline Offline
Light Poster

Re: Parse a XML element with AJAX

  #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  
Join Date: Sep 2006
Posts: 26
Reputation: Alexandro is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Alexandro Alexandro is offline Offline
Light Poster

Re: Parse a XML element with AJAX

  #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
for(i=2,i<13) 
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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 4:06 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC