我有以下XML,它是来自请求的字符串。我如何获得资本价值(华盛顿特区巴黎巴黎)?

<Country>
    <USA>
    <Capital>"Washington, D.C"</Capital>
    </USA>
    <France>
    <Capital>"Paris"</Capital>
    </France>
</Country>

最佳答案

使用DomParser



var xml = `<Country>
    <USA>
    <Capital>"Washington, D.C"</Capital>
    </USA>
    <France>
    <Capital>"Paris"</Capital>
    </France>
</Country>`

var parser = new DOMParser();
var doc = parser.parseFromString(xml, "application/xml");
doc.querySelectorAll('Capital').forEach(
  (cap) => console.log(cap.textContent));

关于javascript - 获取XML字段值javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54852404/

10-12 18:15