Re: Regular Expression for XML Parsing
- From: Tad J McClellan <tadmc@xxxxxxxxxxxxxx>
- Date: Fri, 28 Dec 2007 00:19:44 GMT
tushar.saxena@xxxxxxxxx <tushar.saxena@xxxxxxxxx> wrote:
I have a set of XML files from which I need to extract some data. The
format of the file is as follows :
<tag1>
<tag3>DATA1</tag3>
</tag1>
<tag2>
<tag3>DATA2</tag3>
</tag2>
I thought you said you had an XML file.
That is not a valid XML file...
I need to extract the DATA part of the xml structure
Note : tag3 can be contained either within tag1 or tag2, but I need to
extract data only from tag1. i.e. DATA1 should be extracted, but not
DATA2
If I want to get both DATA1 and DATA2 I can use a simple regex like :
Using a regular expression to "parse" a non-regular language is
fraught with peril, and nearly always a Bad Idea.
Use a module that understands XML for processing XML data.
Any help would be appreciated !
Assuming that you have actual valid XML in $xml, then:
use XML::Simple;
my $ref = XMLin($xml);
foreach my $child ( @{ $ref->{tag1} } ) {
print "$child->{tag3}\n";
}
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
.
- References:
- Regular Expression for XML Parsing
- From: tushar . saxena
- Regular Expression for XML Parsing
- Prev by Date: Re: FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?
- Next by Date: FAQ 5.13 How can I translate tildes (~) in a filename?
- Previous by thread: Re: Regular Expression for XML Parsing
- Next by thread: new "state" variables... nest properly??
- Index(es):
Relevant Pages
|