2 Replies - 1167 Views - Last Post: 06 March 2011 - 04:45 PM

#1 thescoop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 10

parseXml help

Posted 06 March 2011 - 01:57 AM

Well I’m really stuck on this one. I’m trying to figure out a way to search through an xml file by looking for a certain element and producing the whole group using jquery. Here is what I have so far. I appreciate any help you can give me.

For example here is my xml:


<library>
<book>
 <author>
 <name>john doe</name>
 </author>
 <title>book one</title>
 <copyright></copyright>
 <location>http://site.com</location>
 <keywords>
 <keyword>novel</keyword>
 <keyword>history</keyword>
 </keywords>
 </book>
 <book>
 <creator>
 <name>jimmie john</name>
 <email>[email protected]</email>
  </creator>
  <title>Second book</title>
 <location>http://www.site.com</location>
 <keywords>
 <keyword>fullcolor</keyword>
 <keyword>kids book</keyword>
 </keywords>
 </book>
 </library>
 


Here is my html and jquery:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Project 2</title>


<!--ajax-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){

  $("button").click(function(){
      if ($("#one").val() == "" && $("#two").val() == "") {
        alert("nothing is entered");
    }
    else if ($("#one").val() != "" && $("#two").val() == "") {
    $.ajax({
    type: "GET",
    url: "photo.xml",
    dataType: "xml",
    success: function(xml) { parseXml(xml); }

    });
    };

    function parseXml(xml)
    {
        $(xml).find(“photo”).each(function()
        if($("author", this).text() == "#one") {


         }
    }
  });
});


</script>
<!--/ajax-->





<style type="text/css">
<!--
body,td,th {
	color: #F00;
}
body {
	background-color: #000;
}
-->
</style></head>

<body>
<center>
<h1>image search</h1>
    <form STYLE="background:darkgrey" >
    <strong>
      <p>Creator's Name: <input type="text" id="one" name="name" /></p>
      <p>Keyword: <input type="text" id="two" name="key" /></p>
      <button>Search</button>
      </strong>
    </form>


     <div id="results"> </div>

    </center>
  </body>
</html>



This post has been edited by thescoop: 06 March 2011 - 01:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: parseXml help

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: parseXml help

Posted 06 March 2011 - 01:53 PM

Why are you searching for an element named "photo" when your xml doesn't have such an element? When you say whole group you are meaning the entire element node? Or are you wanting a list of photo elements? Pound signs (#) are typically used to identify elements with an id name. So if you wanted to compare the author value to that of the creator's name box, you will want to be doing something along the lines of...

if($("author", this).text() == $("#one").val()) {



This is saying for the author element of this DOM element, compare its text to the value from the input box with the id "one".

But anyways, please be a little more clear as to what you are trying to achieve. Using your XML, can you tell us the element or group of elements you are trying to find and pull out? Keep in mind that when you have used $(selector) you are selecting the element and all of its CHILDREN elements.

:)

This post has been edited by Martyr2: 06 March 2011 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 thescoop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 10

Re: parseXml help

Posted 06 March 2011 - 04:45 PM

my bad. i've been really stressed.

photo = book

i'm trying to make it come up with and display the group of elements under <book> with a matching <author>

does that make more sense?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1