Solved

Querying an xml file using jquery

Posted on 2010-09-05
7
661 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now