I have an xml file which I imported in my html page like this:
<script type="text/xml" src="Categories.xml" id="categoriesXML">
</script>
And using my jQuery code I want to read this xml file and parse it to do some stuff with its data, so I tried to write something like this:
var xml = $('#categoriesXML').text();
$(xml).find("Category").each(function () {
alert($(this).find("Title").text());
});
But what I got is that the xml variable is empty.
I also tried to write it:
var xml = $('#categoriesXML');
but again I can’t get it work.
The xml file content:
<?xml version="1.0" encoding="utf-8" ?>
<Categories>
<Category>
<Title>CAT1</Title>
<SubCategories>
<SubCategory>
<Title>SUB1</Title>
</SubCategory>
</SubCategories>
</Category>
</Categories>
So how to read this xml file this way, without using ajax?
Why I want it without ajax:
Because I want this to happen on the page load, and no need to start getting the file after my page loads which will show my page nearly empty at first load, and will wait till the ajax response with the file which may be very big in some cases.
I think you will need to load the XML with AJAX.
Here is a summary of the way to do it