Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing XML with JQuery 1

Status
Not open for further replies.

pcsmurf

IS-IT--Management
Apr 26, 2002
47
0
0
GB
Not sure if this is an XML question or a JQuery question but I have an XML file which is given to me by another company (so I cannot make any changes to its structure). The general format is:

<products>
<product>
<name>Title</name>
<description>Description ...</description>
<field>
<name>fieldname</name>
<value>New</value>
</field>
</product>
....
</products>

The following jquery code loops through the XML and should print out just the product name. Unfortunately the name filed in <field> is also printed out.

$(document).ready(function()
{
$.ajax({
type: "GET",
url: "pf.xml",
dataType: "xml",
success: parseXml
});
});

function parseXml(xml)
{
//find every product and print the name
$(xml).find("product").each(function()
{
$("#output").append($(this).find("name").text() + "<br />");
});

}

can I make this code only refer to the actual product name ?

 
> $("#output").append($(this).find("name").text() + "<br />");
[tt] $("#output").append($(this).[red]children[/red]("name[red]:first[/red]").text() + "<br />");[/tt]
 
Thank you so much for saving my sanity. I knew it shouldn't be too difficult but I just could not get it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top