[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Search for an Presence of an XML element in a array

Naresh Ramaswamy

2/19/2009 6:24:00 AM

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
--
Posted via http://www.ruby-....

7 Answers

Robert Klemme

2/19/2009 7:35:00 AM

0

On 19.02.2009 07:24, Naresh Ramaswamy 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}
>
> *** 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

--
remember.guy do |as, often| as.you_can - without end

matt

2/19/2009 3:23:00 PM

0

Naresh Ramaswamy <tech4me@in.com> 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.

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

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

7stud --

2/19/2009 7:30:00 PM

0

Naresh Ramaswamy 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"?>
<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



--
Posted via http://www.ruby-....

Naresh Ramaswamy

2/20/2009 6:06:00 AM

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 "<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-...attachment/3322/scrip...

--
Posted via http://www.ruby-....

James Britt

2/20/2009 6:46:00 AM

0

Naresh Ramaswamy 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-p...


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

Naresh Ramaswamy

2/26/2009 2:16:00 PM

0

>
> 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
--
Posted via http://www.ruby-....

Robert Klemme

2/26/2009 7:53:00 PM

0

On 26.02.2009 15:15, Naresh Ramaswamy wrote:
>> 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.

Use case.

robert