gpt4 book ai didi

javascript - html 中的 xml 从 javascript/jquery 读取

转载 作者:行者123 更新时间:2023-11-28 06:28:16 26 4
gpt4 key购买 nike

我有一个像这样的 html 文件:

<html>
<script src="/path/to/script1.js"></script>
<xml id = "PageXML">
<books>
<book>
<name>b1</name>
<name>b2</name>
</book>
</books>
</xml>
</html>

我的问题是:在 Script1.js 中,我能够以文本形式获取 xml ($('#PageXML').text()),但我想读取 xml仅将内容标记为 xml 并将其加载到 jquery/javascript 文件中的 xmldocument 中。

这可能吗?

最佳答案

您可以将其读取为 HTML 并使用 jQuery.parseXML() 解析为 XML 对象。然后将其放入任意变量中。

xmlString = $('#PageXML')[0].outerHTML;

XMLParsed = jQuery.parseXML(xmlString);
console.log(XMLParsed);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<xml id="PageXML"><books><book><name>b1</name><name>b2</name></book></books></xml>

关于javascript - html 中的 xml 从 javascript/jquery 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34927262/

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