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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:50:48+00:00 2026-05-18T19:50:48+00:00

I have a ruby method that performs a complex data collection and places the

  • 0

I have a ruby method that performs a complex data collection and places the information in an XML file. However, I need to add a post-processing step to re-sort the results based on the text value of a particular element.

I have built the loops and added information to a hash in order to allow me to do this. What I’m having difficulty figuring out is how to loop through elements. I have tried examples in the REXML and XML libraries to no avail.

My XML file is structured like this:

<?xml version="1.0"?>
<Data>
  <Apps>
    <Master>
      <VehicleType>Tractor</VehicleType>
      <Make>International</Make>
      <Sub>
        <Model>Model 1600</Model>
        <Years>2003</Years>
        <Breakout1>Green</Breakout1>
        <Part1_PartType>Seat</Part1_PartType>
        <Part1>440K3</Part1>
     </Sub>
      <Sub>
        <Model>Model 1600</Model>
        <Years>2003</Years>
        <Breakout1>Blue</Breakout1>
        <Part1_PartType>Seat</Part1_PartType>
        <Part1>ABC87S</Part1>
     </Sub>
      <Sub>
        <Model>Model 1600</Model>
        <Years>2003</Years>
        <Breakout1>Green</Breakout1>
        <Part1_PartType>Seat</Part1_PartType>
        <Part1>440K4</Part1>
     </Sub>
   </Master>
 </Apps>
</Data>

I need to re-sort those elements on “Breakout1”, I just can’t figure out how to loop through the elements.

The loop body is basically made up of simple nested conditionals where I test for the name of the element, then perform a specific action. Any help is appreciated.

  • 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-18T19:50:49+00:00Added an answer on May 18, 2026 at 7:50 pm

    You can use each_element to search for and loop over elements in REXML or get_elements to return an array. Both take an xpath expression to narrow down the elements. Is this what you need?:

    require "rexml/document"
    
    XML = '<Data>
      <Apps>
        <Master>
          <VehicleType>Tractor</VehicleType>
          <Make>International</Make>
          <Sub>
            <Model>Model 1600</Model>
            <Years>2003</Years>
            <Breakout1>Green</Breakout1>
            <Part1_PartType>Seat</Part1_PartType>
            <Part1>440K3</Part1>
         </Sub>
          <Sub>
            <Model>Model 1600</Model>
            <Years>2003</Years>
            <Breakout1>Blue</Breakout1>
            <Part1_PartType>Seat</Part1_PartType>
            <Part1>ABC87S</Part1>
         </Sub>
          <Sub>
            <Model>Model 1600</Model>
            <Years>2003</Years>
            <Breakout1>Green</Breakout1>
            <Part1_PartType>Seat</Part1_PartType>
            <Part1>440K4</Part1>
         </Sub>
       </Master>
     </Apps>
    </Data>'
    
    doc = REXML::Document.new XML
    doc.root.each_element('//Sub') { |sub| puts sub.get_text('Breakout1') }
    sorted = doc.root.get_elements('//Sub').sort { |s1, s2| s1.get_text('Breakout1') <=> s2.get_text('Breakout1') }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Ruby script that generates a UTF8 CSV file remotely in a
I have a view that takes a Ruby Date, performs some formatting on it
In Ruby we have the 'bang' method capitalize! which has the strange behavior of
I was looking at the Ruby logging library Logging.logger method and have a question
I have a Ruby on Rails Website that makes HTTP calls to an external
I have a Ruby script in my Rails app that I use to load
I have a two part question Best-Practice I have an algorithm that performs some
I have a shoulda macro/method that tests controller responses for XPath conditions like so:
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
In Ruby, methods which change the object have a bang on the end: string.downcase!

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.