I have followed much tutorials about Ruby 2.2 and REXML. This is an example of my xml:
<msg user='Karim'><body action='ChkUsername' r='0'><ver v='153' /></body></msg>
And this is what I currently have as code:
xml = "<msg user='Karim'><body action='ChkUsername' r='0'><ver v='153' /></body></msg>"
doc = Document.new xml
puts doc.root.attributes[action]
That won't work. An error pops out. undefined local variable or method 'action' for #{classname} (NameError)
You can't randomly assume variables exist. The token
actionwill be interpreted as a reference (e.g., a variable or method call) since it's not a string or symbol. You don't have that variable or method, so you get an error telling you precisely what's wrong.The root of your document is the
<msg>tag. The<msg>tag does not have an attributeaction. It has auserattribute, accessible as you'd expect:The
actionattribute is nested further in the document, in the<body>element.There are a number of ways to interrogate a document (all covered in the tutorial, btw), e.g.,