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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:51:11+00:00 2026-05-27T08:51:11+00:00

I’m have a document A and want to build a new one B using

  • 0

I’m have a document A and want to build a new one B using A‘s node values.

Given A looks like this…

<html>
  <head></head>
  <body>
    <div id="section0">
      <h1>Section 0</h1>
      <div>
        <p>Some <b>important</b> info here</p>
        <div>Some unimportant info here</p>
      </div>
    <div>
    <div id="section1">
      <h1>Section 1</h1>
      <div>
        <p>Some <i>important</i> info here</p>
        <div>Some unimportant info here</div>
      </div>
    <div>
  </body>
</html>

When building a B document, I’m using method a.at_css("#section#{n} h1").text to grab the data from A‘s h1 tags like this:

require 'nokogiri'

a = Nokogiri::HTML(html)

Nokogiri::HTML::Builder.new do |doc|
  ...
  doc.h1 a.at_css("#section#{n} h1").text
  ...
end

So there are three questions:

  1. How do I grab the content of <p> tags preserving tags inside
    <p>?

    Currently, once I hit a.at_css("#section#{n} p").text it
    returns a plain text, which is not what’s needed.

    If, instead of .text I hit .to_html or .inner_html, the html appears escaped. So I get, for example, &lt;p&gt; instead of <p>.

  2. Is there any known true way of assigning nodes at the document building stage? So that I wouldn’t dance with text method at all? I.e. how do I assign doc.h1 node with value of a.at_css("#section#{n} h1") node at building stage?

  3. What’s the profit of Nokogiri::Builder.with(...) method? I wonder if I can get use of it…

  • 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-27T08:51:12+00:00Added an answer on May 27, 2026 at 8:51 am
    1. How do I grab the content of <p> tags preserving tags inside <p>?

      Use .inner_html. The entities are not escaped when accessing them. They will be escaped if you do something like builder.node_name raw_html. Instead:

      require 'nokogiri'
      para = Nokogiri.HTML( '<p id="foo">Hello <b>World</b>!</p>' ).at('#foo')
      
      doc = Nokogiri::HTML::Builder.new do |d|
        d.body do
          d.div(id:'content') do
            d.parent << para.inner_html
          end
        end
      end
      
      puts doc.to_html
      #=> <body><div id="content">Hello <b>World</b>!</div></body>
      
    2. Is there any known true way of assigning nodes at the document building stage?

      Similar to the above, one way is:

      puts Nokogiri::HTML::Builder.new{ |d| d.body{ d.parent << para } }.to_html
      #=> <body><p id="foo">Hello <b>World</b>!</p></body>
      

      Voila! The node has moved from one document to the other.

    3. What’s the profit of Nokogiri::Builder.with(...) method?

      That’s rather unrelated to the rest of your question. As the documentation says:

      Create a builder with an existing root object. This is for use when you have an existing document that you would like to augment with builder methods. The builder context created will start with the given root node.

      I don’t think it would be useful to you here.

    In general, I find the Builder to be convenient when writing a large number of custom nodes from scratch with a known hierarchy. When not doing that you may find it simpler to just create a new document and use DOM methods to add nodes as appropriate. It’s hard to tell how much hard-coded nodes/hierarchy your document will have versus procedurally created.

    One other, alternative suggestion: perhaps you should create a template XML document and then augment that with details from the other, scraped HTML?

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.