获取节点属性值和节点值

时间:2012-08-16 04:17:44

标签: jquery xml

我这里有这个XML,

<?xml version="1.0"?>

<!DOCTYPE reviews SYSTEM "bookstore.dtd">

<bookstore>
<location>Port Credit</location>
<address intersection="Hurontario And Lakeshore"/>
<book id="1000">
    <title> Intro to C</title>
    <copies>5</copies>
    <author name="John Fingle"/>
    <author name="Carrie Dingle"/>
<price>20.95</price>
</book>
<book id="1001">
    <title>C for Rocket Scientists</title>
    <copies>3</copies>
    <author name="Robert Johnson"/>
    <author name="B. King"/>
<price>28.95</price>
</book>
<book id="1011">
    <title>Les Miserables</title>
    <copies>5</copies>
    <author name="Victor Hugo"/>
<price>24.95</price>
</book>
</bookstore>

我试图通过这个来获得Book id和它们的价格,

$(xml).find("bookstore").each(function() {
    $(this).find("book").each(function() {
        $("#bookListDiv").html($(xml).find("price").text() + "<br />");
        $("#bookListDiv").html($(this).attr("id"));
    })
 });

此外,我正在努力获得这个名字&amp;也使用同样的东西一起标题。它所做的只是给出一个结果。我必须使用一种循环吗?

1 个答案:

答案 0 :(得分:1)

尝试使用

$(xml).find("bookstore").each(function() {
    $(this).find("book").each(function() {
        $("#bookListDiv").append($(this).find("price").text() + "<br />");
        $("#bookListDiv").append($(this).attr("id"));
    })
 });


html() will replace your content inside div