4

正在将 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。

4

1 回答 1

0

我们可以看到xml文件吗?

首先,确保它以正确的文档类型开头。

例子:

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

然后,尝试将整个文档包装在一个标签中

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

  <someothertag>
    <othercontent><othercontent>
    ...
  </someothertag>
</root>
于 2013-05-10T13:19:34.803 回答