R中的XML到JSON转换
发布时间:2020-12-16 22:48:20 所属栏目:百科 来源:网络整理
导读:我用’ 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 Ita
我用’
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","author": "J K. Rowling","price": "29.99",".attrs": { "category": "children" } },"book": { "title": { "text": "Learning XML","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" },"price": "30.00" },{ "-category": "children","#text": "Harry Potter" },"price": "29.99" },{ "-category": "web","#text": "Learning XML" },"price": "39.95" } ] } } 期待解决方案.任何帮助表示赞赏. 解决方法
正如迈克尔所说,这不是一个好主意.但是,我们不太可能说服您,因为没有自动转换意味着要做一些工作以确保一致性和完全可重复性.
既然你似乎喜欢那个网站,我很确定它使用 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" ## },## "_text": "Harry Potter" ## },## "author": { ## "_text": "J K. Rowling" ## },## "price": { ## "_text": "29.99" ## } ## },## { ## "_attributes": { ## "category": "web" ## },## "_text": "Learning XML" ## },## "author": { ## "_text": "Erik T. Ray" ## },## "year": { ## "_text": "2003" ## },## "price": { ## "_text": "39.95" ## } ## } ## ] ## } ## } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- Flex FileReference 在Firefox 浏览器情况下的异
- Node-webkit中使用sqlite3
- ruby-on-rails – 在rails上的ruby中使用faceboo
- 深入入门正则表达式(java) - 匹配原理 - 1 - 引
- 关于XML字符串和XML Document之间的转换
- ruby-on-rails – 从Ubuntu中的ROR应用程序连接到
- Swift 部署到服务器—添加忽略文件
- 设计模式六大原则(2):里氏替换原则
- Swift – 将Int64转换为AnyObject for NSMutable
- ruby-on-rails – Paperclip在AWS Elastic Beans
热点阅读