将XML转换为JavaScript对象

我有一个 XML文件,其中包含以下内容

<directory>
  <app>
    <title>Carrot</title>
    <url>www.carrot.com</url>
  </app>
  <app>
    <title>Cucumber</title>
    <url>www.cucumber.com</url>
  </app>
</directory>

假设我能够读取它并将内容存储为字符串:

s = '<directory><app><title>Carrot</title><url>www.google.com</url></app><app><title>Cucumber</title><url>www.cucumber.com</url></app></directory>';

如何将其转换为JavaScript对象,如下所示?

{
  "directory": {
    "app": [
      { "title": "Carrot","url": "www.carrot.com" },{ "title": "Cucumber","url": "www.cucumber.com" }
    ]  
  }
}

解决方法

我用这个插件http://www.thomasfrank.se/xml_to_json.html

它总是为我带来魅力.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念