我有一个 XML 数据,要阅读我使用的是“xpath”和“xmldom”。我正在获取relativePath价值,但它只给出第一个 value( /abc/) 3 次。我无法获得其他价值。
var xpath = require('xpath')
var dom = require('xmldom').DOMParser
var le = `
<content>
<data>
<content-item>
<relativePath>/abc/</relativePath>
<text>abc</text>
<leaf>false</leaf>
<lastModified>2018-10-16</lastModified>
</content-item>
<content-item>
<relativePath>/defghi/</relativePath>
<text>defghi</text>
<leaf>false</leaf>
<lastModified>2018-06-23</lastModified>
</content-item>
<content-item>
<relativePath>/jklmn/</relativePath>
<text>jklmn</text>
<leaf>false</leaf>
<lastModified>2019-02-27</lastModified>
</content-item>`;
var doc = new dom().parseFromString(le);
var nodes = xpath.select("//content-item", doc);
nodes.forEach( (n, i) => {
var title = xpath.select("string(//relativePath)", n);
console.log(title);
});
最终输出:
/abc/
/abc/
/abc/
相关分类