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

  • Home
  • SEARCH
  • 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 7499957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:59:54+00:00 2026-05-29T19:59:54+00:00

I am using Ruby to retrieve an XML document with the following format: <project>

  • 0

I am using Ruby to retrieve an XML document with the following format:

<project>
  <users>
   <person>
     <name>LUIS</name>
   </person>
   <person>
     <name>JOHN</name>
   </person>
  </users>
</project>

I want to know how to produce the following result, with the tags concatenated:

<project>
    <users>
      <person>
        <name>LUIS JOHN</name>
      </person>
    </users>
</project>

Here is the code I am using:

file = File.new( "proyectos.xml" )
doc3 = Nokogiri::XML(file)
a=0

@participa = doc3.search("person")
@participa.each do |i|

     @par = @participa.search("name").map { |node| node.children.text }

     @par.each do |i|
         puts @par[a]
         puts '--'
         a = a + 1
    end 

end
  • 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-29T19:59:56+00:00Added an answer on May 29, 2026 at 7:59 pm

    Rather than supply code, here’s how to fish:

    To parse your XML into Nokogiri, which I recommend highly:

    require 'nokogiri'
    
    doc = Nokogiri::XML(<<EOT)
    <project>
      <users>
       <person>
         <name>LUIS</name>
       </person>
       <person>
         <name>JOHN</name>
       </person>
      </users>
    </project>
    EOT
    

    That gives you a doc variable which is the DOM as a Nokogiri::XML::Document. From that you can search, either for matching nodes or a particular node. search allows you to pass an XPath or CSS accessor to locate what you are looking for. I recommend CSS for most things because it is more readable, but XPath has some great tools to dig into the structure of your XML, so often I end up with both in my code.

    So, doc.at('users') is the CSS accessor to find the first users node. doc.search('person') will return all nodes matching the person tag as a NodeSet, which is basically an array which you can enumerate or loop over.

    Nokogiri has a text method for a node that lets you get the text content of that node, including all the carriage-returns between nodes that would normally be considered formatting in the XML as it flows down the document. When you have the text of the node, you can apply the normal Ruby string processing commands, such as strip, squish, chomp, etc., to massage the text into a more usable format.

    Nokogiri also has a children= method which lets you redefine the child nodes of a node. You can pass in a node you’ve created, a NodeSet, or even the text you want rendered into the XML at that point.

    In a quick experiment, I have code that does what you want in basically four lines. But, I want to see your work before I share what I wrote.

    Finally, puts doc.to_xml will let you easily see if your changes to the document were successful.


    Here’s how I’d do it:

    require 'nokogiri'
    
    doc = Nokogiri::XML(<<EOT)
    <project>
      <users>
       <person>
         <name>LUIS</name>
       </person>
       <person>
         <name>JOHN</name>
       </person>
      </users>
    </project>
    EOT
    

    The XML is parsed into a DOM now. Search for the users tags, then locate the embedded name tags and extract the text from them. Join the results into a single space-delimited string. Then replace the children of the users tag with the desired results:

    doc.search('users').each do |users|
      user_names = users.search('name').map(&:text).join(' ')
      users.children = "<person><name>#{ user_names }</name></person>"
    end
    

    If you output the resulting XML you’ll get:

    puts doc.to_xml
    
    <?xml version="1.0"?>
    <project>
      <users><person><name>LUIS JOHN</name></person></users>
    </project>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Ruby I'm trying to split the following text with a Regex ~foo\~\=bar =cheese~monkey
Using ruby regexp I get the following results: >> 'foobar'[/o+/] => oo >> 'foobar'[/o*/]
I am using Ruby on Rails. I want to create a filter field on
In Ruby, how can I traverse an arbitrary document retrieved from a collection using
I am using Ruby on Rails 3 and I would like to retrieve a
I'm trying to retrieve every external link of a webpage using Ruby. I'm using
I am using Ruby on Rails 3.0.7 and I would like to know how
I am using Ruby on Rails 3.0.7 and I would like to know what
Using Ruby + regex, given: starting-middle+31313131313@mysite.com I want to obtain just: 31313131313 ie, what
I am using Ruby on Rails 3 and I am trying to retrieve some

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.