R中的XML到JSON转换

我用’
XML‘包试过了库(rjson).首先,我使用XML :: xmlToList()解析XML并将其转换为列表,然后使用rjson包中的to
JSON()将其转换为
JSON.

我的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"
      }
    ]
  }
}

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

最佳答案 正如迈克尔所说,这不是一个好主意.但是,我们不太可能说服您,因为没有自动转换意味着要做一些工作以确保一致性和完全可重复性.

既然你似乎喜欢那个网站,我很确定它使用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"
##         }
##       }
##     ]
##   }
## }
点赞