Cucurigu Cucurigu - 2 months ago 19
jQuery Question

jQuery - How to access multiple XMLE children, some with same name

I have an XMLE document that has a given structure (I cannot change it), and it goes like this:

<title>Some description</title>
<title>Another Description</title>
<see>Some reference to other entries</see>
<title>Another term<nemod>(-some) (detail)</nemod></title>
<title>A more detailed term</title>
<seeAlso>similar terms</seeAlso>
<term level="1">
<term level="1">

What I'm trying to do is use jQuery to access the contents of this file, and to build a table with it.

Problem is I'm having trouble accessing certain nodes properly.

Here is the jQuery to access the xml file:

url: 'index.xml', // name of file you want to parse
dataType: "xml",
success: parse,
error: function(){alert("Error: Something went wrong");}

And here is the script I am trying to use to build the table:

function parse(document){

$code = ($(this).find('code').text());
$desc = ($(this)).find('mainTerm->title').text();

//Build table
'<table><tr><th>Code</th><th>Description</th></tr><tr><td> '+$code+' </td> <td> '+$desc+' </td></tr></table>'

The $code variable works, but the $desc doesn't. How do I access the text of the node index->letter->mainTerm->title? Note there is another node called "title" higher up, which I don't need.

Appreciate any suggestions!


Use this:

$desc = $(this).find('mainTerm title').text();

It selects the title within the mainTerm element of the selected item.

Read more on element selection.