Forum: Ruby Search for an Presence of an XML element in a array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
78d0dd514b51f54aa016c3d3032ba011?d=identicon&s=25 Naresh Ramaswamy (s2naresh)
on 2009-02-19 07:24
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-19 08:40
(Received via mailing list)
On 19.02.2009 07:24, Naresh Ramaswamy 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
>
>   <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
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-02-19 16:26
(Received via mailing list)
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.

> 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.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-19 20:30
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
78d0dd514b51f54aa016c3d3032ba011?d=identicon&s=25 Naresh Ramaswamy (s2naresh)
on 2009-02-20 07:06
Attachment: script_data.xml (1 KB)
>
> 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
Ff9e18f0699bf079f1fc91c8d4506438?d=identicon&s=25 James Britt (Guest)
on 2009-02-20 07:46
(Received via mailing list)
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-x...


--
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
78d0dd514b51f54aa016c3d3032ba011?d=identicon&s=25 Naresh Ramaswamy (s2naresh)
on 2009-02-26 15:16
>
> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-26 20:58
(Received via mailing list)
On 26.02.2009 15:15, Naresh Ramaswamy wrote:
> elsif Element 'WAIT' exists
>     do this
> else
>     p "Error"
> end
>
> I need a solution for the above.

Use case.

  robert
This topic is locked and can not be replied to.