开发者

Converting string to E4X XML using Javascript

开发者 https://www.devze.com 2023-01-19 10:21 出处:网络
Using E4X, i can easily access nodes in XML using javascript as follows: <script language=\"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);
0

精彩评论

暂无评论...
验证码 换一张
取 消