[英]Parsing XML using the xpath.js NPM Module
My XML looks like this: 我的XML看起来像这样:
<jobs>
<job>
<title>Engineer - System Planning</title>
<url>http://my.jobs/0629cdc680d04cf3bcd711a1c0b69836321</url>
<company>Iberdrola USA</company>
<location>Augusta, ME</location>
<dateacquired>2013-3-22 6:24 PM</dateacquired>
<jvid>0629cdc680d04cf3bcd711a1c0b69836321</jvid>
</job>
<job>
<title>Engineer - Hydro</title>
<url>http://my.jobs/61cccbfba50c4f93a5169aafc13c82b2321</url>
<company>Iberdrola USA</company>
<location>Rochester, NY</location>
<dateacquired>2013-7-5 8:33 PM</dateacquired>
<jvid>61cccbfba50c4f93a5169aafc13c82b2321</jvid>
</job>
</jobs>
And I am wanting to use the XPath.js NPM Module ( https://www.npmjs.org/package/xpath.js ). 我想使用XPath.js NPM模块( https://www.npmjs.org/package/xpath.js )。 but I am just not sure how to loop through each job
in the XML document using that module 但我不知道如何使用该模块遍历XML文档中的每个job
From the first example in the xpath.js documentation , with an XPath expression like //job
you'll get a node-set: 从xpath.js文档中的第一个示例开始,使用像//job
这样的XPath表达式,您将得到一个节点集:
var nodes = select(doc, "//job");
which you can loop using regular JavaScript, since it's an array. 你可以使用常规JavaScript循环,因为它是一个数组。 You can then pass each node as a parameter in another XPath expression and obtain the data inside the other nodes: 然后,您可以将每个节点作为另一个XPath表达式中的参数传递,并获取其他节点内的数据:
for (i = 0; i < nodes.length; i++) {
var title = select(nodes[i], "title/text()")[0].data;
var url = select(nodes[i], "url/text()")[0].data;
var company = select(nodes[i], "company/text()")[0].data;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.