Converting string to E4X XML using Javascript
Using E4X, i can easily access nodes in XML using javascript as follows:
<script language="javascript">
xmldata = <books><book><title>AA</title></book></books>; // notice the no st开发者_如何学Pythonring quotes
alert(xmldata.book.title);
</script>
However, the application returning the data to me is returning XML as string. How can i access using this :
<script language="javascript">
xmldata = '<books><book><title>AA</title></book></books>'; // string quote around the xmldata
alert(xmldata.book.title);
</script>
This would give me a javascript error. Can someone please tell me how i can achieve the earlier result ?
The XML constructor defined in E4X accepts an XML string as an argument. So for your second example, try
var xmldata = '<books><book><title>AA</title></book></books>';
var xml = new XML(xmldata); // takes in an xml string
alert(xml.book.title);
精彩评论