how to get all attributes in an element

Naresh Ramaswamy

Usenet Archive
Joined
Dec 30, 2008
Messages
13
Reaction score
0
hi,

I am using REXML

I want to get all the attributes under an element. Just imagine that I know the element name but not the attribute names.

say <element att1="x" att2="y" att3="3"/> and so on as many attributes that can be.

I need to get all the attributes under the element. How shall I do it.

I looked into REXML doc that provides
"doc.root.attributes.each {|name, value| puts name+" => "+value }"

But I am able to use this only on root element, and does not work on child or subchild elements.

Please let me know how can I do that.

Regards,
Naresh
 

7stud --

Usenet Archive
Joined
Sep 2, 2007
Messages
1,566
Reaction score
0
Naresh said:
hi,

I am using REXML

I want to get all the attributes under an element. Just imagine that I know the element name but not the attribute names.

say <element att1="x" att2="y" att3="3"/> and so on as many attributes that can be.

I need to get all the attributes under the element. How shall I do it.

I looked into REXML doc that provides
"doc.root.attributes.each {|name, value| puts name+" => "+value }"

But I am able to use this only on root element, and does not work on child or subchild elements.

Please let me know how can I do that.

Regards,
Naresh

require 'rexml/document'
include REXML

xml = <<ENDSTRING
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body att1="x" att2="y">Don't forget me this weekend!</body> </note> ENDSTRING

doc = Document.new xml

el_name = "body"
matching_elements = XPath.match(doc, "//#{el_name}")

matching_elements.each do |el|
attrs = el.attributes
attrs.each_key do |key|
puts attrs[key]
end
end

--output:--
x
y
 

Naresh Ramaswamy

Usenet Archive
Joined
Dec 30, 2008
Messages
13
Reaction score
0
--output:--

Thank you for the solution, it almost worked to me, but when I apply it on my xml document I am getting the output from reverse order, why so?

<?xml version="1.0" encoding="iso-8859-1"?>

<TEST_SCRIPT name='Basetest'>
<TXN value = '1'>
<SEND request='INVITE' sdp='true'>
<REMOVE_1 header='req_line' mname='InVIte'
uri='tel:'/>
<ADD_1 header='to' dname='ister'/>
<REMOVE_2 header='call_id' />
</SEND>
</TXN>
</TEST_SCRIPT>

I applied the code for the element "REMOVE_1" and observe the following as the output.

-----output------
tel:
InVIte
req_line
 

Phlip

Usenet Archive
Joined
Feb 22, 2000
Messages
2,075
Reaction score
0
Naresh said:
Thank you for the solution, it almost worked to me, but when I apply it on my xml document I am getting the output from reverse order, why so?
<REMOVE_1 header='req_line' mname='InVIte'
uri='tel:'/>
I applied the code for the element "REMOVE_1" and observe the following as the output.

-----output------
tel:
InVIte
req_line

XML has a concept of document order, but not of attribute order within one tag. XML readers are (apparently) not required to remember there order.

Next, the attributes come in a Hash, and this has a BUG (!) which will be FIXED (!) in the next Ruby versions. It can't preserve the order you inserted its keys.

Until then, your own code must put the keys into the order you need. Try

element.attributes.to_a.sort

to put them into asciibetic order, for example.
 

7stud --

Usenet Archive
Joined
Sep 2, 2007
Messages
1,566
Reaction score
0
Phlip said:
Naresh Ramaswamy wrote:

Next, the attributes come in a Hash, and this has a BUG (!) which will be FIXED (!) in the next Ruby versions. It can't preserve the order you inserted its keys.

Hmm. A bug is when a language doesn't operate as described:

----
p. 46, pickaxe 2:

[a hash's] elements are not ordered, so you can not easily use a hash as a stack or a queue.

p. 492

The order in which keys and/or values are returned by the various iterators over hash contents may seem arbitrary and will generally not be in insertion order.
----

$ ri Hash

------------------------------------------------------------ Class: Hash A +Hash+ is a collection of key-value pairs. It is similar to an +Array+, except that indexing is done via arbitrary keys of any object type, not an integer index. The order in which you traverse a hash by either key or value may seem arbitrary, and will generally not be in the insertion order.

Hashes have a _default value_ that is returned when accessing keys that do not exist in the hash. By default, that value is +nil+.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top