Reputation: 11
I am new to jQuery and am trying to read in an xml file. I have created a simple html file as shown below and in the same directory have an xml file called allTasks.xml. I see the alert when the document is loaded and I see the second alert 'Here Now'. However when I try and use the find method I cannot traverse the xml correctly. Is there a good way to test if I am actually loading the xml file at all? Also should this work in a html file or should I be using php?
Any help would be greatly received !
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Read XML File</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
alert('Loaded');
var $myXML;
$.get('allTasks.xml', function(data) {
$myXML = data; ;
}, 'xml');
alert('Here now ');
alert($myXML.find("Tasks>Task>TaskId").text());
});
</script>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header" class="ui-bar ui-bar-b" style="height:50px;">
</div> <!-- end of header -->
<div data-role="content">
<div id='names'></div>
</div> <!-- end of content -->
</div> <!-- end of page div -->
</body>
</html>
xml
<TaskList>
<Tasks>
<Task>
<TaskId>1</TaskId>
<Name>Name of Task 1</Name>
</Task>
<Task>
<TaskId>2</TaskId>
<Name>Name of Task 2</Name>
</Task>
</Tasks>
</TaskList>
Upvotes: 1
Views: 3849
Reputation: 3939
$.get('allTasks.xml', function(data) {
var parsed = $(data);
alert( parsed.find('Tasks>Task>TaskId').text() );
}
Upvotes: 1