0

我有这个 XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<sizeResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<sizeReturn xsi:type="xsd:int">1</sizeReturn>
</sizeResponse>
</soapenv:Body>
</soapenv:Envelope>

我想访问 1 但 .find() 不起作用它在我的控制台中给了我这个错误

Uncaught TypeError: Object <?xml version="1.0"
encoding="UTF-8"?><soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><sizeResponse
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><sizeReturn
xsi:type="xsd:int">0</sizeReturn></sizeResponse></soapenv:Body></soapenv:Envelope>
 has no method 'getElementsByTagName'

如何使用 jQuery 或 JS 访问它(如果有使用 Xpath 插件的方法,请提供 Xpath 表达式)?

谢谢

4

2 回答 2

1

你可以试试这个。

<script>
    function GetVal()
    {
        var txt = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><sizeResponse   soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><sizeReturn   xsi:type="xsd:int">0</sizeReturn></sizeResponse></soapenv:Body></soapenv:Envelope>';
        if (window.DOMParser)
        {
            parser=new DOMParser();

            xmlDoc = parser.parseFromString(txt, "text/xml");

            var v = xmlDoc.getElementsByTagName("sizeReturn")[0].childNodes[0].nodeValue;
            alert("t" +v);
        }
        else // Internet Explorer
        {
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async=false;
            xmlDoc.loadXML(txt); 
        }
    }

</script>

干杯:)

于 2013-05-14T07:36:20.780 回答
1

尝试这个:

var xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><sizeResponse   soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><sizeReturn   xsi:type="xsd:int">0</sizeReturn></sizeResponse></soapenv:Body></soapenv:Envelope>',
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc );
console.log($xml.find("sizeReturn").html());

阅读文档http://api.jquery.com/jQuery.parseXML/

小提琴:http: //jsfiddle.net/cY5xZ/

于 2013-05-14T05:30:41.947 回答