parsing xml documents using rexml

D

Derek

------=_NextPart_000_0016_01C54E91.42976650
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

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

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=3D"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=3D"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>




------=_NextPart_000_0016_01C54E91.42976650--
 
R

Ryan Leavengood

Derek said:
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
 
R

Robert Klemme

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
 

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
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top