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
    That is a better solution!!!
    Thanks for the help!
Re^2: Regular Expression XML Searching Help
by Anonymous Monk on Jun 16, 2008 at 18:57 UTC
    I can't install the module XML::LibXML on this windows box, is there any other Perl module that would work with this code example?

      Not directly, no. But you could try to install XML::Twig (or one of the other good ones) and try to adapt the recipe. Even an eval around an XML::Simple::XMLin() might work. I don't recommend the module but if you've got it already...

      I'm no expert on Win installs but you could try to install the C lib for libxml before trying to install the Perl modules. Might be the only problem. There is some really good work lately with Strawberry Perl to make Perl behave more like it does on other OSes. If you don't have it, try it maybe(?).