gpt4 book ai didi

r - R 中的 XML 到 JSON 转换

转载 作者:数据小太阳 更新时间:2023-10-29 02:11:50 26 4
gpt4 key购买 nike

我试过使用“XML”包的library(rjson)。首先,我解析了 XML 并使用 XML::xmlToList() 将其转换为列表,然后使用 rjson 包中的 toJSON() 将其转换为 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"
}
]
}
}

期待解决方案。感谢您的帮助。

最佳答案

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

因为您似乎喜欢那个网站,所以我非常确定它使用 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"
## }
## }
## ]
## }
## }

关于r - R 中的 XML 到 JSON 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50383745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com