gpt4 book ai didi

javascript - 使用 jQuery 访问 XML 值

转载 作者:行者123 更新时间:2023-12-02 18:44:35 24 4
gpt4 key购买 nike

我有这个 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 表达式)?

谢谢

最佳答案

试试这个:

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/

fiddle : http://jsfiddle.net/cY5xZ/

关于javascript - 使用 jQuery 访问 XML 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16535756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com