将XML转换为JavaScript对象

前端之家收集整理的这篇文章主要介绍了将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

它总是为我带来魅力.

猜你在找的XML相关文章