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

时间:2022-01-02 19:41:36

am converting xml file to json, it throws error

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

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

am using json gem to conver, my code is

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

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

how to convert xml to json in rails.

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

1 个解决方案

#1


0  

Can we see the xml file?

我们可以看到xml文件吗?

First of all, make sure it begins with the right doctype.

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

example:

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

Then, try to wrap the entire document in a single tag

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

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

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

#1


0  

Can we see the xml file?

我们可以看到xml文件吗?

First of all, make sure it begins with the right doctype.

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

example:

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

Then, try to wrap the entire document in a single tag

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

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

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