Working with XML DOM in Javascript

Discussion in 'HTML & Website Design' started by ecigazon, Feb 1, 2010.

  1. #1
    I'm implementing/learning some ajax things, but I'm a bit stuck on how to work with the
    xml result. I'm basing my code off Dynamic Drives ajax examples.

    http://www.dynamicdrive.com/dynamicindex17/ajaxroutine.htm


    The text version comes back fine and displays all the relevant echoed text from the server, but when the XMl version echos back I simply get the DOM. The examples on Dynamic drive don't include anything for working
    client side with the DOM and I've scoured the net and tried numerous things in an attempt to work directly
    with the return xml.


    function processGetPost()
    {

    var myajax=ajaxpack.ajaxobj
    var myfiletype=ajaxpack.filetype

    if (myajax.readyState == 4)
    { //if request of file completed
    document.getElementById("img-l").className='nothere2';
    document.getElementById("btncc").className='there';
    if (myajax.status==200 || window.location.href.indexOf("http")==-1)
    { //if request was successful or running script locally
    if (myfiletype=="txt")
    {
    alert(myajax.responseText)
    }
    else
    {
    alert(myajax.responseXML);
    ....

    Result is alert message box: [object Document]


    So the question is how do I work directly with the xml/read it.

    Say my xml looked something like this...

    <xmlresponse>
    <result><? echo $result ?></result>
    <code><? echo $code ?></code>
    <fname><? echo $fname ?></fname>
    <lname><? echo $lname ?></lname>
    </xmlresponse>

    How do I work with the XML DOM in order to display/get/work with fname?
     
    ecigazon, Feb 1, 2010 IP
  2. drhowarddrfine

    drhowarddrfine Peon

    Messages:
    5,428
    Likes Received:
    95
    Best Answers:
    7
    Trophy Points:
    0
    #2
    You are supposed to get back the DOM. If that's the response you set, it will parse it into the DOM tree.
     
    drhowarddrfine, Feb 2, 2010 IP
  3. ecigazon

    ecigazon Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I managed to figured this out eventually.

    //XML

    <xmlresponse>
    <transaction>
    <result><? echo $result ?></result>
    <code><? echo $code ?></code>
    <fname><? echo $fname ?></fname>
    <lname><? echo $lname ?></lname>
    </transaction>
    </xmlresponse>

    alert(myajax.responseXML);

    //The code

    var xmlobj=myajax.responseXML

    //working in FF & chrome
    alert(xmlobj.getElementsByTagName("transaction")[0].childNodes[1].textContent); //result ok
    alert(xmlobj.getElementsByTagName("transaction")[0].childNodes[3].textContent); //code ok
    alert(xmlobj.getElementsByTagName("transaction")[0].childNodes[5].textContent); //fname ok
    alert(xmlobj.getElementsByTagName("transaction")[0].childNodes[7].textContent); //lname ok


    When I attempt to get the result in a message box in IE. I get the first message/alert box displaying [object]. Then the following message boxes don't display.

    Anyone know why this is?
     
    ecigazon, Feb 3, 2010 IP