Using Nokogiri HTML Builder to create fragment with multiple root nodes
As you noted, Builder
will not allow you to build an HTML document with multiple root nodes. You'll need to use DocumentFragment
@doc = Nokogiri::HTML::DocumentFragment.parse ""
Nokogiri::HTML::Builder.with(@doc) do |doc|
doc.div {
doc.p "first test"
}
doc.div {
doc.p "second test"
}
end
puts @doc.to_html
How to add a comment before XML root node, in Nokogiri?
Comment should be added inside xml.children
NodeSet.
Here is an example:
xml = Nokogiri::XML('<hello/>')
=> #<Nokogiri::XML::Document:0x3fe1db8d0ed0 name="document" children=[#<Nokogiri::XML::Element:0x3fe1db8d0584 name="hello">]>
xml.children.before(Nokogiri::XML::Comment.new(xml, 'how are you?'))
=> #<Nokogiri::XML::Element:0x3fe1db8d0584 name="hello">
xml.to_s
=> "<?xml version=\"1.0\"?>\n<!--how are you?-->\n<hello/>\n"
How to add an attribute to Nokogiri XML builder?
Add the attributes to the node like this
xml = Nokogiri::XML::Builder.new do |x|
x.root do
x.book(isbn: 1235) do
x.text('Don Quixot')
end
end
end.doc
Or, after re-rereading your question perhaps you wanted to add it to the parent further in the do block. In that case, this works:
xml = Nokogiri::XML::Builder.new do |x|
x.root do
x.book do
x.parent.set_attribute('isbn', 12345)
x.text('Don Quixot')
end
end
end.doc
Generates:
<?xml version="1.0"?>
<root>
<book isbn="1235">Don Quixot</book>
</root>
XML Formation using Nokogiri
Try this instead:
require 'nokogiri'
builder = Nokogiri::XML::Builder.new do
root {
action "GetApplication"
email "apiorder@rently.com"
password "f1467868c64f818c7b9394d85cc46d98"
request {
response_format "detailed"
application_id "15544"
render_applicant_extended_fields {
ari_criminal_info ""
ari_evictions_info ""
limelyte_employment ""
limelyte_drivers_license""
}
}
}
end
req_body = builder.to_xml
Nokogiri XML builder - save options
You can perform an or
operation on your options' bits.
doc.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)
This will apply the defaults of AS_XML
and the additional setting of NO_EMPTY_TAGS
Ruby + Nokogiri: How to create XML node with attribute=value?
I would do as below :
require 'nokogiri'
builder = Nokogiri::XML::Builder.new do |xml|
xml.CityStateLookupRequest('userid' => 'xxxxxx' ) {
xml.zip("id" => '10'){
xml.Zip5 '90210'
}
}
end
puts builder.to_xml
# >> <?xml version="1.0"?>
# >> <CityStateLookupRequest userid="xxxxxx">
# >> <zip id="10">
# >> <Zip5>90210</Zip5>
# >> </zip>
# >> </CityStateLookupRequest>
Related Topics
Ruby Split String by Repeating Characters or a Space
How to Establish a Ssl Enabled Tcp/Ip Connection in Ruby
How to Use Nokogiri::Xml::Reader to Parse Large Xml Files
Convert Array to Hash While Preserving Array Index Values in Ruby
What Does the Regular Expression [\W-] Mean
Pry Not Stopping When Called from a Ruby Script That Reads from Stdin
Why Is This Permissions Error Occurring with Mod_Passenger.So
Call Task More Than Once in Rails 3 Generator
Url_For of a Custom Restful Resource (Composite Key; Not Just Id)
Ruby Model Output Id as Object Oid
Does C1 Code Coverage Analysis Exist for Ruby
Rspec and Shoulda - Complementary or Alternatives
Watir. Scroll to a Certain Point of the Page
How to Save Dates in Local Timezone to Db with Rails3
Rails 4 - Syntax Error, Unexpected Tidentifier, Expecting End-Of-Input