pure js xml parser supporting xpath

443 views
Skip to first unread message

Angelo Chen

unread,
Jul 18, 2012, 10:27:39 AM7/18/12
to nodejs
Hi,

is there such xml parser exists? js based. my need is simple,
something like this in libxmljs:

xmlDoc = libxmljs.parseXmlString(xml)
s = xmlDoc.get("/paymentService/reply/error")

problem with libxmljs is, difficult to get it installed, any ideas?

Thanks,

Angelo

Andrew Lunny

unread,
Jul 18, 2012, 1:41:40 PM7/18/12
to nod...@googlegroups.com
https://github.com/racker/node-elementtree has worked quite well in my experience.

One catch I've found is that absolute XPath expressions don't work, so instead of:

xmlDoc.get("/paymentService/reply/error")

would need to say

xmlDoc._root.get("paymentService/reply/error")

Other than that (which is probably an easy patch, but I think they want to maintain API compatibility with the python source library), it's been fine.

Andrew


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Angelo Chen

unread,
Jul 18, 2012, 9:02:28 PM7/18/12
to nodejs
looks good to me, but the doc provides no sample and i don't really
know pythonelement, any easy sample to get started?
say I got this following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//WorldPay//DTD WorldPay
PaymentService v1//EN"
"http://dtd.worldpay.com/
paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="1234">
<reply>
<error code="5"><![CDATA[XML failed validation: Invalid payment
details : Card number must be at least 10 digits.:1212****]]>
</error>
</reply>
</paymentService>

and I want to get the error code which is 5 in this case? thanks.

On Jul 19, 1:41 am, Andrew Lunny <alu...@gmail.com> wrote:
> https://github.com/racker/node-elementtreehas worked quite well in my
> experience.
>
> One catch I've found is that absolute XPath expressions don't work, so
> instead of:
>
> xmlDoc.get("/paymentService/reply/error")
>
> would need to say
>
> xmlDoc._root.get("paymentService/reply/error")
>
> Other than that (which is probably an easy patch, but I think they want to
> maintain API compatibility with the python source library), it's been fine.
>
> Andrew
>

Angelo Chen

unread,
Jul 18, 2012, 11:06:48 PM7/18/12
to nodejs
Hi Andrew,

testing out the node-elementtree, it does not have enough
documentation for me to fully understand, here is an example, how can
we get the Error using xpath:

var et = require('elementtree');
var s = '<p version="1.4" Code="123"><reply><error code="5"><!
[CDATA[XML failed]]></error></reply></p>'
var e = et.parse(s);
var r = e._root.find("p/reply/error")
console.log(r)

underfined



On Jul 19, 1:41 am, Andrew Lunny <alu...@gmail.com> wrote:
> https://github.com/racker/node-elementtreehas worked quite well in my
> experience.
>
> One catch I've found is that absolute XPath expressions don't work, so
> instead of:
>
> xmlDoc.get("/paymentService/reply/error")
>
> would need to say
>
> xmlDoc._root.get("paymentService/reply/error")
>
> Other than that (which is probably an easy patch, but I think they want to
> maintain API compatibility with the python source library), it's been fine.
>
> Andrew
>

Angelo Chen

unread,
Jul 19, 2012, 10:57:58 AM7/19/12
to nodejs
node-elementtreehas works quite well, just too little documentation,
thanks for recommending.


On Jul 19, 1:41 am, Andrew Lunny <alu...@gmail.com> wrote:
> https://github.com/racker/node-elementtreehas worked quite well in my
> experience.
>
> One catch I've found is that absolute XPath expressions don't work, so
> instead of:
>
> xmlDoc.get("/paymentService/reply/error")
>
> would need to say
>
> xmlDoc._root.get("paymentService/reply/error")
>
> Other than that (which is probably an easy patch, but I think they want to
> maintain API compatibility with the python source library), it's been fine.
>
> Andrew
>
Reply all
Reply to author
Forward
0 new messages