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

I have this XML string which I need to know the root name to parse the remaining elements. How can I parse the root name? Note: I need to parse several different string with different root names. Sample String: <Meter_Request Name="CREDIT"/>

Replies are listed 'Best First'.
Re: XML Parsing
by Discipulus (Canon) on May 28, 2015 at 06:57 UTC
    Hello, if i understand your question, ie retrieve the root name of the XML (not in your example), you can do it using XML::Twig
    #!perl use strict; use warnings; use XML::Twig; my $twig= XML::Twig->new( ... ); $twig->parse(<DATA>); print $twig->root->gi,"\n";

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Thx, I'll give that a try. I was using LibXML and was able to use KeepRoot option and get the name put was not able to extract it. I'm new to Perl.

Re: XML Parsing
by choroba (Cardinal) on May 28, 2015 at 16:54 UTC
    Using XML::LibXML:
    #!/usr/bin/perl use warnings; use strict; use XML::LibXML; my $xml = 'XML::LibXML'->load_xml( string => '<Meter_Request Name="CRE +DIT"/>' ); my $root_name = $xml->documentElement->getName; print "$root_name\n";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: XML Parsing
by Anonymous Monk on May 28, 2015 at 03:00 UTC