in reply to Regular Expression XML Searching Help
I'm guessing your real intention is to check for well-formedness. The question also implies you're doing XML parsing in a fragile way elsewhere. If I'm right, the following might be a good start to doing things in a way that's more bomb-proof and easier to extend. See XML::LibXML for more.
use XML::LibXML; local $/ = "::FILE::"; my $parser = XML::LibXML->new(); # $parser->recover(1); <-- turn on to "save" many bad docs. while ( my $xml = <DATA> ) { chomp($xml); my $doc = eval { $parser->parse_string($xml) }; if ( $doc ) { print "File $. is valid.\n"; # Do whatever you want with your valid $doc here. } else { print "File $. is NOT valid.\n"; # Deal with bad docs here... } } __DATA__ <order> <customer> <name>Coyote, Ltd.</name> <shipping_info> <address>1313 Desert Road</address> <city>Nowheresville</city> <state>AZ</state> <zip>90210</zip> </shipping_info> </customer> <item> <product id="1111">Acme Rocket Jet Pack</product> <quantity type="each">1</quantity> </item> <item> <product id="2222">Roadrunner Chow</product> <quantity type="bag">10</quantity> </item> </order> ::FILE:: <order> <customer> <name>Coyote, Ltd.</name> <shipping_info> <address>1313 Desert Road</address> <city>Nowheresville</city> <state>AZ</state> <zip>90210</zip> </shipping_info> </customer> <item> <product id="1111">Acme Rocket Jet Pack</product> <quantity type="each">1</quantity> </item> <item> <product id="2222">Roadrunner Chow</product> <quantity type="bag">10</quantity> </item>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regular Expression XML Searching Help
by Anonymous Monk on Jun 16, 2008 at 17:07 UTC | |
|
Re^2: Regular Expression XML Searching Help
by Anonymous Monk on Jun 16, 2008 at 18:57 UTC | |
by Your Mother (Archbishop) on Jun 16, 2008 at 19:12 UTC |