Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

XML parsing

by arunshankar.c (Initiate)
on Nov 05, 2012 at 13:21 UTC ( #1002321=perlquestion: print w/replies, xml ) Need Help??
arunshankar.c has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have an XML File as below:
<?xml version="1.0" encoding="UTF-8"?> <ServiceChangeRequest Operation="Activate" SubscriberKey="|IMSI_NUMBER +|" AlternateSubscriberKey="|MSISDN_NUMBER|" Refresh="false"> <RequiredServices> <ServiceDescription ServiceTag="|service|"> <ParameterDesc ParameterTag="MSISDN" Parameter +Value="|MSISDN_NUMBER|"/> <ParameterDesc ParameterTag="SCPId" ParameterV +alue="|SCPID|"/> <ParameterDesc ParameterTag="ServiceClass" Par +ameterValue="|service_class|"/> <ParameterDesc ParameterTag="PromotionPlan" Pa +rameterValue="|promo_plan|"/> </ServiceDescription> <ServiceDescription ServiceTag="VMSS"> <ParameterDesc ParameterTag="VMSMSISDN" Parame +terValue="|VMSMSISDN_NUM|"/> </ServiceDescription> <ServiceDescription ServiceTag="VCFD"/ +> <ServiceDescription ServiceTag="CALW"/ +> <ServiceDescription ServiceTag="AUC"> <ParameterDesc ParameterTag="KI" ParameterValu +e="|KI|"/> </ServiceDescription> <ServiceDescription ServiceTag="CONTENT"/> <ServiceDescription ServiceTag="GPRS"/> <ServiceDescription ServiceTag="CAMEL"/> <ServiceDescription ServiceTag="APNWAP"/> <ServiceDescription ServiceTag="APNMMS"/> </RequiredServices> </ServiceChangeRequest>
In the above XML file, I have pipe '|' delimited entered eg. : |MSISDN_NUMBER| I want to split the file according to pipe delimited format. Below is the code that I have written:
#!/usr/bin/perl open(FILE, 'removed.xml') or die "Can't read file 'filename' [$!]\n"; + while (<FILE>) { $document .= $_ } print "document is $document\n"; @lines = split("\|",$document); for ($i=0;$i< @lines;$i++) { print "$_\n"; }
I have read from an XML file called removed.xml into variable $document. Now the problem is the @lines does not show any output. Can you please suggest a way in which the XML file can be splitted according to '|' pipe delimited format. Sorry: My mistake, I got the output, please apologize,my question is itself wrong Thanks Arun

Replies are listed 'Best First'.
Re: XML parsing
by choroba (Bishop) on Nov 05, 2012 at 13:27 UTC
    In the for-loop, you are printing $_. You probably meant $lines[$i].

    The more Perlish way would be

    print "$_\n" for @lines;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thank you so much choroba :-) I worked for me...
Re: XML parsing
by rjt (Deacon) on Nov 05, 2012 at 13:50 UTC
    perl -nle 's/\|(.+?)\|/print $1/eg' < test.xml



    However, if you need something more robust, you will want to check out one of the many XML modules such as XML::Simple.

      Thank you rjt for providing the one liner..
Re: XML parsing
by sundialsvc4 (Abbot) on Nov 05, 2012 at 15:15 UTC

    I would echo the sentiment that, if you are dealing with an XML document, you should use one of the many tools that are expressly designed to do that.   (XML::LibXML and XML::Twig are my personal favorites.)

    If you are trying to construct a “customized” XML output based on an XML input that you don’t want to have to be terribly “smart” about, then perhaps a general-purpose templating system would be useful.   Template::Tutorial::Datafile is a really good discussion of this notion.   Here, the application program is not specific, not at all, to what it is producing as an output ... it’s just taking its output directly from its input ... but the input file is made to use standard templating constructions.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002321]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2018-04-21 12:10 GMT
Find Nodes?
    Voting Booth?