[英]XML to JSON Conversion in R
我已经尝试了带有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"
}
]
}
}
期待解决方案。 任何帮助表示赞赏。
正如迈克尔所说,这不是一个好主意。 但是,我们不太可能说服您,因为没有自动转换意味着要进行工作以确保一致性和完全可重复性。
由于您似乎喜欢该网站,所以我很确定它使用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"
## }
## }
## ]
## }
## }
第一点:没有将XML转换为JSON或反之亦然的明确的“正确”方法。 有许多不同的库可以执行此操作,并且它们的执行方法也有所不同。 不幸的是,选择了一个实际上会生成不正确的JSON的代码。
第二点:如果您知道开始使用的是XML以及想要结束使用的JSON,那么您不太可能会找到可以进行所需转换的库。
因此,您将不得不进行一些手动调整。 您可以潜在地在转换之前对输入进行预处理,或在转换之后对输出进行后处理,或者可以“手动”完成全部操作。
在XSLT中(或可能在R中)手工完成整个过程实际上并不那么困难,但是我对此并不熟悉。
我个人的选择是使用XSLT 3.0将XML转换为映射和数组,然后将结果序列化为JSON:
<xsl:output method="json" indent="yes">
<xsl:template match="booklist">
<xsl:map-entry key="'booklist'">
<xsl:map-entry key="'book'">
<saxon:array>
<xsl:for-each select="book">
<saxon:array-member>
<xsl:map>
<xsl:map-entry key="'-category'" select="@category"/>
<xsl:map-entry key="'title'">
<xsl:apply-templates select="title"/>
</xsl:map-entry>
<xsl:map-entry key="'author'" select="author"/>
<xsl:map-entry key="'year'" select="year"/>
<xsl:map-entry key="'price'" select="price"/>
</xsl:map>
</saxon:array-member>
</xsl:for-each>
</saxon:array>
</xsl:map-entry>
</xsl:map-entry>
</xsl:template>
<xsl:template match="title">
<xsl:map-entry key="'-lang'" select="@lang"/>
<xsl:map-entry key="'#text'" select="string(.)"/>
</xsl:template>
当然,您也可以尝试概括其中一些规则,例如使模板规则匹配所有属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.