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

  <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-forum.com/.

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

···

On 19.02.2009 07:24, Naresh Ramaswamy wrote:

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

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...! :slight_smile:

m.

···

Naresh Ramaswamy <tech4me@in.com> wrote:

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

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-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/\.

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-forum.com/\.

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

Use case.

  robert

···

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.