I have a function like so:
def add_auth_fields
xml = Builder::XmlMarkup.new(:target => '')
xml.instruct! :xml
xml.inquiry do |inquiry|
inquiry.authentication do |auth|
auth.username USERNAME
auth.password PASSWORD
end
end
xml
end
How can I access the xml.inquiry
node at a later time to add more data inside of that node?
If I call xml.inquiry do |inquiry|
again, it just adds another node to my xml.
Here is another example where I want to change the value of foo, but instead, it is adding another foo node to the xml.
[195] pry(main)> xml_markup = Builder::XmlMarkup.new
=> <pretty_inspect/>
[196] pry(main)> xml_markup.foo 'bar'
=> "<pretty_inspect/><foo>bar</foo>"
[197] pry(main)> xml_markup.foo 'test'
=> "<pretty_inspect/><foo>bar</foo><foo>test</foo>"
Builder does not seem to support what you are looking for, the generated XML is stored as a string and not nodes or any other data type.
You should refactor your code so that the data you wish to present in XML is managed in a variable at least, or as a model class or collection of classes; and separate the presentation of the XML from the storage and logic of the data.
You could also try another gem that supports working with a non-string data type, such as Gyoku or Nokogiri. With Gyoku, for example, you would create the XML from a plain ruby hash, and simply edit the values in the hash when necessary. You can output the current XML string at any point using Gyoku.xml(my_hash)
.