Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 842109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:54:28+00:00 2026-05-15T05:54:28+00:00

I am currently doing some XML parsing and I’ve chosen to use Hpricot because

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:54:28+00:00Added an answer on May 15, 2026 at 5:54 am

    My solution was to just replace the literal ‘\n’ characters with line breaks and remove the extra punctuation by simply adding two gsubs that looked like the following:

    f.write( dict.to_original_html.gsub('\n', "\n").gsub('" ,"', '') )
    

    I don’t know why I didn’t see this before. Like I said, it might be an easy answer that I wasn’t seeing and that’s exactly how it turned out. Thanks for all the answers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using JDOM for doing some simple XML parsing, and it seems like
I am currently parsing some xml that looks like this <Rows> <Row MANUFACTURERID=76 MANUFACTURERNAME=Fondont
So currently I'm doing some XML-> XSLT-> (HTML5/CSS3) work. Right now I have a
I am currently trying to parse some xml data into an NSDictionary . This
I'm new to tinkering with app.config and xml, and am currently doing some refactoring
I'm currently attempting to unmarshal some existing XML into a few classes I have
I am currently running IE8 and am doing some testing with WCF data services.
I'm currently doing some testing to determine the performance implications of including an index
I'm currently doing some last-measure optimizations, mostly for fun and learning, and discovered something
I am currently doing some network programming and had a couple questions concerning timeouts.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.