Skip to content

Instantly share code, notes, and snippets.

@djanix
Created April 13, 2012 17:23
Show Gist options
  • Save djanix/2378525 to your computer and use it in GitHub Desktop.
Save djanix/2378525 to your computer and use it in GitHub Desktop.
js -> parse xml file
/////////////////////////////////////////////////////
XML DOCUMENT EXAMPLE
/////////////////////////////////////////////////////
<?xml version="1.0"?>
<items>
<item>
<headline>title</headline>
<description>text</description>
<image>content_logo.png</image>
</item>
<item>
<headline>title</headline>
<description>text</description>
<image>content_logo.png</image>
</item>
</items>
/////////////////////////////////////////////////////
END XML DOCUMENT EXAMPLE
/////////////////////////////////////////////////////
function getXml() {
var filePath = 'pathToFile/document.xml';
$.ajax({
type: "GET",
url: filePath,
dataType: "xml",
success: parseXml
});
}
function parseXml(obj) {
var xml = $(obj).find('items');
var items = new Array();
$(xml).find('item').each(function(index) {
items[index] = new Array();
$(this).children().each(function() {
items[index][(this).nodeName] = $(this).text();
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment