XML DOC中的属性值

时间:2016-09-20 09:04:49

标签: javascript html xml xmldom

我试图获取XML文件中的属性值,但始终收到消息"Object required"

我的JS代码

    <!DOCTYPE html>
    <html>
    <body>
    <p id="demo"></p>
    <script

加载xml

        var x, i, txt;
            try{
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.load("atm.xml");
            }
            catch(err){
            alert(err.message);
            }

        if(!xmlDoc){
        alert("Error");
        }

        x = xmlDoc.getElementsByTagName("softKey")[0].attributes;
        alert(x.length);
        txt = x.getNamedItem("speech").nodeValue + "<br>" + x.length;
        alert(txt);
    </script>
    </body>
    </html>

并且有XMl文件:

        <?xml version="1.0" encoding="UTF-8"?>
<atmPage id="000" type="null" flash="" timeout="StandardTimeout" isEpp="false" isCancel="2" isFix="2" isOk="2" langCode="some text" skin="REGULAR" inputLength="0" popupGif="">
    <atmSoftKeys>
        <softKey index="1" text="some text " speech="some text" key="A" type="SOFT_KEY" langType="mu"/>
        <softKey index="2" text="some text" speech="some text" key="B" type="SOFT_KEY" langType="mu"/>
    </atmSoftKeys>
    <pageTitle title="some text" titleLangType="some text"/>
    <notification text="" notificationLangType="some text"/>
    <atmEppKeys/>
</atmPage>

1 个答案:

答案 0 :(得分:0)

好的,很简单,

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.load("Xml.xml");
        var x = xmlDoc.getElementsByTagName("softKey")[0].getAttribute("speech");