'Rails nokogiri XML create

In ruby on rails i use Nokogiri to create XML files. Now I want to create a XML line like this:

<mis:actions param="-900" accuracy="1" cameraIndex="0" payloadType="0" payloadIndex="0">GimbalPitch</mis:actions>

I thought the code has to look like this (1):

xml['mis'].actions('param' => "-900", "accuracy" => "1", "cameraIndex" => "0", "payloadType" => "0", "payloadIndex" => "0") 'GimbalPitch'

or (2)

xml['mis'].actions('param' => "-900", "accuracy" => "1", "cameraIndex" => "0", "payloadType" => "0", "payloadIndex" => "0") do 
'GimbalPitch'
end

But (1) is not working at all and (2) looks like this:

<mis:actions param="-900" accuracy="1" cameraIndex="0" payloadType="0" payloadIndex="0">

So in (2) i miss the part ...GimbalPitch</mis:actions> at the end.

Thanks, Andreas



Solution 1:[1]

xml['mis'].actions 'text', params: '1' # => <mis:actions params="1">text</mis:actions>

It might be not obvious, but it's simple. When building basic xml structure

xml.user {                  # <user>    
  xml.name 'user name'      #   <name>user name</name>
}                           # </user>

xml.name with a string as argument maps to a tag with text.

Nothing is changed when you add a namespace. A bunch of parameters kind of threw me off a bit as well:

# without namespace
xml.actions 'text'                        # <actions>text</actions>

# with namespace
xml['mis'].actions 'text'                 # <mis:actions>text</mis:actions>

# add options. (it's just a ruby method)
xml['mis'].actions 'text', params: '1'    # <mis:actions params="1">text</mis:actions>

Also there is a text method but it adds class="text"

xml['mis'].actions(params: 1).text('text') # <mis:actions params="1" class="text">text</mis:actions>       

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex