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
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