My XML looks like this:
<ns2:OrderList>
<order order_id="123" item_name="123"/>
<order order_id="234" item_name="1233"/>
<order order_id="2357" item_name="1234"/>
......
</ns2:OrderList>
I am wanting to use the XPath.js from npjms .
How can I get array with "order_id" values ????
var select = require('xpath.js')
, dom = require('xmldom').DOMParser
var xml = `<ns2:OrderList>
<order order_id="123" item_name="123"/>
<order order_id="234" item_name="1233"/>
<order order_id="2357" item_name="1234"/>
</ns2:OrderList>`;
var doc = new dom().parseFromString(xml);
var nodes = select(doc, "//order/@order_id");
var orderIds = [];
nodes.forEach(function(node) {
orderIds.push(node.nodeValue);
});
console.log(orderIds);
Something like this should do the trick if you want to use xpath.js (you also need xmldom for parsing though).
const Dom = require('xmldom').DOMParser;
const select = require('xpath.js');
const xml = '<ns2:OrderList> <order order_id="123" item_name="123"/> <order order_id="234" item_name="1233"/> <order order_id="2357" item_name="1234"/> </ns2:OrderList>¬';
const doc = new Dom().parseFromString(xml);
const nodes = select(doc, '//order');
const orderIds = nodes.map((node) => node.getAttribute('order_id'));
console.log(orderIds);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.