Hi!
I want to be able to get CDATA from an XML file using XPATH.
I use 2 files, books.html and books.xml.
books.html should print out the CDATA.
This is the code that I'm using:
books.html
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
xml=loadXMLDoc("books.xml");
path="/bookstore/book/author/text()"
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);
for (i=0;i<nodes.length;i++)
{
document.write(nodes[i].childNodes[0].nodeValue);
document.write("<br />");
}
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();
while (result)
{
document.write(result.childNodes[0].nodeValue);
document.write("<br />");
result=nodes.iterateNext();
}
}
</script>
</body>
</html>
books.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"><![CDATA[Everyday Italia]]></title>
<author><![CDATA[Giada De Laurentiis]]></author>
<year><![CDATA[2005]]></year>
<price><![CDATA[30.00]]></price>
</book>
</bookstore>
How can this be done?