Archive
Read and Parse XML web resource using Jscript – CRM 2011
I have a XML file with below content added as a web resource (new_fruitsxml) in my CRM application
<?xml version=”1.0″ encoding=”utf-8″ ?>
<Fruits>
<Fruit Code=”App”>
<Name>Apple</Name>
<Price>20</Price>
</Fruit>
<Fruit Code=”Orng”>
<Name>Orange</Name>
<Price>10</Price>
</Fruit>
</Fruits>
Below is the Jscript code to read and parse the XML.
Note – The code uses JQuery to parse the XML, so you have to refer “jquery1.4.1.min.js” file to make the code works.
function ReadXML() {
try {
var xmlPath = “../WebResources/new_fruitsxml”;
$.ajax({
type: “GET”,
url: xmlPath,
dataType: “xml”,
success: parseXML
});
} catch (e) {
alert(“Error while reading XML; Description – ” + e.description);
}
}
function parseXML(xml) {
$(xml).find(“Fruit”).each(function () {
// Read attribute
alert(“Fruit Code – ” + $(this).attr(“Code”));
// Read Nodes
alert(“Fruit Name – ” + $(this).find(“Name”).text());
alert(“Fruit Price – ” + $(this).find(“Price”).text());
});
}
- In “ReadXML()” function we read the webresource and if the read succeeds, we mention the “parseXML()” function in “success: parseXML”
- So, on success, control comes to “parseXML()” function where we parse the XML
🙂