need a routine to mimic XML parsing

C

crb

Greetings.

I am in need of perl code that will essentially mimic XML parsing,
since our co. has several dozen systems on which I need to run a
script and none of them have the XML::parser or XML::Simple or XML
modules installed (and doing so is much more trouble than writing a
script to get around the issues).

I'm an infrequent visitor to the world of perl, and have struggled w/
this one the past few days. Since it's probably something that someone
has done, I figured I'd post the request here.

I'm trying to parse out specific items in XML tags where the tags
could appear in multiple places in the XML file.

What I need is looping logic that, when it encounters a tag named,
say, <components type="...">, it will loop thru until it finds a tag
called <item> and then grabs some of the parameters of <item>. It has
to know that when it reaches </components>, it's done w/ that
component and captures that specific <item>. So the XML looks like:
<components type="type1">
<services .../>
<item color="red" size="large"/>
</components>
<components type="type2">
<services .../>
<item color="red" size="small"/>
</components>

The problem/issue I'm facing now is that there are several
<components> and <item> tags in the XML.

I know how to find the component I'm looking for (i.e.
while (<FILDHANDLE>) {
next if ( /^\s+\<components type=(.*) .*$/ );
if ( $1 = 'type1' ) {
. . .
}
. . .
}

but I need to capture the <item> for <components type="type1"> before
I encounter an <item> for <components type="type2">. Searching for
<item color=(.*)...> is not enough, since this information can be
duplicated across several <components> in the XML file.

Hopefully that adequately explains what I'm trying to do. Any help
would be greatly appreciated.

thx,

~~crb
 
A

A. Sinan Unur

(e-mail address removed) (crb) wrote in
Greetings.

I am in need of perl code that will essentially mimic XML parsing,
since our co. has several dozen systems on which I need to run a
script and none of them have the XML::parser or XML::Simple or XML
modules installed (and doing so is much more trouble than writing a
script to get around the issues).

I am not even going to attempt to address the issue of writing an XML
parser from scratch, but point out that you can bundle the libraries you
need with your script without having admin access to the machines on which
your script is going to run.

perldoc -q lib

Sinan.
 
M

Matt Garrish

crb said:
What I need is looping logic that, when it encounters a tag named,
say, <components type="...">, it will loop thru until it finds a tag
called <item> and then grabs some of the parameters of <item>. It has
to know that when it reaches </components>, it's done w/ that
component and captures that specific <item>. So the XML looks like:

Switch goes on; switch goes off. First thing that springs to my mind.

Turn on your switch when you find your component. If switch is active, start
looking for your item. If you encounter the next component before you find
the item, croak. Otherwise, grab your item and you're off to the races.

Certainly isn't elegant, but simplicity isn't a bad thing if you can trust
your data... (caveat emptor)

Matt
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top