I’m trying to parse a String which is XML like, and looks like this:
<note>
<user>Username 1</user>
<notes>Notes user wrote</notes>
</note>
<note>
<user>Username 2</user>
<notes>Notes user wrote</notes>
</note>
I’m a little bit confused with how to use the JQuery XML parser, I guess i’m used to SQL programming to access data.
What I want to do is print out the note based on the user name
so in SQL that’d be “SELECT notes FROM note WHERE user=’Username 1′”
How do you do a selection like that in the JQuery XML Parser?
Edit what i’m trying:
For some reason this keeps returning Null:
var xml = '<url>urlofpage1</url><notes>These notes should load!</notes>';
var $xml = $(xml);
var $notes = $xml.find('url')
.filter(function() { return $(this).text() == 'urlofpage1' })
.closest('notes');
console.log('$notes: ', $notes.html());
First you’d want a jQuery object:
Then you could use
findand the:containsselector to find the particular<user>you wanted and thenclosestto go back up to the<note>:For example (run with the console open): http://jsfiddle.net/ambiguous/3Yqqu/
You have to be careful with the
:containsselector though as:Hence the
user:contains(a narrow search) followed byclosestto go back up. You might want to add an extra filtering step if you’re looking for strict equality rather than something LIKEish:And an example of this (console open again please): http://jsfiddle.net/ambiguous/fYNXx/
The thinking is similar to SQL in that you’re working with sets most of the time. However, you often chain things together more in the sense of shell pipeline than nested queries, sub-selects, and derived tables.