SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX Xml Response Problem

    I am returning a valid XML string from by server side page, but when i call the xmlHttp.responseXML, i get a completely empty XML document object with child, parent and sibling nodes all null.

    JS is as follows:

    // State Change Handler
    function stateChanged()
    {
    if (xmlHttp.readyState == 4 /*complete*/)
    {
    debugger;

    var xml = xmlHttp.responseXML;
    var txt = xmlHttp.responseText;
    var xmlNodes = xml.getElementById("xmlouter")

    }
    }

    The XML Is as follows, and i have tried it with and without the XML Header:

    <?xml version="1.0" encoding="utf-8"?><xmlouter><xmlinner name="Header">Header Text</xmlinner><xmlinner name="Body">Body Text</xmlinner></xmlouter>

    The XML Object (var xml) content is as follows:

    nodeName: "#document"
    nodeValue: null
    nodeType: 9
    parentNode: null
    childNodes: {...} //Length = 0
    firstChild: null
    lastChild: null
    previousSibling: null
    nextSibling: null
    attributes: null
    ownerDocument: null
    nodeTypeString: "document"
    text: ""
    specified: true
    definition: null
    nodeTypedValue: null
    dataType: null
    xml: ""
    parsed: true
    namespaceURI: ""
    prefix: ""
    baseName: ""

    I'm slightly (REALLY) perplexed by this, can anyone explain what's going on to me?

    Cheers

    Tris

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to parse the xml use xmldomparser...
    http://www.w3schools.com/dom/dom_parser.asp
    cheers

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is parsed.

    The functionality for using the XML should work as follows:

    function myHandler()
    {
    if (req.readyState == 4 /*complete*/) {
    // Update address field in a form with first street address
    var addrField = document.getElementById('addr');
    var root = req.responseXML;
    var addrsElem = root.getElementsByTagName('addresses')[0];
    var firstAddr = addrsElem.getElementsByTagName('address')[0];
    var addrText = fistAddr.firstChild;
    var addrValue = addrText.nodeValue;
    addrField.value = addrValue;
    }
    }

    My problem is, why is the parsed XML empty?

  4. #4
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, i have it working using the thingy

    var txt = xmlHttp.responseText;
    var xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.async="false";
    xml.loadXML(txt);

    var xmlNodes = xml.getElementsByTagName("xmlinner")

    But shouldn't it do that natively? I mean, it returns a XMLDOM object from the XMLHTTP.responseXML, and i hate messy code ....

    Cheers

    T


Bookmarks

Posting Permissions

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