Link to home
Start Free TrialLog in
Avatar of Carnou
CarnouFlag for United States of America

asked on

Querying an xml file using jquery

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
SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you need to get the data of the selected node :


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

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Carnou

ASKER

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!
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 >>
Thanks for the points! Have a good week-end!