REXML Nodes and Elements


REXML, a Ruby-style XML toolkit

What’s the difference between results from code (1) and (2) below ?
(element is an XML element)

Code (1), use Element#elements :


element.elements.each do |e|
 puts e.inspect
end

Code (2), use Element#to_a :


element.to_a.each do |e|
 puts e.inspect
end

Update: We can actually use just element.each .. no .to_a requied — thanks to P’Pok for this

Code (2) will give us texts, elements (as well as other nodes).
Where code (1) will give us only elements.

If our input is:


<p><b>bold</b> text</p>

Code (1) will give:


<b> ... </>

While code (2) will give:


<b> ... </>
" text"

This tiny difference already wasted me hours, shamed 🙁
I was thought that text is a kind of element, … that’s plain wrong,
both text and element are kinds of node !

For several REXML tutorials/examples I’ve found, where I copied and pasted codes from for my quick-n-dirty-self-education, all of them show only the use of Element#elements but not Element#to_a.

This is probably because all of them only deal with a data-oriented XML, where a use of ‘mix content’ is rare (and indeed not recommended).
But that’s no longer true for document/text-oriented XML — for example, XHTML.

If you going to process a XML with mix content, beware of #elements.

Correct me if I do anything wrong here.
REXML veteran? Share! 😉


Tutorials:
REXML Home |
XML.com |
developerWorks
API docs


2 responses to “REXML Nodes and Elements”

  1. งงๆ ตาม bact' ไปด้วย(ว่า text เป็น element)แวบไปดู javadoc org.w3c.dom มาก็เลยกระจ่าง==================interface Text extends Nodeinterface Element extends Node==================เสริมอีกนิดClass Element มัน extend จาก class Parent ซึ่งมัน include Enumerable ด้วยทำให้เราใช้อย่างนี้ได้ด้วยelement.each do |node| puts node.inspectend

  2. แป่ววววว ใช้ #each ตั้งแต่แรกก็หมดเรื่องแย้ววว – -"เออ เนอะ หัวเราคงยังไม่เป็น Ruby จริง ๆ ด้วยไม่งั้นคงต้องลอง each ไปก่อนเปิด api doc แล้วขอบคุณครับว่าแต่ … ฮ่ะ ๆ javadoc rulez !

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version