List Info

Thread: xml beginner




xml beginner
user name
2006-05-22 18:35:07
Mark thank you very much for that. This is definitely
getting me on the
right road and looks like I can whip something up today.
Question though
when I do a:

Foreach my $install (x->findnodes('//InstallInfo'))
{
}

Inside that foreach loop I want to check each item inside of
the
InstallInfo node and do certain conditions if I see certain
nodes. Now
to accomplish this how do I compare $install? Can I do
something like
if($install eq "something") or do I have to
convert $install to
something? Is it possible to use Regex in some cases as
well? It looks
like in worse case scenario I could do ->toString but
then I lose the
whole advantage of having well formed and defined data.

Thanks


-----Original Message-----
From: Thomas, Mark - BLS CTR [mailto:Thomas.Markbls.gov] 
Sent: Monday, May 22, 2006 6:16 AM
To: Mesdaq, Ali; perl-xmllistserv.ActiveState.com
Subject: RE: xml beginner

Ali,

Here is some sample code that works on your XML string:

use XML::XPath;
my $x = XML::XPath->new(xml=>$xml);

foreach my $install ($x->findnodes('//InstallInfo')){
    #extract name
    printf "Name: %s\n",
$install->findvalue('InstallDetection/InstallName');
    #you can even grab the date from the parent tag
    printf "Date: %s\n",
$install->findvalue('ancestor::Installlog/Date');
}

Grant's suggestion of XML::LibXML is an excellent one, and
that is the
module I prefer to use in most cases. The code above would
work on an
XML::LibXML object as well.

One caveat is that these are DOM-based modules, which parse
the document
in memory. If you have an extremely large document and
building a DOM
would exceed your available RAM, you may want to use
XML::Twig instead.

-- 
Mark Thomas 
Internet Systems Architect
_______________________________________
BAE SYSTEMS Information Technology 
2525 Network Place
Herndon, VA  20171  USA 


_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
xml beginner
user name
2006-05-22 20:16:40
Mesdaq, Ali wrote:
> Mark thank you very much for that. This is definitely
getting 
> me on the right road and looks like I can whip
something up 
> today. Question though when I do a:
> 
> Foreach my $install
(x->findnodes('//InstallInfo')) { }
> 
> Inside that foreach loop I want to check each item
inside of 
> the InstallInfo node and do certain conditions if I see

> certain nodes. 

There are a couple of ways to do it. Since XPath has
expressions, you
can use them. Let's say you wanted to print the word
"Eureka" if any
InstallInfo node contains a <gold> node with an
attribute called "type"
that does not have the value "pyrite". You can
put this in your loop:

print "Eureka" if
($install->findnodes('gold[type !=
"pyrite"]'));

Note that you probably don't need to put things like this
in your
loop--in fact you may find that you're not really doing
anything with
the InstallInfo loop itself; it's just a convenient mental
stopping
point. It's amazing many times you can avoid loops
altogether with
well-crafted XPath expressions.

Another way to do it is with XML::LibXML where you have the
full DOM
interface and you have many alternate methods to find
children by name,
compare two nodes, etc. 

- Mark.


_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )