Only Fools and Webgeeks
May 30, 2007 4:49 PM Subscribe
I need to parse an XML flat file with JavaScript, and I'm struggling with how to use the DOM to select one particular node via an attribute then use its child nodes to populate some values. [more]
(Let's get one thing out of the way first: I don't think I can use XSLT. Please prove me wrong, but the problem is that I'm taking the XML node values and using them in a JavaScript function (that calls runs embedded Flash videos), and by rule don't I have to enclose JavaScript within an XSLT stylesheet inside CDATA, thus meaning any XSLT tag inside won't be parsed?)
(Oh, and my DBA is so overwhelmed there's no way I can get his attention to give me some tables before July... and I need this done a week ago.)
Anyway, here's the basic XML, really simplified:
<root>
<president ident="Clinton_Bill" />
<name> William J. Clinton </name>
<video url="http://someurl.com/clinton.flv" />
</root>
What I want is to have a query string on the URI, then use the value of that query string to match it against the ident attribute on president and if it matches use that node's values on the page.
In XSLT, I would set a param we'll call "query" and give it a value of the query string, then select root/president[@ident = $query].
So, how do I do this bit in JavaScript? I know how to load the node through XMLHTTPRequest and other methods, but how do I traverse the node until I find the right one? I'm thinking I run a for-next loop and if the attribute value matches process it, but that seems... inefficient. Is that the only way? Is there a better way?
I've gone through a dozen pages, but I haven't found anyone attempting to do this, which seems really strange to me. And, admittedly, JavaScript is my weakest web language, so there's that going against me.
(Let's get one thing out of the way first: I don't think I can use XSLT. Please prove me wrong, but the problem is that I'm taking the XML node values and using them in a JavaScript function (that calls runs embedded Flash videos), and by rule don't I have to enclose JavaScript within an XSLT stylesheet inside CDATA, thus meaning any XSLT tag inside won't be parsed?)
(Oh, and my DBA is so overwhelmed there's no way I can get his attention to give me some tables before July... and I need this done a week ago.)
Anyway, here's the basic XML, really simplified:
<root>
<president ident="Clinton_Bill" />
<name> William J. Clinton </name>
<video url="http://someurl.com/clinton.flv" />
</root>
What I want is to have a query string on the URI, then use the value of that query string to match it against the ident attribute on president and if it matches use that node's values on the page.
In XSLT, I would set a param we'll call "query" and give it a value of the query string, then select root/president[@ident = $query].
So, how do I do this bit in JavaScript? I know how to load the node through XMLHTTPRequest and other methods, but how do I traverse the node until I find the right one? I'm thinking I run a for-next loop and if the attribute value matches process it, but that seems... inefficient. Is that the only way? Is there a better way?
I've gone through a dozen pages, but I haven't found anyone attempting to do this, which seems really strange to me. And, admittedly, JavaScript is my weakest web language, so there's that going against me.
Response by poster: Huh. Someone told me that there was no xpath support. Quite wrongly, it would seem.
Apparently, selectNodes with XPath is only MSXML; Mozilla implements XPath "more completely" which requires some testing and hacking. But it looks like there's boilerplate out there for that.
posted by dw at 11:20 PM on May 30, 2007
Apparently, selectNodes with XPath is only MSXML; Mozilla implements XPath "more completely" which requires some testing and hacking. But it looks like there's boilerplate out there for that.
posted by dw at 11:20 PM on May 30, 2007
If you're in control of the data format, and don't have to use XML, you might want to look into using JSON for your data. In my experience, JSON is easier to get your head around than trying to parse XML. You could even continue to use XML, but convert it to JSON to parse.
Just an idea. Good luck!
posted by fletchmuy at 8:30 AM on June 1, 2007
Just an idea. Good luck!
posted by fletchmuy at 8:30 AM on June 1, 2007
« Older Best place to get married/divorced? | How do I give my graphics the emotion of joy and... Newer »
This thread is closed to new comments.
Depending on the assumptions you can make about the structure of the XML, you can also be smarter. Like use document.getElementsByTagName (if you're not using XML namespacing) to find all the <president/>s.
Alternatively, the major web browsers support a subset of XPath via selectNodes() and selectSingleNode().
posted by Khalad at 5:11 PM on May 30, 2007