REXML Element exists question

J

John Butler

An xml file is generated and i am currently parsing through it in a
rails app. The problem with this XML file is sometimes the elelment
doesnt have a value it sometimes doesnt include the tags in the file.
So for example:

If it has a value
<Client>Client Name</Client>

An empty value
<Client></Client>

Sometimes i wont even have the above tags depending on how the file was
generated from a different application.

I want to check if the Client element is present in the file so the
following code below works somehow:

if !element.elements["Client"].nil?
project.client = element.elements["Client"].text
end

My question is there a more elegant way to this? I dont want to have to
write an if/case statment for every possible element. I did try the
below but that gave me an error on the nil? because it couldnt find the
element to test if it was nil, thats why im suprised the above code
works.

!element.elements["Client"].nil ? project.client =
element.elements["Client"].text : project.client = nil


I have looked at the rexml docs for elements, attributes but nothing
works if the <client></client> tags are not present.

Any advice appreciated,

JB
 
U

Urban Hafner

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


My question is there a more elegant way to this? I dont want to
have to
write an if/case statment for every possible element. I did try the
below but that gave me an error on the nil? because it couldnt find
the
element to test if it was nil, thats why im suprised the above code
works.

!element.elements["Client"].nil ? project.client =
element.elements["Client"].text : project.client = nil

I have no idea of REXML but you are missing a ? here. The one present is
the one of the ?: operator, but you need the .nil? method and not the
nil method so you have to add a second one.

And if everything works like the above (i.e. the XML Tag has the same
name as the model attribute) then you could use something like this
(untested of course) and maybe stick it into a method:

e = "Client"
meth = "#{e.downcase}=".to_sym
text = !element.elements[e].nil? ? element.elements[e].text : nil
project.send(meth, text)

Urban





-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (Darwin)

iD8DBQFG1qlsggNuVCIrEyURAp38AJ4y76fXMtYgKRA60ovmpdz0kuXt6QCfbtqr
2SBTn6C4TlP/iCdQt14WJXI=
=YWwN
-----END PGP SIGNATURE-----
 
R

Robert Klemme

An xml file is generated and i am currently parsing through it in a
rails app. The problem with this XML file is sometimes the elelment
doesnt have a value it sometimes doesnt include the tags in the file.
So for example:

If it has a value
<Client>Client Name</Client>

An empty value
<Client></Client>

Sometimes i wont even have the above tags depending on how the file was
generated from a different application.

I want to check if the Client element is present in the file so the
following code below works somehow:

This should do what you need:

doc = REXML::Document.new(File.read(f))
cl = REXML::XPath.first(doc, '//Client')
project.client = cl.text if cl
if !element.elements["Client"].nil?
project.client = element.elements["Client"].text
end

My question is there a more elegant way to this? I dont want to have to
write an if/case statment for every possible element. I did try the
below but that gave me an error on the nil? because it couldnt find the
element to test if it was nil, thats why im suprised the above code
works.

!element.elements["Client"].nil ? project.client =
element.elements["Client"].text : project.client = nil


I have looked at the rexml docs for elements, attributes but nothing
works if the <client></client> tags are not present.

IMHO the REXML tutorial is great source:
http://www.germane-software.com/software/rexml/docs/tutorial.html

I also often look at
http://www.w3schools.com/xpath/
http://www.zvon.org/xxl/XPathTutorial/General/examples.html

Kind regards

robert
 
D

dima

You should consider using XPaths capabilities of REXML.

http://stdlib.rubyonrails.org/libdoc/rexml/rdoc/index.html
http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/XPath.html
http://www.ibm.com/developerworks/xml/library/x-matters18.html
http://www.germane-software.com/software/rexml/docs/tutorial.html




-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

My question is there a more elegant way to this? I dont want to
have to
write an if/case statment for every possible element. I did try the
below but that gave me an error on the nil? because it couldnt find
the
element to test if it was nil, thats why im suprised the above code
works.
!element.elements["Client"].nil ? project.client =
element.elements["Client"].text : project.client = nil

I have no idea of REXML but you are missing a ? here. The one present is
the one of the ?: operator, but you need the .nil? method and not the
nil method so you have to add a second one.

And if everything works like the above (i.e. the XML Tag has the same
name as the model attribute) then you could use something like this
(untested of course) and maybe stick it into a method:

e = "Client"
meth = "#{e.downcase}=".to_sym
text = !element.elements[e].nil? ? element.elements[e].text : nil
project.send(meth, text)

Urban

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (Darwin)

iD8DBQFG1qlsggNuVCIrEyURAp38AJ4y76fXMtYgKRA60ovmpdz0kuXt6QCfbtqr
2SBTn6C4TlP/iCdQt14WJXI=
=YWwN
-----END PGP SIGNATURE-----
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top