Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xmlns in root node #39

Open
maxmeyer opened this issue Jan 10, 2012 · 10 comments
Open

xmlns in root node #39

maxmeyer opened this issue Jan 10, 2012 · 10 comments

Comments

@maxmeyer
Copy link

Hi there,

how do I have to use roxml to get

 xmlns:namespace=example.com

as parameter in root node when using

Object.to_xml.to_s

I tried to figure out how to do it by myself, but I didn't find a way to do it (I had a look at spec/namespaces).

I used the following input file (btw: this is how the output should look like)

<?xml version="1.0" encoding="UTF-8"?>
<ns2:user xmlns:ns2="http://foo.example.com">                                                                                               
  <attribute>attr</attribute>
</ns2:user>

and the following script

require 'roxml'
require 'pry'
require 'pp'

class User
  include ROXML

 #not needed but my real classes look like UserSimple etc.
  xml_name 'user'
  xml_namespace :ns2
  xml_namespaces :ns2 => 'http://foo.example.com'

  xml_accessor :attribute, :namespace => false                                                                                              
end


user = User.from_xml(File.read(File.expand_path('example-user.xml', File.dirname(__FILE__))))
pp user.to_xml.to_s

Help would be very welcome, as I'm stuck in the moment and need the stuff working for my master thesis.

Thanks.

Cheers,
Max meyer

@ruckus
Copy link

ruckus commented Feb 19, 2012

According to the README ROXML does not support outputting namespaced nodes. Apparently this is planned for a future version.

I've needed the same functionality myself and I've had to override to_xml and build the XML + namespaces manually. Not elegant but it works.

@maxmeyer
Copy link
Author

Mmmh, thx for the clarification. I consulte the README, but may have overseen that...

@benWoz
Copy link

benWoz commented Mar 13, 2012

@ruckus We're running into the same issue over here.

Could you detail what you did to override to_xml to build the XML with namespaces? Perhaps point us to some code?

We've got some time to work on this, if we feel like this would be a beneficial contribution for the project.

@ruckus
Copy link

ruckus commented Mar 13, 2012

@benWoz my approach ended up being fairly manual.

Check out this stuff:

https://github.com/ruckus/quickeebooks/blob/master/lib/quickeebooks.rb#L27

That Model class is a base class, an example concrete class is

https://github.com/ruckus/quickeebooks/blob/master/lib/quickeebooks/model/account.rb

When I have an instance of Account and want to convert it to XML I call to_xml_ns.

This works but feels kinda janky. In the base Model I call to_xml but write it out to a temporary StringIO object that I can mutate even more.

Hope this helps

@benWoz
Copy link

benWoz commented Mar 14, 2012

@ruckus, thanks for the quick response. I'll take a look at it and see what can be done!

@bishboria
Copy link

Any further forward with this?

I'm trying to output the following, e.g.

<example xmlns="http://example.com/">
    <other>stuff</other>
</example>

Right now, I'm doing the following nasty hack:

  to_xml.to_s.sub("example", "example xmlns='http://example.com'")

@benWoz
Copy link

benWoz commented Jun 19, 2012

I think we ended up taking a different route. Can't remember exactly what &
don't have access to the client's code at this point...

Wish I had more info for you.

  • Ben Woz

On Tue, Jun 19, 2012 at 11:07 AM, Stuart Gale <
reply@reply.github.com

wrote:

Any further forward with this?

I'm trying to output the following, e.g.

<example xmlns="http://example.com/"> <other>stuff</other> </example>


Reply to this email directly or view it on GitHub:
#39 (comment)

@bishboria
Copy link

That's cool don't worry. My nasty hack works for now... :)

@chewi
Copy link
Contributor

chewi commented Nov 23, 2012

A nicer alternative to @bishboria's hack is this...

class Example
  include ROXML
  xml_reader :xmlns, :from => :attr

  def initialize
    @xmlns = "http://example.com"
  end
end

@eabartlett
Copy link

For other folks that run into this - the easiest way I found (outside of forking + patching) is to override to_xml on the class

class Example
  include ROXML
  xml_accessor :some_attribute
  
  def to_xml(params = {})
    params[:namespaces] = { "ns1" => "http://example.com" }
    super
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants