开发者

javascript/ajax help needed

How 开发者_高级运维can I get the document object out of this?

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


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜