Search for an Presence of an XML element in a array

hi,

I am reading an XML document and storing in a array.
Now I need to search for presence of a particular element and

array = {elements of XML Document}
array[1] = {chile elelments of XML document}

*** I want to ***
search if element X or Y or Z exists
if
X exists
Do this
elsif Y exists
Do this
else
Do This
end

I am not finding any way to search for elelemtns in XML file.
Please provide your solution.

My XML file looks like this

I am using REXML solution to play with Ruby file.

regards,
Naresh

Naresh R. [email protected] wrote:

hi,

I am reading an XML document and storing in a array.
Now I need to search for presence of a particular element and

array = {elements of XML Document}
array[1] = {chile elelments of XML document}

That seems like a mistake. An XML document is a form of data storage -
think of it as a kind of database - and it is impossible to represent an
arbitrary XML document in some other form. So I suggest that you
should read the XML document and not store it an array; just leave it,
and use REXML to explore it and manipulate it.

end

I am not finding any way to search for elelemtns in XML file.

XPath? Isn’t this what XPath is for?

I suggest reading a good book about XML. It can be an eye-opening
experience…! :slight_smile:

m.

On 19.02.2009 07:24, Naresh R. wrote:

if
X exists
Do this
elsif Y exists
Do this
else
Do This
end

I am not finding any way to search for elelemtns in XML file.
Please provide your solution.

XPath. You can find it in REXML’s tutorial section.

My XML file looks like this

I am using REXML solution to play with Ruby file.

It is not clear to me how you do the array storage. With REXML you
basically just need to read the document. Then you can operate on the
DOM. At the moment I do not see a need for you to separately store
something in an Array. Can you be more specific about what your program
does - and how?

Kind regards

robert

Naresh R. wrote:

hi,

I am not finding any way to search for elelemtns in XML file.
Please provide your solution.

I am using REXML solution to play with Ruby file.

xml2.xml:

<?xml version="1.0" encoding="iso-8859-1"?> Jane 123-4567 David Tom James 666-6666

require ‘rexml/document’
include REXML

f = File.new(“xml2.xml”)
doc = Document.new(f)

names = XPath.match(doc, “//friend”)
puts names.length #3

addresses = XPath.match(doc, “//addresses”)
puts addresses.length #0

addresses = XPath.match(doc, “//addresses”)
puts addresses.length #0

Thank you very much for your responses.
Let me get in a little more deep into my problem, I have attached my
actual XML document for your referance and suggestion.

I need to look into every TXN element “” where x can be
1 to n
before that I need to count the occurance of “<TXN”, this is why I used
array so that from array size I can get the occurance.

Further I shall get into every “<TXN value” element and search for SEND,
EXPECT, WAIT based on their occurance and in the same order. For every
occurance I shall be performing some action in the ruby script.
I am storing the values under "<TXN " into another array and based on
its size I am picking the instances of SEND, EXPECT, WAIT untill the
array.size is reached.

I feel the above solution is quite clumsy, Please suggest me a better
solution for it.

thanks,
Naresh

addresses = XPath.match(doc, “//addresses”)
puts addresses.length #0

I have tried the above solution but it searches for the element and
producess an array of these nodes.
I would like to just know
if Element ‘SEND’ exists
do this
elsif Element ‘EXPECT’ exists
do this
elsif Element ‘WAIT’ exists
do this
else
p “Error”
end

I need a solution for the above.

thanks,
Naresh

Naresh R. wrote:

I feel the above solution is quite clumsy, Please suggest me a better
solution for it.

Use a pull parser and convert the XML into a more sensible data
structure.

A pull parser also makes it easier to trigger events based on elements.
Maybe you can skip the restructuring and directly work with the element
stream.

I think my Dr. Dobbs article may still be relevant:

http://www.jamesbritt.com/2007/4/14/transforming-xml-the-rexml-pull-parser


James B.

www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff

On 26.02.2009 15:15, Naresh R. wrote:

elsif Element ‘WAIT’ exists
do this
else
p “Error”
end

I need a solution for the above.

Use case.

robert