[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

parsing xml documents using rexml

Derek

5/1/2005 11:20:00 AM

Hi,

I'm new to ruby.
I want to use ruby to process an xml file.

At the moment I am using the REXML but are open to other xml parsers.

In the xml file below how would I go about processing the section <details> in the following way: -
- when the tag <copy> is detected perform a method "copy". within the "copy" method extract other child tags within the current <copy> element.
- when the tag <mkdir> is detected perform a method "mkdir". within the "mkdir" method extract other tags within the current <mkdir> element.

I've looked at using XPATH, and have been able to extract specific information but have not worked out how to do it.

here is a sample xml file: -

<?xml version="1.0"?>
<fileGroup>

<summary>
<application>
This can state one or multiple applications
</application>
<owner>
Person from development who create this file
</owner>
<description>
This is a sample File group xml
</description>
</summary>

<details>
<copy>
<source version="3">//myPath/to/File.txt</source>
<target>target directory path goes here</target>
</copy>
<copy>
<source>//myPath/to/File2.txt</source>
<target>target directory path goes here</target>
</copy>
<mkdir>relative/Path</mkdir>
</details>
</fileGroup>



2 Answers

Ryan Leavengood

5/1/2005 6:47:00 PM

0

Derek wrote:
> Hi,
>
> I'm new to ruby.
> I want to use ruby to process an xml file.
>
> At the moment I am using the REXML but are open to other xml parsers.
>
> In the xml file below how would I go about processing the section <details> in the following way: -
> - when the tag <copy> is detected perform a method "copy". within the "copy" method extract other child tags within the current <copy> element.
> - when the tag <mkdir> is detected perform a method "mkdir". within the "mkdir" method extract other tags within the current <mkdir> element.
>
> I've looked at using XPATH, and have been able to extract specific information but have not worked out how to do it.

Here is a somewhat sloppy version, but it works:

CODE_BEGIN
require 'rexml/document'
include REXML

def copy(source, target)
puts "Copying from #{source} to #{target}"
end

def mkdir(dir)
puts "Making directory #{dir}"
end

xml_file = ARGV[0] || 'filegroup.xml'
doc = Document.new File.new(xml_file)
doc.elements.each('fileGroup/details') do |element|
element.each do |command|
if command.kind_of? Element
case command.name
when 'copy'
source = ''
target = ''
command.each do |param|
if param.kind_of? Element
if param.name == 'source'
source = param.text
elsif param.name == 'target'
target = param.text
end
end
end
copy(source, target)
when 'mkdir'
mkdir(command.text)
else
puts "Unknown command: #{command.name}"
end
end
end
end
CODE_END

If you used attributes instead of subtags it would be easier:
<copy source="something" target="whatever"/>
<mkdir dir="some_dir"/>

CODE_BEGIN
doc.elements.each('fileGroup/details') do |element|
element.each do |command|
if command.kind_of? Element
case command.name
when 'copy'
copy(command.attributes['source'], command.attributes['target'])
when 'mkdir'
mkdir(command.attributes['dir'])
else
puts "Unknown command: #{command.name}"
end
end
end
end
CODE_END

Ryan Leavengood


Robert Klemme

5/1/2005 7:46:00 PM

0


"Derek" <derekhaskin@optusnet.com.au> schrieb im Newsbeitrag
news:001901c54e3d$713334c0$a0b21cd3@dhaskin...
Hi,

I'm new to ruby.
I want to use ruby to process an xml file.

At the moment I am using the REXML but are open to other xml parsers.

In the xml file below how would I go about processing the section <details>
in the following way: -
- when the tag <copy> is detected perform a method "copy". within the
"copy" method extract other child tags within the current <copy> element.
- when the tag <mkdir> is detected perform a method "mkdir". within the
"mkdir" method extract other tags within the current <mkdir> element.

I've looked at using XPATH, and have been able to extract specific
information but have not worked out how to do it.

here is a sample xml file: -

<?xml version="1.0"?>
<fileGroup>

<summary>
<application>
This can state one or multiple applications
</application>
<owner>
Person from development who create this file
</owner>
<description>
This is a sample File group xml
</description>
</summary>

<details>
<copy>
<source version="3">//myPath/to/File.txt</source>
<target>target directory path goes here</target>
</copy>
<copy>
<source>//myPath/to/File2.txt</source>
<target>target directory path goes here</target>
</copy>
<mkdir>relative/Path</mkdir>
</details>
</fileGroup>




Without an actual implementation I think you need two levels of iteration
here: you iterate through all "copy" and "mkdir" elements that are childs of
"details" and then for each of them you need to collect specific child data.
That's the way I would try to approach this. HTH

Kind regards

robert