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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:59:00+00:00 2026-05-21T11:59:00+00:00

I am using nokogiri and ruby to script some deployment scripts and I need

  • 0

I am using nokogiri and ruby to script some deployment scripts and I need to update the file from many sources of information. The first file I loop through I just do a simple replace with gsub

File.open(@sql_file, 'w') { |file| file.puts text.gsub(/#{find}/, replace.to_s) }

That works great for the first file but for the second time I need to parse out that text I replaced initially and potentially update it. Using Nokogiri I have come up with the following:

def write_changes(find, replace)
  text = ''
  text = File.read(@sql_file)
  if text =~ /#{find}/
    File.open(@sql_file, 'w') do |file| 
      file.puts text.gsub(/#{find}/, replace.to_s)
    end
  else
    xml = Nokogiri::XML.parse(replace)
    element xml.at_xpath('//items')
  end
end

Maybe not very ruby:ish but it works. The problem here though is that I need to parse out

<items id="">
  <item />
</items>

from the text in the file so that I can use Nokogiri to update that text and later replace it with my changes.

So basically I guess I need to find the startindex of my items tag where the id matches and then cut out everything from <items id=""> to </items>.

Does it make sense? Should I clarify further?

EDIT 1. I have something somewhat working but I can’t figure out the last part.

    original = text[begin_index, end_index]
    xml2 = Nokogiri::XML.parse(original)
    update_element_values(xml, xml2)
    add_missing_elements(xml, xml2)
    # text[begin_index, end_index] = xml2.root.to_s
    text.insert(begin_index, xml2.root.to_s)
    File.open(@sql_file, 'w') { |file| file.puts text }

if I uncomment the line that does a replace between indexes I get some corrupt chopped up result with most of it missing.

If I use the insert version I get duplicates for every file I try to merge. What is the correct way to do replace between two indexes in a string in ruby?

  • 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-21T11:59:00+00:00Added an answer on May 21, 2026 at 11:59 am

    My solution inspired by @Andrew’s comment was to use open the text as a html document to be able to navigate to the xpath and do replacement on that. After that is done i just clean up the generated html headers. Wish there was a better way though.

    def replace_provider(xml)
      element = xml.at_xpath('//provider')
      return if element.nil?
    
      doc = Nokogiri::HTML(open(@sql_file))
      original_xml = doc.at_xpath("//provider[@name=\"#{element['name']}\"]")
      return if original_xml.nil?
    
      update_element_values(xml, original_xml)
      add_missing_elements(xml, original_xml)
    
      doc.xpath("//provider[@name=\"#{element['name']}\"]") do |item|
        item.replace original_xml
      end
      text = remove_all_html_tags(doc)
      File.open(@sql_file, 'w') { |file| file.puts text }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to write a ruby script using mechanize to batch upload a
I'm using Ruby, with the Nokogiri module, and i want to get the content
Using Nokogiri and Ruby. I have a page to parse with div id's like:
This Ruby code using Nokogiri doc.xpath(//tbody).remove removes the children of the <tbody> (as well
How could I use ruby to extract information from a table consisting of these
I have a website that requires using Nokogiri on many different websites to extract
I am trying to get the ASIN number from amazon html page using nokogiri
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C#, I need a class called User that has a username, password, active

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.