使用 Javascript 将字符串转换为 E4X XML
使用 E4X,我可以使用 javascript 轻松访问 XML 中的节点,如下所示:
<script language="javascript">
xmldata = <books><book><title>AA</title></book></books>; // notice the no string quotes
alert(xmldata.book.title);
</script>
但是,将数据返回给我的应用程序将 XML 作为字符串返回。我如何使用它访问:
<script language="javascript">
xmldata = '<books><book><title>AA</title></book></books>'; // string quote around the xmldata
alert(xmldata.book.title);
</script>
这会给我一个 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 string 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
E4X 中定义的 XML 构造函数 接受 XML 字符串作为参数。所以对于你的第二个例子,尝试
The XML constructor defined in E4X accepts an XML string as an argument. So for your second example, try