Lamis
Lamis

Reputation: 466

Finding an XML element value using Jquery

I have an XML file that I need to find the item of specific PK using Jquery and Ajax so far I get to know the object but I have two questions :

  1. Is there a better idea than doing a loop to find the pk value?
  2. the XML is large and I need to know if there is a better way to query it than loading it into memory, does XSLT help better? or is there anyway to do it better than jquery?

Here is my code

$.ajax({
    url: 'xml/products.xml',
    dataType: 'html',
    success: function(xml) {
        $(xml).find('pk').each(function() {
            if ($(this).text() == "1")
            //do something
        });
    }
});

And here is my xml

<products>
<item>
    <pk>1</pk>
    <name>test</name>
</item>
<item>
    <pk>2</pk>
    <name>test2</name>
</item>
<item>
    <pk>3</pk>
    <name>test3</name>
</item>
<item>
    <pk>4</pk>
    <name>test4</name>
</item>
</products>

Upvotes: 4

Views: 26088

Answers (2)

gaurang171
gaurang171

Reputation: 9080

First, you have to write correct XML string, like you have to complete/ending same tag which has been started last one. on above sample code, you have done mistake with closing . it is wrong xml syntax. please make correction as below: 1 test

Here i have made on sample bins for parsing XML data or tags, Instead of Ajax i have just parse xml data on button click event because on bins Ajax call is not possible to call external file.

Here is Demo: http://codebins.com/bin/4ldqp7u

HTML

<div>
  <input type="button" id="btnxml" value="Get XML Data" />
  <input type="button" id="btnreset" value="Reset" style="display:inline"/>
  <div id="result">
  </div>
</div>
<div id="xmldata">
  <products>
    <item>
      <pk>
        1
      </pk>
      <name>
        test
      </name>
    </item>
    <item>
      <pk>
        2
      </pk>
      <name>
        test2
      </name>
    </item>
    <item>
      <pk>
        3
      </pk>
      <name>
        test3
      </name>
    </item>
    <item>
      <pk>
        4
      </pk>
      <name>
        test4
      </name>
    </item>
  </products>
</div>

JQuery:

$(function() {
    $("#btnxml").click(function() {
        var xml = "<rss version='2.0'>";
        xml += $("#xmldata").html();
        xml += "</rss>";
        var xmlDoc = $.parseXML(xml),
            $xml = $(xmlDoc);

        var result = "";
        if ($xml.find("item").length > 0) {

            result = "<table class='items'>";
            result += "<tr><th>PK</th><th>Name</th></tr>";

            $xml.find("item").each(function() {
                result += "<tr>";
                result += "<td>" + $(this).find("pk").text() + "</td>";
                result += "<td>" + $(this).find("name").text() + "</td>";
                result += "</tr>";
            });

            result += "</table>";
            $("#result").html(result);
        }


    });

    //Reset Result 
    $("#btnreset").click(function() {
        $("#result").html("");
    });

});

CSS:

#xmldata{
  display:none;
}
table.items{
  margin-top:5px;
  border:1px solid #6655a8;
  background:#55a5d9;
  width:20%;
}
table.items th{
  border-bottom:1px solid #6655a8;
}
table.items td{
  text-align:center;
}
input[type=button]{
  border:1px solid #a588d9;
  background:#b788d9;
}

Demo: http://codebins.com/bin/4ldqp7u

Upvotes: 6

bhamlin
bhamlin

Reputation: 5187

At the very least, you can use a more specific query than just "pk". In this example, $(xml).find("products item pk") should be faster.

Upvotes: 1

Related Questions