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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:44:39+00:00 2026-05-26T13:44:39+00:00

I’m trying to edit an XML file and replace strings with ruby variables. For

  • 0

I’m trying to edit an XML file and replace strings with ruby variables.
For now this is my code :

<?xml version="1.0" encoding="US-ASCII"?>
<Standart-Profile>
    <class1>
        <class2>
            <class3>
               <value1>old_A</value2>
               <value1>old_B</value2>
               <value1>old_C</value2>
            </class3>
        </class2>
    </class1>
</Standart-Profile>

And this is the Ruby file :

require "rexml/text"
require 'rexml/document'
include REXML

def generate_2
    ...
end

def generate_1
    ...
end


File.open('Standart-Profile.xml') do |config_file|
  config = Document.new(config_file)
  config.root.elements['old_A'].text = 'generate_1'
  config.root.elements['old_B'].text = 'generate_2'
  config.root.elements['old_C'].text = 'generate_1'

  formatter = REXML::Formatters::Default.new
  File.open('New-Profile.xml', 'w') do |result|
  formatter.write(config, result)
  end
end

But I keep getting this error :

Final-Tool-Kit.rb:19:in `block in <main>': undefined method `text=' for nil:NilC
lass (NoMethodError)
        from test.rb:16:in `open'
        from test.rb:16:in `<main>'
  • 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-26T13:44:41+00:00Added an answer on May 26, 2026 at 1:44 pm

    Your problem is that you’re looking for an element with the name old_A when you should be looking for an element with the text contents of old_A.

    Here’s a solution using Nokogiri, which I find more convenient than REXML:

    require 'nokogiri' # gem install nokogiri
    
    xml = "<Standart-Profile>
        <class1>
            <class2>
                <class3>
                   <value1>old_A</value2>
                   <value1>old_B</value2>
                   <value1>old_C</value2>
                </class3>
            </class2>
        </class1>
    </Standart-Profile>"
    
    doc = Nokogiri.XML(xml)
    doc.at('//text()[.="old_A"]').content = 'generate_1'
    doc.at('//text()[.="old_B"]').content = 'generate_2'
    doc.at('//text()[.="old_C"]').content = 'generate_3'
    
    File.open('output.xml','w') do |f|
      f.puts doc
    end
    #=> <?xml version="1.0" encoding="US-ASCII"?>
    #=> <Standart-Profile>
    #=>     <class1>
    #=>         <class2>
    #=>             <class3>
    #=>                <value1>generate_1</value1>
    #=>                <value1>generate_2</value1>
    #=>                <value1>generate_3</value1>
    #=>             </class3>
    #=>         </class2>
    #=>     </class1>
    #=> </Standart-Profile>
    

    If you actually want to call a generate_1 method (as you have defined) then you would instead use:

    ...content = generate_1 # no quotes
    

    Edit: Here’s one way to do it with XPath in REXML (after I fixed the source XML to be valid):

    require 'rexml/document'    
    doc = REXML::Document.new(xml)
    REXML::XPath.first(doc,'//*[text()="old_A"]').text = 'generate_1'
    REXML::XPath.first(doc,'//*[text()="old_B"]').text = 'generate_2'
    REXML::XPath.first(doc,'//*[text()="old_C"]').text = 'generate_3'
    puts doc
    #=> <Standart-Profile>
    #=>     <class1>
    #=>         <class2>
    #=>             <class3>
    #=>                <value1>generate_1</value1>
    #=>                <value1>generate_2</value1>
    #=>                <value1>generate_3</value1>
    #=>             </class3>
    #=>         </class2>
    #=>     </class1>
    #=> </Standart-Profile>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm parsing an XML file, the creators of it stuck in a bunch social
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
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.