首页 > 解决方案 > R中的XML到JSON转换

问题描述

我已经尝试过library(rjson)使用“XML”包。首先,我解析 XML 并将其转换为列表,然后使用rjson 包将其XML::xmlToList()转换为 JSON 。toJSON()

我的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

我的源代码:

rm(list = ls())
library(XML)
library(rjson)

xml_parse<-xmlParse(file = "path/book")
xml_root <- xmlRoot(xml_parse)
xml_list <- xmlToList(xml_root,addAttributes = T, simplify = F)
#rjson package
xml_rjson <-toJSON(xml_list)
cat(xml_rjson)

从 rjson 转换的 JSON 文件:

  {
  "book": {
    "title": {
      "text": "Everyday Italian",
      ".attrs": {
        "lang": "en"
      }
    },
    "author": "Giada De Laurentiis",
    "year": "2005",
    "price": "30.00",
    ".attrs": {
      "category": "cooking"
    }
  },
  "book": {
    "title": {
      "text": "Harry Potter",
      ".attrs": {
        "lang": "en"
      }
    },
    "author": "J K. Rowling",
    "year": "2005",
    "price": "29.99",
    ".attrs": {
      "category": "children"
    }
  },
  "book": {
    "title": {
      "text": "Learning XML",
      ".attrs": {
        "lang": "en"
      }
    },
    "author": "Erik T. Ray",
    "year": "2003",
    "price": "39.95",
    ".attrs": {
      "category": "web"
    }
  }
}

由于重复键“book”并且没有根名称“bookstore”,这显然是错误的。

理想的 JSON 文件应该是这样的:

{
  "bookstore": {
    "book": [
      {
        "-category": "cooking",
        "title": {
          "-lang": "en",
          "#text": "Everyday Italian"
        },
        "author": "Giada De Laurentiis",
        "year": "2005",
        "price": "30.00"
      },
      {
        "-category": "children",
        "title": {
          "-lang": "en",
          "#text": "Harry Potter"
        },
        "author": "J K. Rowling",
        "year": "2005",
        "price": "29.99"
      },
      {
        "-category": "web",
        "title": {
          "-lang": "en",
          "#text": "Learning XML"
        },
        "author": "Erik T. Ray",
        "year": "2003",
        "price": "39.95"
      }
    ]
  }
}

期待解决方案。任何帮助表示赞赏。

标签: rjsonxml

解决方案


正如迈克尔所说,这不是一个好主意。但是,我们不太可能让您相信这一点,因为没有自动转换意味着要努力确保一致性和完全可重复性。

由于您似乎喜欢那个网站,我很确定它使用xml-js或非常接近它的东西,所以我拼凑了一个小的 V8 包装器包:https ://github.com/hrbrmstr/blackmagic

该功能有大量潜在的参数设置,xml_to_json()因此在任何“但它不会自动为我执行 xyz”评论之前查看它。

devtools::install_github("hrbrmstr/blackmagic")

'<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>' -> books

cat(xml_to_json(books, spaces = 2, compact = TRUE, ignoreDeclaration = TRUE))

## {
##   "bookstore": {
##     "book": [
##       {
##         "_attributes": {
##           "category": "cooking"
##         },
##         "title": {
##           "_attributes": {
##             "lang": "en"
##           },
##           "_text": "Everyday Italian"
##         },
##         "author": {
##           "_text": "Giada De Laurentiis"
##         },
##         "year": {
##           "_text": "2005"
##         },
##         "price": {
##           "_text": "30.00"
##         }
##       },
##       {
##         "_attributes": {
##           "category": "children"
##         },
##         "title": {
##           "_attributes": {
##             "lang": "en"
##           },
##           "_text": "Harry Potter"
##         },
##         "author": {
##           "_text": "J K. Rowling"
##         },
##         "year": {
##           "_text": "2005"
##         },
##         "price": {
##           "_text": "29.99"
##         }
##       },
##       {
##         "_attributes": {
##           "category": "web"
##         },
##         "title": {
##           "_attributes": {
##             "lang": "en"
##           },
##           "_text": "Learning XML"
##         },
##         "author": {
##           "_text": "Erik T. Ray"
##         },
##         "year": {
##           "_text": "2003"
##         },
##         "price": {
##           "_text": "39.95"
##         }
##       }
##     ]
##   }
## }

推荐阅读