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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:19:33+00:00 2026-05-31T09:19:33+00:00

I have the following XML consumed from a REST API: <dataitems> <dataitem colour=null> <value>

  • 0

I have the following XML consumed from a REST API:

<dataitems>
  <dataitem colour="null">
     <value>
        <label>Intel</label>
        <count>43</count>
     </value>
     <value>
        <label>AMD</label>
        <count>39</count>
     </value>
     <value>
        <label>ARM</label>
        <count>28</count>
     </value>
  </dataitem>
</dataitems>

I would like to search for the text in the <label> tag and display the matching value for <count> in a table.

In the controller I have: @post_count = Nokogiri::XML(Post.item_data.to_xml).

In the view I’m not sure whether I need to use @post_count.xpath or @post_count.search.

Can someone please point me in the right direction on the right method and syntax for it?

Thanks in advance.

  • 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-31T09:19:34+00:00Added an answer on May 31, 2026 at 9:19 am

    Although I’m not sure what information you are looking for, I have a few suggestions.

    1) If you know the value in the element before you do your searching:

    doc = Nokogiri.XML(open(source_xml))
    
    # Assuming there is only one of each label
    node = doc.xpath('//label[text()="Intel"]').first
    count = node.next_element.text
    
    # or if there are many of each label
    nodes = doc.xpath('//label[text()="Intel"]')
    nodes.each {|node|
      count = node.next_element.text
      # do something with count here
    }
    

    2) Assuming that you don’t know the names within the tag in advance

    doc = Nokogiri.XML(open(source_xml))
    labels = {}
    doc.xpath('//label').each {|node|
      labels[node.text] = node.next_element.text
    }
    
    # labels => {"Intel"=>"43", "AMD"=>"39", "ARM"=>"28"}
    

    I personally like the second solution better because it gives you a clean hash, but I prefer to work with hashes and arrays as quickly as possible.

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

Sidebar

Related Questions

I have the following xml that's sent to me from a web service. I'm
I have the following XML: <InterSection criteria=Microsoft-ASP>value</Intersection> <InterSection criteria=Microsoft-MVC>value</Intersection> <InterSection criteria=HP-MVC>value</Intersection> I need to
We have a requirement to fetch information from the following API http://www.viralheat.com/developer/sentiment_api#method1 which can
I have following xml file: <ab> <![CDATA[ <table> <tbody> <tr> <th>abcdef</th> </tr> <tr> <p>
I have following xml as a response to a service not i want to
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
I have the following xml I'd like to deserialize into a class <?xml version=1.0
I have the following XML document: <projects> <project> <name>Shockwave</name> <language>Ruby</language> <owner>Brian May</owner> <state>New</state> <startDate>31/10/2008
I have the following XML <?xml version=1.0?> <FileHeader xmlns=urn:schemas-ncr-com:ECPIX:CXF:FileStructure:020001 VersionNumber=020001 TestFileIndicator=P CreationDate=13012009 CreationTime=172852 FileID=0000000001

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.