Created
April 13, 2012 17:23
-
-
Save djanix/2378525 to your computer and use it in GitHub Desktop.
js -> parse xml file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////////////////// | |
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