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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:17:59+00:00 2026-06-06T13:17:59+00:00

I’m way new to working with XML but just had a need dropped in

  • 0

I’m way new to working with XML but just had a need dropped in my lap. I have been given an usual (to me) XML format. There are colons within the tags.

<THING1:things type="Container">
  <PART1:Id type="Property">1234</PART1:Id>
  <PART1:Name type="Property">The Name</PART1:Name>
</THING1:things>

It is a large file and there is much more to it than this but I hope this format will be familiar to someone. Does anyone know a way to approach an XML document of this sort?

I’d rather not just write a brute-force way of parsing the text but I can’t seem to make any headway with REXML or Hpricot and I suspect it is due to these unusual tags.

my ruby code:

    require 'hpricot'
    xml = File.open( "myfile.xml" )

    doc = Hpricot::XML( xml )

   (doc/:things).each do |thg|
     [ 'Id', 'Name' ].each do |el|
       puts "#{el}: #{thg.at(el).innerHTML}"
     end
   end

…which is just lifted from: http://railstips.org/blog/archives/2006/12/09/parsing-xml-with-hpricot/

And I figured I would be able to figure some stuff out from here but this code returns nothing. It doens’t error. It just returns.

  • 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-06T13:18:01+00:00Added an answer on June 6, 2026 at 1:18 pm

    As @pguardiario mentioned, Nokogiri is the de facto XML and HTML parsing library. If you wanted to print out the Id and Name values in your example, here is how you would do it:

    require 'nokogiri'
    
    xml_str = <<EOF
    <THING1:things type="Container">
      <PART1:Id type="Property">1234</PART1:Id>
      <PART1:Name type="Property">The Name</PART1:Name>
    </THING1:things>
    EOF
    
    doc = Nokogiri::XML(xml_str)
    
    thing = doc.at_xpath('//things')
    puts "ID   = " + thing.at_xpath('//Id').content
    puts "Name = " + thing.at_xpath('//Name').content
    

    A few notes:

    • at_xpath is for matching one thing. If you know you have multiple items, you want to use xpath instead.
    • Depending on your document, namespaces can be problematic, so calling doc.remove_namespaces! can help (see this answer for a brief discussion).
    • You can use the css methods instead of xpath if you’re more comfortable with those.
    • Definitely play around with this in irb or pry to investigate methods.

    Resources

    • Parsing an HTML/XML document
    • Getting started with Nokogiri

    Update

    To handle multiple items, you need a root element, and you need to remove the // in the xpath query.

    require 'nokogiri'
    
    xml_str = <<EOF
    <root>
      <THING1:things type="Container">
        <PART1:Id type="Property">1234</PART1:Id>
        <PART1:Name type="Property">The Name1</PART1:Name>
      </THING1:things>
      <THING2:things type="Container">
        <PART2:Id type="Property">2234</PART2:Id>
        <PART2:Name type="Property">The Name2</PART2:Name>
      </THING2:things>
    </root>
    EOF
    
    doc = Nokogiri::XML(xml_str)
    doc.xpath('//things').each do |thing|
      puts "ID   = " + thing.at_xpath('Id').content
      puts "Name = " + thing.at_xpath('Name').content
    end
    

    This will give you:

    Id   = 1234
    Name = The Name1
    
    ID   = 2234
    Name = The Name2
    

    If you are more familiar with CSS selectors, you can use this nearly identical bit of code:

    doc.css('things').each do |thing|
      puts "ID   = " + thing.at_css('Id').content
      puts "Name = " + thing.at_css('Name').content
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I need to clean up various Word 'smart' characters in user input, including but
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.