繁体   English   中英

对json show的哈希xml解析没有有效的root

[英]Hash xml parse to json shows does not have valid root

我正在将xml文件转换为json,它会抛出错误

The document "some xml data" does not have a valid root. 

我使用json gem来转换,我的代码是

 require 'json'
 scheduledoc = "xmlfile"
 scheduleData = Hash.from_xml(scheduleDoc).to_json
 puts "schedule json #{scheduleData}

如何在rails中将xml转换为json。

我们可以看到xml文件吗?

首先,确保它以正确的doctype开头。

例:

<?xml version="1.0" encoding="utf-8"?>

然后,尝试将整个文档包装在单个标记中

<?xml version="1.0" encoding="utf-8"?>
<root>
  <sometag></sometag>
  <sometag></sometag>

  <someothertag>
    <othercontent><othercontent>
    ...
  </someothertag>
</root>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM