Javascript 使用jQuery访问XML值

Javascript 使用jQuery访问XML值,javascript,jquery,xml,xpath,Javascript,Jquery,Xml,Xpath,我有这个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=

我有这个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.
我想访问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'
未捕获类型错误:对象0
没有方法“getElementsByTagName”
我如何使用jQuery或JS访问它(如果有办法使用Xpath插件,请提供Xpath表达式)

谢谢

试试这个:

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());
var xml='0',
xmlDoc=$.parseXML(xml),
$xml=$(xmlDoc);
log($xml.find(“sizeReturn”).html());
阅读文档

小提琴:你能试试这个吗

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

函数GetVal()
{
var txt='0';
if(window.DOMParser)
{
parser=新的DOMParser();
xmlDoc=parser.parseFromString(txt,“text/xml”);
var v=xmlDoc.getElementsByTagName(“sizeReturn”)[0]。子节点[0]。节点值;
警报(“t”+v);
}
else//internetexplorer
{
xmlDoc=新的ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async=false;
xmlDoc.loadXML(txt);
}
}

干杯:)

我想这会对你有所帮助:那不值得一个沮丧的人不工作,对不起;看看这是否有助于将XML字符串放入Ajax的数据变量中call@Seeking知识:也许你应该在你的问题中提供更多的上下文(例如,不工作的javascript部分)