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 9115385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:25:09+00:00 2026-06-17T04:25:09+00:00

I am trying to figure out how to turn some XML into a nice

  • 0

I am trying to figure out how to turn some XML into a nice readable statement. I am currently using Nokogiri, and have become stuck while trying to do the following:

I want to convert the following:

<baz>
    <option id="foo">Hello</option>
    <option id="bar">World</option>
</baz>

into the following:

baz has options: 'Foo' = 'Hello', 'Bar' = 'World'

I am currently using the following code to achieve this, but I feel like there must be a better way using join, and I can’t seem to figure it out.

optText = "baz has options:"
baznode.xpath("option").each { |opt| 
   optText << "'#{opt.xpath("@id").text}' = '#{opt.text}', "
}
optText << args[0..-3] << "\n"

Any suggestions?

  • 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-06-17T04:25:11+00:00Added an answer on June 17, 2026 at 4:25 am

    Other than maybe using collect/join I don’t see much benefit in mucking with it.

    opts = baznode.xpath("option").collect do |opt|
             "'#{opt.xpath("@id").text}' = '#{opt.text}'" }
           end.join(", ")
    optText = "baz has options: #{opts} #{args[0..-3]}\n"
    

    I might change the output to be something more like:

    baz has options:
      foo = bar
      plugh = xyzzy
    

    But that depends entirely on your needs, of course. Scanning across a line might be a bit difficult if there are a lot of options, but if they’re aren’t, the original is probably easier to parse mentally.

    If you’re doing this all over the place with multiple “things” it might make sense to transform it all into JSON/YAML and just dump that, unless you want really human-readable text.

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

Sidebar

Related Questions

Been trying to figure out a way to turn the gcide xml file to
I'm trying to figure out how to turn this object and use it for
trying to figure out why this is happening - I have an input text
I am trying to figure out how to use the UIIMagePicker or some way
I am trying to figure out how to turn the following, one-line CSV file
I am trying to figure out how to turn off the screen from within
I'm trying to figure out how to enable HTTP keep alive using the OAuth2
I'm trying to figure out how I can turn this: $('#username').blur(function(){ $.post('register/isUsernameAvailable', {username:$('#username').val()}, function(data){
This is what I am trying to figure out, I have a MC9S08AW60. It
Trying to figure out how I can do this properly. The print_r looks like

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.