开发者

"Read" XML with Javascript

Still playing with Chrome Extension and have a little problem when I want to get some information from an XML file. Here is how the XML look :

<?xml version='1.0' encoding='utf8'?>
<objects>
    <url>
        <domain>...</domain>
        <nb_clics>...</nb_clics>
        <alias>...</alias>
        <title>...</title>
        <source>...</source>
        <description />
    </url>
</objects>

I've tried this but, no result :

<html>
    &l开发者_如何学Pythont;head>
        <style>
            body {
                width:160px;
            }
            p {
                font-weight:bold;
                margin-bottom:0;
            }
        </style>
    </head>
    <script type="text/javascript">
        function DOMImplementation(sUrl, fCallback) {
            var dom;
            if(window.ActiveXObject) {
                dom = new ActiveXObject("Microsoft.XMLDOM");
                dom.onreadystatechange = function() {
                    if(dom.readyState == 4) {
                        fCallback(dom);
                    }
                };
            }
            else if(document.implementation && document.implementation.createDocument) {
                dom = document.implementation.createDocument("", "", null);
                dom.onload = function() { 
                    fCallback(dom); 
                }
            }   
            else {
                alert("ERROR");
                return;
            } 
            dom.load(sUrl);
        }

        function shrimit() {
            var nicknick = localStorage["nick_name"];
            var apiapi = localStorage["api_key"];
            var yourc = document.getElementById("your");

            chrome.tabs.getSelected(null,function(tab) {
                var tablink = tab.url;
                if(!nicknick || !apiapi){
                    yourc.setAttribute("value","Set the options");
                } else {
                    DOMImplementation("post.xml", getData);
                    function getData(oData) {
                        var tablink2 = oData.getElementsByTagName("alias")[0].firstChild.data;
                        yourc.setAttribute("value",tablink2);
                    }
                }
            });
        }
    </script>
    <body onload="shrimit()">
        <input id="your" name="your" type="text" value="" />
    </body>
</html>

Can you help me ?


Use XMLHttpRequest to load an XML file. This is standardised and reliable across browsers. document.implementation.createDocument ... load is not.

(You may get an HTMLDocument instead of a plain XML Document. Normally sniff for DOMParser before falling back to document.implementation.createDocument. Also createDocument('', '', null); is invalid: you can't have an empty-string root element name. Use createDocument(null, 'name', null). Also, finally, best not use the name DOMImplementation because that's the name of a built-in interface/class present in Chrome and some others.)

Finally, using a function statement inside an else clause is invalid in JavaScript. Browsers tend to allow it but with variable results. Either declare the function at the top level, or use an inline function expression.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜