jose1221
jose1221

Reputation: 263

The document \"<?xml version='1.0'?>\\n\" does not have a valid root

I am new to parsing the xml in the ruby and I am stuck with an issue. I'll try my best to explain. I get the below response from an api

"PK\x03\x04\x14\x00\b\b\b\x00,\x18ET\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1D\x00\x00\x00"

I believe it is .tds format. I am trying to parse it into a valid xml so this is what I tried.

xml = Nokogiri::XML(response) which gives me #<Nokogiri::XML::Document:0xf744 name="document">

Then I tried to do Hash.from_xml(xml.to_xml)

But this throws error The document \"<?xml version='1.0'?>\\n\" does not have a valid root

Any idea what am I missing here?

Upvotes: 0

Views: 249

Answers (1)

hmdne
hmdne

Reputation: 504

This string starts with "PK", which are initials of Phil Katz, author of the ZIP format. Which means it's a ZIP file. There are certain formats that are ZIP files, but actually follow some further structure conventions, like Java JAR files, but also all OpenDocument formats like .ods, .odt and all MS Office Open XML formats.

Since you are probably expecting an .ods file... While you could unzip it and then use Nokogiri to parse the XML, there's a better way to proceed.

There's an interesting Gem called Roo, that supports all most common spreadsheet formats and produces a nice Ruby API to deal with them: https://github.com/roo-rb/roo

I would recommend you to save the string to a temporary file and then open it with Roo.

Upvotes: 1

Related Questions