[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Finding all paths in an XML document

Ghislain MARY

7/4/2005 7:59:00 PM

Hi all,

I'd like to know how you would search for all the paths of an XML
document using REXML. For example, let say that the document is the
following:

<document>
<content>a simple sample</content>
<node>
<content>some text</content>
</node>
<node>
<foo>
<content>an other sample</content>
</foo>
</node>
</document>

What I'd like to have is an Array like this:
[['content'],
['node'],
['node', 'content'],
['node', 'foo'],
['node', 'foo', 'content']]

Any ideas ?

Thanks,

Ghislain


2 Answers

Robert Klemme

7/5/2005 9:02:00 AM

0

Ghislain Mary wrote:
> Hi all,
>
> I'd like to know how you would search for all the paths of an XML
> document using REXML. For example, let say that the document is the
> following:
>
> <document>
> <content>a simple sample</content>
> <node>
> <content>some text</content>
> </node>
> <node>
> <foo>
> <content>an other sample</content>
> </foo>
> </node>
> </document>
>
> What I'd like to have is an Array like this:
> [['content'],
> ['node'],
> ['node', 'content'],
> ['node', 'foo'],
> ['node', 'foo', 'content']]
>
> Any ideas ?
>
> Thanks,
>
> Ghislain

This might help:

require "rexml/document"

doc = File.open( "doc.xml" ){|io| REXML::Document.new io}

doc.elements.each( "//*" ) do |el|
p el.xpath.split(%r{/})
end

Alternative:

require "rexml/document"

doc = File.open( "doc.xml" ){|io| REXML::Document.new io}

doc.elements.each "//*" do |el|
path = []
par = el

while par
path.unshift par.name
par = par.parent
end

path.shift
p path
end

Kind regards

robert

Ghislain MARY

7/5/2005 3:14:00 PM

0

That worked fine.

Thanks a lot Robert.