使用 Javascript 将字符串转换为 E4X XML

发布于 2024-09-26 18:09:03 字数 617 浏览 4 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2024-10-03 18:09:03

E4X 中定义的 XML 构造函数 接受 XML 字符串作为参数。所以对于你的第二个例子,尝试

var xmldata = '<books><book><title>AA</title></book></books>';
var xml = new XML(xmldata); // takes in an xml string

alert(xml.book.title);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文