1

I'm using xml-mapping in Ruby (on Sinatra) for some XML stuff. Generally I follow this tutorial: http://xml-mapping.rubyforge.org/. I can create objects and write them to XML strings using

login.save_to_xml.to_s

But when I try

login = Login.load_from_xml(xml_string)

I get the following error:

XML::MappingError - no value, and no default value: Attribute username not set (XXPathError: path not found: username):

Here is the XML string I receive:

<login><username>ali</username><password>baba</password></login>

This is what the class looks like:

class Login
  include XML::Mapping

  text_node :username, "username"
  text_node :password, "password"
end

So the class name is the same, the nodes are named the same. I actually get the exact same string when I create an instance of my object and fill it with ali/baba:

test = Login.new
test.username = "ali"
test.password = "baba"
p test.save_to_xml.to_s

<login><username>ali</username><password>baba</password></login>

What am I missing?

Thanks, MrB

EDIT:

When I do

test = login.save_to_xml

And then

login = Login.load_from_xml(test)

it works. So the problem seems to be that I'm passing a string, while the method is expecting.. well, something else. There is definitely a load_from_xml(string) method in the rubydocs, so not sure what to pass here. I guess I need some kind of reverse to_s?

1 Answer 1

2

It looks like you save_to_xml creates a REXML::Element. Since that works, you may want to try:

Login.load_from_xml(REXML::Document.new(xml_string).root)

See the section on "choice_node" for a more detailed example http://xml-mapping.rubyforge.org/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.