?
Solved

jQuery Filter XML data

Posted on 2009-07-13
1
Medium Priority
?
2,366 Views
Last Modified: 2012-05-07
I have the following code below.  I can load the XML no problems but what i want to do is filter the xml data so it will select the node with the id=1 and put title,teaser in separate variables.  


<!-Start xml Code -->
<?xml version="1.0" encoding="iso-8859-1"?>
 <stories>
   <story id='1'>
     <title>MyTitle 1</title>
     <teaser>test</teaser>
	 <readmore>test</readmore>
   </story>
   
    <story id='2'>
     <title>MyTitle 2</title>
     <teaser>test</teaser>
	 <readmore>test</readmore>
   </story>
   
   
    <story id='3'>
     <title>MyTitle 3</title>
     <teaser>test</teaser>
	 <readmore>test</readmore>
   </story>
   
 </stories>
<!--End XML code --->
 
 
<!-- Start jQuery Code -->
  <script type="application/javascript">
	 <!-- Avoid W3C validator on heavy javascript 
        $(document).ready(function() {
             $.ajax({
                 type: "GET",
                 url: "xml/myxml.xml",
                 dataType: "xml",
                 success: function(xml) {
                    var returnRecord = $(xml).filter("[ id = 1 ]");
					var str=jQuery.param(returnRecord);
   
                 }
             }); //close $.ajax(
         }); //
 	 //-->
    </script>
<!-- End jQuery Code -->

Open in new window

0
Comment
Question by:stargateatlantis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 24845918

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
  <script type="text/javascript">
         
        $(document).ready(function() {
             $.ajax({
                 type: "GET",
                 url: "xml/myxml.xml",
                 dataType: "xml",
                 success: function(xml) {
				
                    
				var title = $(xml).find("story[id=1] > title").text();
                   var teaser = $(xml).find("story[id=1] > teaser").text();
				alert("One by one-\ntitle=" + title + "\nteaser=" + teaser);
				
				var data=[];
				$(xml).find("story[id=1] > *").each(
					function(){
						if(1==this.nodeType)
						{
							data[this.nodeName]=this.firstChild.nodeValue;
						}
					}
				);
				var all="";
				for(var k in data)
				{
					all+="\n"+k+"="+data[k];
				}
				alert("all at once-"+all);
				
				//you can of course get the values from
				//data.title, data.teaser, etc OR data["title"], data["teaser"]
   
                 }
             }); //close $.ajax(
         }); //
        
    </script>
 
</body>
</html>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question