`
白云城主
  • 浏览: 16872 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

This example illustrates the use of the parseFromString and the loadXML methods:

 
阅读更多
<head>
    <script type="text/javascript">
        function CreateMSXMLDocumentObject () {
            if (typeof (ActiveXObject) != "undefined") {
                var progIDs = [
                                "Msxml2.DOMDocument.6.0",
                                "Msxml2.DOMDocument.5.0",
                                "Msxml2.DOMDocument.4.0",
                                "Msxml2.DOMDocument.3.0",
                                "MSXML2.DOMDocument",
                                "MSXML.DOMDocument"
                              ];
                for (var i = 0; i < progIDs.length; i++) {
                    try {
                        return new ActiveXObject(progIDs[i]);
                    } catch(e) {};
                }
            }
            return null;
        }

        function BuildXMLFromString (text) {
            var message = "";
            if (window.DOMParser) { // all browsers, except IE before version 9
                var parser = new DOMParser();
                try {
                    xmlDoc = parser.parseFromString (text, "text/xml");
                } catch (e) {
                        // if text is not well-formed,
                        // it raises an exception in IE from version 9
                    alert ("XML parsing error.");
                    return false;
                };
            }
            else {  // Internet Explorer before version 9
                xmlDoc = CreateMSXMLDocumentObject ();
                if (!xmlDoc) {
                    alert ("Cannot create XMLDocument object");
                    return false;
                }

                xmlDoc.loadXML (text);
            }

            var errorMsg = null;
            if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
                errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
                          + " at line " + xmlDoc.parseError.line
                          + " at position " + xmlDoc.parseError.linepos;
            }
            else {
                if (xmlDoc.documentElement) {
                    if (xmlDoc.documentElement.nodeName == "parsererror") {
                        errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
                    }
                }
                else {
                    errorMsg = "XML Parsing Error!";
                }
            }

            if (errorMsg) {
                alert (errorMsg);
                return false;
            }

            alert ("Parsing was successful!");
            return true;
        }

        function TestContent1 () {
            var xmlText = "<root><fruit color='red'></root>";
            BuildXMLFromString (xmlText);
        }
        function TestContent2 () {
            var xmlText = "<root><fruit color='red'></fruit></root>";
            BuildXMLFromString (xmlText);
        }
    </script>
</head>
<body>
    <span>&lt;root&gt;&lt;fruit color='red'&gt;&lt;/root&gt;</span><br />
    <button onclick="TestContent1 ()">Build XML</button>
    <br /><br />
    <span>&lt;root&gt;&lt;fruit color='red'&gt;&lt;/fruit&gt;&lt;/root&gt;</span><br />
    <button onclick="TestContent2 ()">Build XML</button>

    <div id="info"></div>
</body>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics