Ruby - Writing Hpricot data to a file

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-06-02T18:33:38Z Indexed on 2010/06/02 23:54 UTC
Read the original article Hit count: 175

Filed under:
|
|

Hey everyone,

I am currently doing some XML parsing and I've chosen to use Hpricot because of it's ease of use and syntax, however I am running into some problems. I need to write a piece of XML data that I have found out to another file. However, when I do this the format is not preserved. For example, if the content should look like this:

<dict>
  <key>item1</key><value>12345</value>
  <key>item2</key><value>67890</value>
  <key>item3</key><value>23456</value>
</dict>

And assuming that there are many entries like this in the document. I am iterating through the 'dict' items by using

hpricot_element = Hpricot(xml_document_body)
f = File.new('some_new_file.xml')
(hpricot_element/:dict).each { |dict| f.write( dict.to_original_html ) }


After using the above code, I would expect that the output look like the following exactly like the XML shown above. However to my surprise, the output of the file looks more like this:

<dict>\n", "    <key>item1</key><value>12345</value>\n", "    <key>item2</key><value>67890</value>\n", "    <key>item3</key><value>23456</value\n", "  </dict>


I've tried splitting at the "\n" characters and writing to the file one line at a time, but that didn't seem to work either as it did not recognize the "\n" characters. Any help is greatly appreciated. It might be a very simple solution, but I am having troubling finding it. Thanks!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about file-io