A
Avalon1178
Hi,
I'm trying to write a somewhat simple parser using perl to parse an xml
element node, without having to use one of the CPAN libraries like
XML::Simple or SAX, etc. My application is fairly simple and I thought
I could accomplish it using regex. Basically, I just want to extract
the value of an attribute within an xml node.
For example, I have the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<theNode name="blah" type="blah2" opt="blah3">
<desc>Some node</desc>
</theNode>
</main>
In my app, all I want is to extract the attribute value of "type" and
"opt", using regex, but I'm having trouble with it.
For starters, I attempted to extract the attribute value for "name", so
I did the following:
my $regex = qr/^\s*<theNode\s*name=\"(\w+)\".*/;
open(XML, "file.xml");
while(<XML>) {
if (/$regex/) {
print "Regex match: $1\n";
} else {
print "NO Regex match!\n";
}
}
close(XML);
Expecting $1 to be "blah" when it reaches the 3rd line of file.xml
(where file.xml is the sample xml that I posted above), instead I get a
NO Regex match.
Can anyone help? Thanks!
Avalon1178
I'm trying to write a somewhat simple parser using perl to parse an xml
element node, without having to use one of the CPAN libraries like
XML::Simple or SAX, etc. My application is fairly simple and I thought
I could accomplish it using regex. Basically, I just want to extract
the value of an attribute within an xml node.
For example, I have the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<theNode name="blah" type="blah2" opt="blah3">
<desc>Some node</desc>
</theNode>
</main>
In my app, all I want is to extract the attribute value of "type" and
"opt", using regex, but I'm having trouble with it.
For starters, I attempted to extract the attribute value for "name", so
I did the following:
my $regex = qr/^\s*<theNode\s*name=\"(\w+)\".*/;
open(XML, "file.xml");
while(<XML>) {
if (/$regex/) {
print "Regex match: $1\n";
} else {
print "NO Regex match!\n";
}
}
close(XML);
Expecting $1 to be "blah" when it reaches the 3rd line of file.xml
(where file.xml is the sample xml that I posted above), instead I get a
NO Regex match.
Can anyone help? Thanks!
Avalon1178