Solved

Querying an xml file using jquery

Posted on 2010-09-05
7
669 Views
Last Modified: 2012-05-10
Another newbie-to-jquery question.  I can code C# and Flash ActionScript 3, but I'm still spinning up on jquery...

I'm trying to load an XML file and access specific data elements within it.  I'm following the example at http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery and others that all report the same idea.  I'm missing something, and I'm sure it's obvious.

Here's my code, and the xml I'm testing with.
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Test search</title>
		<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
		<script type="text/javascript">

			function searchXml( xml )
			{
				alert( 'searchXml.  xml = ' + xml );
				//search = '*';
				search = "[name='h'][blah='feh']";
				xml.find(search).each( function() { alert( 'yippie' ); } );
				xml.find(search).each( function() { alert( $(this) ); } );
			}
			
			function searchClicked()
			{
				alert( 'Search clicked' );
				var options = 
				{
					type: 'GET',
					url: 'test.xml',
					dataType: ($.browser.msie) ? 'tt' : 'xml',
					success: searchXml
				};
				$.ajax( options);
			};
				
			function documentReady()
			{
				$('#seqSearch').click( searchClicked );
			};
				
			$(document).ready( documentReady );
        </script>
        
</head>

<body>
<div id="leftPane" class="ui-widget-content">
	<div id="leftPanel">
		<div>
			<form action=""> 
				<button id="seqSearch">Search</button>
			</form>
		</div>
	</div>
</div>				
</body>
</html>

Open in new window


 
<?xml version="1.0" encoding="UTF-8"?>
<abc>
	<def>def 1</def>
	<def>def 2</def>
	<def>def 3</def>
	<def name="h">named def h</def>
	<def name="i">named def i</def>
	<def name="h" blah="feh">named def h feh</def>
	<def>
		<def>embedded</def>
		<ghi>ghi</ghi>
		<ghi name="g">ghi named g</ghi>
	</def>
</abc>

Open in new window


In IE, the alert statements at the top of both the searchClicked statement and the searchXml statement fire off.  The xml that is displayed is correct.  I've not ever gotten one of the find(..).each blocks to work, whether I search for * or anything else.  Ideally, I want to find something along the lines of what the code is searching for right now.  When I run in Firefox, I never even see the statement from searchXml.  searchClicked fires, but searchXml apparently does not.

So, two questions:

1. What am I doing wrong for search not to work?  I'm sure it's a simple thing I just haven't caught on to.
2. Why doesn't this code run the same way in firefox.

Files are attached at the bottom for easy testing/playing with.  Thanks for your time!

testFind.html
test.xml
0
Comment
Question by:Carnou
  • 4
  • 2
7 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
ID: 33607881
You need an jquery object to use jquery function, so replace : xml.find
By : $(xml).find

You "button" object post the form, you need to prevent this by using, for example, e.preventDefault() or use a simple input button (type) : http://api.jquery.com/event.preventDefault/

           function searchClicked(e)
            {
                e.preventDefault();



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Test search</title>
		<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
		<script type="text/javascript">

			function searchXml( xml )
			{
				alert( 'searchXml.  xml = ' + xml );
				//search = '*';
				search = "[name='h'][blah='feh']";
				$(xml).find(search).each( function() { alert( 'yippie' ); } );
				$(xml).find(search).each( function() { alert( $(this) ); } );
			}

			function searchClicked(e)
			{
				e.preventDefault();
				alert( 'Search clicked' );
				var options = 
				{
					type: 'GET',
					url: 'test.xml',
					dataType: ($.browser.msie) ? 'tt' : 'xml',
					success: searchXml,
				};
				$.ajax(options);
			};
				
			function documentReady()
			{
				$('#seqSearch').click( searchClicked );
			};
				
			$(document).ready( documentReady );
        </script>
        
</head>

<body>
<div id="leftPane" class="ui-widget-content">
	<div id="leftPanel">
		<div>
			<form action=""> 
				<button id="seqSearch">Search</button>
			</form>
		</div>
	</div>
</div>				
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33607913
if you need to get the data of the selected node :


$(xml).find(search).each( function() { alert( $(this).text() ); } );

Open in new window

0
 
LVL 10

Accepted Solution

by:
Carnou earned 0 total points
ID: 33609961
Hi, Lekim971,
Thanks for your help.  The preventDefault function works like a charm - it now loads in Firefox, and changing it to $(xml) makes it run the find command properly.  So, it works fine in Firefox, and your second posting will be a great help in my next steps in debugging.
But in IE, I still have issues.  It doesn't get any further than it did before - it gets into the searchXml function, but find.each comes up with nothing.
As I was searching for my reference as to why I had special case code for IE, I found the remaining solution at http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery.  Namely, the searchXml function should look like the attached code.
Yay special casing for Microsoft.
Between your help and this site, I've now gotten past this step.
Thanks for getting me there!

function searchXml( xml )
			{
				alert( 'searchXml.  xml = ' + xml );
				
				if ($.browser.msie)
				{
					var xml_doc = new ActiveXObject( "Microsoft.XMLDOM" );
					xml_doc.loadXML( xml );
					xml = xml_doc;
				}
				//search = '*';
				search = "[name='h'][blah='feh']";
				$(xml).find(search).each( function() { alert( 'yippie' ); } );
				$(xml).find(search).each( function() { alert( $(this) ); } );
			}

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 10

Author Comment

by:Carnou
ID: 33609984
Oy.
Note to mods, cause this will pop up eventually:
I intended to close this question with full points to ID 33607881 and a ranking of A, but with my message 33609961 as an assisted, zero-point solution.
Thanks!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33610676
Question not closed as the asker would want it :

<< I intended to close this question with full points to ID 33607881 and a ranking of A >>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33652782
Thanks for the points! Have a good week-end!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 79
jQuery detect if it is a mobile device 3 173
Replace &lt; with < 14 59
ajaxSubmit is giving me an error 1 34
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now