Search for an Presence of an XML element in a array

N

Naresh Ramaswamy

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

<TXN value = '3'>
<SEND request = 'X' > <!- This can be either X or Y or Z -->
<MODIFY val1 = 'from' val2 = 'Show' val3 = '200'/>
</SEND>
</TXN>

I am using REXML solution to play with Ruby file.

regards,
Naresh
 
R

Robert Klemme

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.

XPath. You can find it in REXML's tutorial section.
My XML file looks like this

<TXN value = '3'>
<SEND request = 'X' > <!- This can be either X or Y or Z -->
<MODIFY val1 = 'from' val2 = 'Show' val3 = '200'/>
</SEND>
</TXN>

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
 
M

matt neuburg

Naresh Ramaswamy said:
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.
*** 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.

XPath? Isn't this what XPath is *for*?

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

m.
 
7

7stud --

Naresh said:
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"?>
<classmates>
<friend>
<name>Jane</name>
<phone>123-4567</phone>
</friend>

<foe>
<name>David</name>
<friend>Tom</friend>
</foe>

<friend>
<name>James</name>
<phone>666-6666</phone>
</friend>

</classmates>
------------



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
 
N

Naresh Ramaswamy

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 "<TXN value = 'x'>" 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




Attachments:
http://www.ruby-forum.com/attachment/3322/script_data.xml
 
J

James Britt

Naresh said:
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 Britt

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

Naresh Ramaswamy

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
 
R

Robert Klemme

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.

Use case.

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

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top