Add a node to XML using Nokogiri::XML::Builder - ruby-on-rails

I have a Nokogiri::XML::Builder instance, when I call to_xml it produces following structure:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>...</item>
<item>...</item>
</root>
Using this instance I'd like to add one more <item> node under <root> like this:
def add_static_job(builder)
source = builder.doc.root
item = Nokogiri::XML::Node.new('item', source)
item.content = '<title>Hello</title>'
source << item
end
Unfortunatelly this doesn't produce valid xml in the end, rather something like:
<item><title>Hello<title></item>
What could the problem be?

You could do it in 2 steps :
create title node with "Hello" as content
create item node with title as content
xml = '<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>A</item>
<item>B</item>
</root>'
require 'nokogiri'
doc = Nokogiri::XML.parse(xml)
source = doc.root
title = Nokogiri::XML::Node.new('title', doc)
title.content = "Hello"
item = Nokogiri::XML::Node.new('item', doc)
item << title
source << item
puts doc
# =>
# <?xml version="1.0" encoding="UTF-8"?>
# <root>
# <item>A</item>
# <item>B</item>
# <item><title>Hello</title></item></root>

Related

Nokogiri: Searching node with ":"

I have a simple XML structure:
<?xml version="1.0" encoding="utf-8"?>
<response xmlns:msg="..." xmlns:ld="...">
<msg:testResultBatch providerId="12345" testName="Hello Labs">
.
.
.
</msg:testResultBatch>
</response>
When I pass it to Nokogiri.XML like:
req = Nokogiri.XML('
<?xml version="1.0" encoding="utf-8"?>
<response xmlns:msg="..." xmlns:ld="...">
<msg:testResultBatch providerId="12345" testName="Hello Labs">
.
.
.
</msg:testResultBatch>
</response>
')
I'm unable to search nodes with ":". So,
req.search("response") # works
but,
req.search("msg:testResultBatch") # doesn't works
and gives me []
By using xpath and '//msg:testResultBatch' you can get the msg:testResultBatch:
require 'nokogiri'
req = Nokogiri.XML('
<?xml version="1.0" encoding="utf-8"?>
<response xmlns:msg="..." xmlns:ld="...">
<msg:testResultBatch providerId="12345" testName="Hello Labs">
</msg:testResultBatch>
</response>
')
p req.xpath('//msg:testResultBatch').first.name # "testResultBatch"

Nokogiri : NoMethodError (undefined method `inner_html' for nil:NilClass)

I'm trying to parse a simple XML data with nokogiri.
this is my XML:
POST /.... HTTP/1.1
Host: ....
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://...."
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="...." xmlns:xsd="...." xmlns:soap="....">
<soap:Body>
<WS_QueryOnSec xmlns="......">
<type>string</type>
<ID>string</ID>
</WS_QueryOnSec>
</soap:Body>
</soap:Envelope>
and this is my simle request:
require "nokogiri"
#doc = Nokogiri::XML(request.body.read)
#something = #doc.at('type').inner_html
But Nokogiri can not find the Type or ID node.
When I change the data into this every thing works fine:
<soap:Body>
<type>string</type>
<ID>string</ID>
</soap:Body>
It seems the problem is the raw text above the data and the nods with xmlns or the other attributes!
What do you recommend to resolve this ?
The first "XML" isn't XML. It's text that contains XML. Remove the header information down to the blank line and try it again.
I think it'd help you to read the XML spec or to read some tutorials about creating XML which will help you understand how it's defined. XML is a tight specification and doesn't allow any deviation. The syntax is pretty flexible, but you have to play by its rules.
Consider these examples:
require 'nokogiri'
doc = Nokogiri::XML(<<EOT)
foo
<root>
<node />
</root>
EOT
doc.errors # => [#<Nokogiri::XML::SyntaxError: Start tag expected, '<' not found>]
Removing the text, which is outside the root tag results in a proper parse:
require 'nokogiri'
doc = Nokogiri::XML(<<EOT)
<root>
<node />
</root>
EOT
doc.errors # => []
<root> isn't neccesarily the name of the "root" node, it's just the outermost tag:
doc = Nokogiri::XML(<<EOT)
<foo>
<node />
</foo>
EOT
doc.errors # => []
and still results in a valid DOM/internal representation of the document:
puts doc.to_html
# >> <foo>
# >> <node></node>
# >> </foo>
Your XML sample is using namespaces, which complicate matters somewhat. The Nokogiri documentation talks about how to deal with them, so you'll want to understand that part of parsing XML because you'll encounter it again. Here's the easy way of working with them:
require 'nokogiri'
doc = Nokogiri::XML(<<EOT)
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns:xsi="...." xmlns:xsd="...." xmlns:soap="....">
<Body>
<WS_QueryOnSec xmlns="......">
<type>string</type>
<ID>string</ID>
</WS_QueryOnSec>
</Body>
</Envelope>
EOT
namespaces = doc.collect_namespaces
doc.at('type', namespaces).text # => "string"

Ruby to_xml change attributes name for Api request

I want to convert the keys for xml response So that they match with third party Api request.
class Person1
include ActiveModel::Serializers::Xml
attr_accessor :name, :age
def attributes
{'name' => nil, 'age' => nil}
end
def capitalized_name
name.capitalize
end
end
p = Person1.new
p.name = "test"
puts p.to_xml
output ::-
<?xml version="1.0" encoding="UTF-8"?>
<person1>
<age nil="true"/>
<name>test</name>
</person1>
I am looking for a way to change keys in xml output like.
<?xml version="1.0" encoding="UTF-8"?>
<person1>
<Age nil="true"/>
<Name>test</Name>
</person1>
How about:
puts p.to_xml(:camelize => true)
<?xml version="1.0" encoding="UTF-8"?>
<Person1>
<Age nil="true"/>
<Name>test</Name>
</Person1>
Or if the uppercase Person bothers you, I guess you can do something like that:
puts p.to_xml(:camelize => true).sub('<Person1>','<person1>').sub('</Person1>','</person1>')
<?xml version="1.0" encoding="UTF-8"?>
<person1>
<Age nil="true"/>
<Name>test</Name>
</person1>

How to parse this returned XML with Nokogiri

I'm attempting to parse this XML with nokogirl but I'm having trouble. Any ideas where I'm going wrong? I'd like to get each Dealer and get the values for each of them.
doc = Nokogiri::Slop(response.body)
puts doc.content #works, shows the response below
puts doc.DTX_LEAD_ID.content #errors, no method found.
puts doc.NEWCAR_PINGGX_RESPONSE.content #errors, no method found
returned XML:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="www.example.com/">
<?xml version="1.0" encoding="utf-8"?>
<NEWCAR_PINGGX_RESPONSE xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="www.example.com/SellerMessages">
<DTX_LEAD_ID>1779853194</DTX_LEAD_ID>
<SUCCESS>true</SUCCESS>
<CACHED_RESPONSE>false</CACHED_RESPONSE>
<PRICE>20</PRICE>
<DealerList>
<Dealer>
<BUYER_ID>0000-2127</BUYER_ID>
<Reservation_ID>1779853194|0000-2067|520a8037-57c8-497e-be4b-f4ea8dfa6c6f|14187-20</Reservation_ID>
<Price>20</Price>
<Name>Randy's Rides</Name>
<State>MI</State>
<City>Southfield</City>
<Street>2001 Town Center</Street>
<Postalcode>48076</Postalcode>
<Distance>2.56002068066733</Distance>
<DealerGroup id="2067" max_post="5" />
<Contact><Name>John Campbell</Name>
<Phone>2483521314</Phone>
</Contact>
</Dealer>
</DealerList>
</NEWCAR_PINGGX_RESPONSE></string>
Previously I've had a response like this:
<?xml version="1.0" encoding="utf-8"?>
<results>
<status>accepted</status>
<id>1724128693</id>
<purchaseprice>8.0000</purchaseprice>
<error>false</error>
<messages>
<message>coverage available</message>
</messages>
</results>
Which parses really easily with nokogiri:
doc.results.messages.message.content #coverage available
I want to do something like:
doc.NEWCAR_PINGGX_RESPONSE.DealerList.Dealer.Name.content #returns "Randy's Rides"
To see what's wrong with a document use the errors method. After parsing your XML:
doc.errors
# => [#<Nokogiri::XML::SyntaxError: xmlns: URI www.example.com/ is not absolute>,
# #<Nokogiri::XML::SyntaxError: XML declaration allowed only at the start of the document>,
# #<Nokogiri::XML::SyntaxError: xmlns: URI www.example.com/SellerMessages is not absolute>]
To extract the data I'd use something like this:
doc = Nokogiri::XML(XML)
doc.remove_namespaces!
dealers = doc.search('Dealer').map{ |dealer|
{
buyer_id: dealer.at( 'BUYER_ID' ).text,
reservation_id: dealer.at( 'Reservation_ID' ).text,
name: dealer.at( 'Name' ).text
}
}
dealers
# => [{:buyer_id=>"0000-2127",
# :reservation_id=>
# "1779853194|0000-2067|520a8037-57c8-497e-be4b-f4ea8dfa6c6f|14187-20",
# :name=>"Randy's Rides"},
# {:buyer_id=>"0000-2127",
# :reservation_id=>
# "1779853194|0000-2067|e42fd5c6-0a36-4552-8b6a-ad2decebd0db|14200-10",
# :name=>"Jarrett's New Car Dealership 01"},
# {:buyer_id=>"0000-2127",
# :reservation_id=>
# "1779853194|0000-2067|3fecb591-3a81-49f9-82b3-1f0d7fb3f7a6|14160-20",
# :name=>"Campbell's Crazy Cars"},
# {:buyer_id=>"0000-2127",
# :reservation_id=>
# "1779853194|0000-2067|731b09e9-700b-4f41-8cb0-eaf80e861d76|14158-7",
# :name=>"Demo Dealer 3"}]
Of course you'll want to add/remove/change fields being extracted to fit your use-case.
Using slop mode has its dangers, as stated by the Nokogiri documentation.
Don’t use this.
This may or may not be a backhanded compliment.
No, really, don’t use this. If you use it, don’t report bugs.
You’ve been warned!
I've never used it as a result. Often we don't want to use remove_namespaces! either, but it appears safe in your situation.

Rails XML Feed: ID as node attribute

I set up a simple XML feed for a vendor we're using (who refuses to read JSON).
<recipes type="array">
<recipe>
<id type="integer">1</id>
<name>
Hamburgers
</name>
<producturl>
http://test.com
</producturl>
...
</recipe>
...
<recipe>
However, the vendor requests that instead of having an id node, id is an attribute in the parent node. e.g.
<recipes type="array">
<recipe id="1">
<name>
Hamburgers
</name>
<producturl>
http://test.com
</producturl>
...
</recipe>
...
<recipe>
I'm building this with (basically)
xml_feed = []
recipes.each do |recipe|
xml_feed <<{id: recipe.id, name: recipe.name, ...}
end
...
render xml: xml_feed.to_xml(root: 'recipes')
But I'm unsure of how to include the id (or any field) as an attribute in the parent node like that. I googled around and couldn't find anything, nor were the http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html docs very helpful
Thanks!
I would suggest you use the nokogiri gem. It provides all you can possible need for handling XML.
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {
xml.objects {
xml.object.classy.thing!
}
}
end
puts builder.to_xml
<?xml version="1.0"?>
<root>
<objects>
<object class="classy" id="thing"/>
</objects>
</root>
The suggestion to use Nokogiri is fine. Just the sintax should be a little bit different to achive what you have requested:
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {
xml.object('type' => 'Client') {
xml.name 'John'
}
}
end
puts builder.to_xml
<?xml version="1.0"?>
<root>
<object type="Client">
<name>John</name>
</object>
</root>

Resources