1

I want to create a simple site to display records from an xml file about butterflies. I think the requirements are simple. Search by one node(i.e. color, length,etc.). I know I could probably use php and simpleXML. But what about jQuery? Would I be able to show all records that have black in the color node of each record? I suspect that there will be no more than about 400 records. At this point also I do not need any "backend" I will just edit the xml files manually.

My XML is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<Butterflies>
  <Butterfly submitter="lildog">
    <CommonName>Monarch</CommonName>
    <LatinName>Something Latiny</LatinName>
    <MainColor>black</MainColor>
    <Date>12/3/2011</Date>
  </Butterfly>
  <Butterfly submitter="lildog">
    <CommonName>Admiral</CommonName>
    <LatinName>Something Latiny</LatinName>
    <MainColor>red</MainColor>
    <Date>12/3/2011</Date>
  </Butterfly>
</Butterflies>

Any thoughts much appreciated

Todd

1 Answer 1

4
$.get("path/to/xml/file.xml")
    .success(function(xmlData) {
        var elem = "#element_id_to_put_in"; //actually you can use any css3 selector
        $(xmlData).find("[color=black]").each(function() {
             $(elem).append($(this).html());
        });
    })
    .error(function(jqXHR, textStatus, errorThrown) {
          alert("error");
    });
Sign up to request clarification or add additional context in comments.

2 Comments

That works very well for attributes. How would you get all records that have the MainColor child as black using the xml I added above?
That is excellent! One last question...how to display the CommonName of butterflies that are maincolor black? This does not work: $(xml).find("MainColor:contains('black')").each(function() { $("#output").append($(this).find("CommonName").text()); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.