views:

38

answers:

1

Hi,

how can i get the document object out of this

var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml");
+1  A: 

In your example, xmlobject is the document object, according to MDC. According to w3schools, on IE, you need to use an IE-specific ActiveX object instead of DOMParser:

var xmlDoc, parser;
if (window.DOMParser) {
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(text,"text/xml");
}
else { // Internet Explorer
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(text); 
}

You've said that getElementById isn't working. Note that id is not a special attribute (an attribute of type "ID") in XML by default, so even if you're giving elements an id attribute, getElementById won't work (it should return null). Details in the W3C docs for getElementById. I've never done it, but I assume you'd assign an attribute the "ID" type via a DTD.

Without one, though, you can use other traversal mechanisms. For example (live copy):

var xmlDoc, parser, text, things, index, thing;

text =
    '<test>' +
    '<thing>Thing 1</thing>' +
    '<thing>Thing 2</thing>' +
    '</test>';
if (window.DOMParser) {
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(text,"text/xml");
}
else { // Internet Explorer
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(text); 
}
things = xmlDoc.documentElement.getElementsByTagName('thing');
for (index = 0; index < things.length; ++index) {
    thing = things.item(index);
    display(index + ": " + getText(thing));
}

...where getText is:

function getText(element) {
    return textCollector(element, []).join("");
}
function textCollector(element, collector) { 
    for (node = element.firstChild; node; node = node.nextSibling) { 
        switch (node.nodeType) { 
            case 3: // text 
            case 4: // cdata 
            collector.push(node.nodeValue); 
            break; 
          case 8: // comment 
              break; 
          case 1: // element 
            if (node.tagName == 'SCRIPT') { 
                break; 
            } 
            // FALL THROUGH TO DEFAULT 
            default: 
            // Descend 
            textCollector(node, collector); 
            break; 
        } 
    }
    return collector;
}   

(getText is a good example of why I use libraries like jQuery, Closure, Prototype, YUI, or any of several others for this stuff. You'd think it would be simple to get the text inside an element, and it is if the element has exactly one text node inside it [as our things do above]. If it doesn't, well, it gets complicated fast.)

T.J. Crowder
hmmmm....i was not able to invoke methods like getElementById etc...thts why,...guess there is something else wrong...
Arun Abraham
@Arun: You may be looking for `xmlDoc.documentElement` -- I'll work up a quick example.
T.J. Crowder
@Arun: I've added an example.
T.J. Crowder
@Arun: ...and a note about why `getElementById`, specifically, may not have been working for you.
T.J. Crowder
@T.J.Crowder:ok cool....my problem was that there an invalid xml returned by my platform so i kinda had to get the response in string itself and iterate through and fetch only the respective div i needed and assigned and innerHTML to the div that needed the replacement...thnks buddy...
Arun Abraham
@Arun: *Ouch* Well, in any case, glad that helped.
T.J. Crowder
@T.J.Crowder: one more doubt clearTimeOut() method does not work for me in the platform that i am working on...it works fine when i am trying in a browser...w3schools example..any idea wat could be causing this?
Arun Abraham
@Arun: Best to ask new questions as questions. `clearTimeout` (note the capitalization, it matters in JavaScript) should be provided by all implementations that support the timer functions of the `window` object. Of course, JavaScript is not limited to browsers, and so it's perfectly valid to have a JavaScript environment that doesn't have `setTimeout`, `clearTimeout`, etc. In that case, if the environment doesn't provide something equivalent, you're out of luck -- there's no way to do it with JavaScript on its own without help from the environment.
T.J. Crowder