http://www.perlmonks.org?node_id=853291

perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am trying to extract the part of the xml from the server.xml file, the xml is as below
<?xml version="1.0" encoding="UTF-8"?><status>SUCCESS</status> /***********For QA only ****************/ <?xml version="1.0" encoding="UTF-8"?> <request> <visitorID>4d1aebec-543b-41ae-8336-3a30462f5227</visitorID> <reportSuiteID>aolmobilewebdev</reportSuiteID> <pageName>wpt: help_pv</pageName> <pageURL>http://mqa.aol.com/portal/default/help.do?icid=ft_help</pageU +RL> <ipAddress>10.146.175.162</ipAddress> <userAgent>JoeDog/1.00 [en] (X11; I; Siege 2.70)</userAgent> <scXmlVer>1.0</scXmlVer> <timezone>-5</timezone> <channel>us.wptportal</channel> <referrer></referrer> <events>event10</events> <server>qawire-ql26.tweb.aol.com</server> <eVar16>ft_help</eVar16> <prop1>wpt: portal</prop1> <prop2>wpt: Help Page</prop2> <prop3>gmt_5</prop3> <prop10>JoeDog/1.00 [en] (X11; I; Siege 2.70)</prop10> <prop12>http://mqa.aol.com/portal/default/help.do</prop12> <prop13>non-authenticated</prop13> <prop14>no referrer</prop14> <prop24>uaid_na</prop24> <prop49>xml api</prop49> </request>

From the above xml i want to extract or get the xml tag starting from the <request> to </request>
monks,help me out for the above task and suggest me how can i start, i mean what modules i need to use for the above activity.

Replies are listed 'Best First'.
Re: How to get the part of the xml from the server log
by ikegami (Patriarch) on Aug 06, 2010 at 03:32 UTC
    You could remove the first two lines. For example, if $file contains the contents of the file,
    $file =~ s/^(?:.*\n){2}//;
Re: How to get the part of the xml from the server log
by murugu (Curate) on Aug 06, 2010 at 03:40 UTC

    you can use XML::Twig. I am just giving you a skeleton of the code you require.

    use XML::Twig; my $twig= XML::Twig->new( twig_handlers => { request => \&request }, ); $twig->parsefile( 'server.xml'); sub request { # some actions here }

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

      Been pondering the ease of use of the xml modules..which take quite a bit of getting used to.
      I think that for simple node + children extraction it would be easier to use xslt?
      the hardest line to type correctly is: stty erase ^H
Re: How to get the part of the xml from the server log
by ikegami (Patriarch) on Aug 06, 2010 at 03:33 UTC
    You could remove the first two lines. For example, if $file contains the contents of the file,
    $file =~ s/^(?:.*\n){2}//;
Re: How to get the part of the xml from the server log
by Anonymous Monk on Aug 06, 2010 at 15:35 UTC

    There's always the .. operator, like:

    $ perl -ane 'print if /<request>/../<\/request>/' server.xml <request> <visitorID>4d1aebec-543b-41ae-8336-3a30462f5227</visitorID> <reportSuiteID>aolmobilewebdev</reportSuiteID> <pageName>wpt: help_pv</pageName> <pageURL>http://mqa.aol.com/portal/default/help.do?icid=ft_help</pageU +RL> <ipAddress>10.146.175.162</ipAddress> <userAgent>JoeDog/1.00 [en] (X11; I; Siege 2.70)</userAgent> <scXmlVer>1.0</scXmlVer> <timezone>-5</timezone> <channel>us.wptportal</channel> <referrer></referrer> <events>event10</events> <server>qawire-ql26.tweb.aol.com</server> <eVar16>ft_help</eVar16> <prop1>wpt: portal</prop1> <prop2>wpt: Help Page</prop2> <prop3>gmt_5</prop3> <prop10>JoeDog/1.00 [en] (X11; I; Siege 2.70)</prop10> <prop12>http://mqa.aol.com/portal/default/help.do</prop12> <prop13>non-authenticated</prop13> <prop14>no referrer</prop14> <prop24>uaid_na</prop24> <prop49>xml api</prop49> </request>