Reputation: 55
I have an XMLE document that has a given structure (I cannot change it), and it goes like this:
<index>
<letter>
<title>A</title>
<mainTerm>
<title>Some description</title>
<code>Q87.1</code>
</mainTerm>
<mainTerm>
<title>Another Description</title>
<see>Some reference to other entries</see>
</mainTerm>
<mainTerm>
<title>Another term<nemod>(-some) (detail)</nemod></title>
<code>F44.4</code>
</mainTerm>
<mainTerm>
<title>A more detailed term</title>
<seeAlso>similar terms</seeAlso>
<term level="1">
<title>acute</title>
<code>R10.0</code>
</term>
<term level="1">
<title>angina</title>
<code>K55.1</code>
</term>
</mainTerm>
</letter>
</index>
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:
$.ajax({
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){
$(document).find("letter").each(function(){
$code = ($(this).find('code').text());
$desc = ($(this)).find('mainTerm->title').text();
//Build table
$("#content").append(
'<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!
Upvotes: 1
Views: 62
Reputation: 702
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.
Upvotes: 1