On Wed, Sep 05, 2012 at 08:26:30PM +0530, Anirban Adhikary wrote: > Hi List,
Hello,
(Note: the first (unquoted) snippet is missing a single-quote :))
> Now in the case of following XML file
>
> <xn:VsDataContainer id="20408112016662">
> <xn:attributes>
> <xn:vsDataType>vsMscServerCell</xn:vsDataType>
> <xn:vsDataFormatVersion>vsData1.0</xn:vsDataFormatVersion>
> <xn:vsMscServerCell>
> <xn:callSourceName>RADIO-IU</xn:callSourceName>
> <xn:cellGrpName>INVALID</xn:cellGrpName>
> <xn:cellType>3GCell</xn:cellType>
> <xn:gci_sai>20408112016662</xn:gci_sai>
> <xn:iDPLNAA>IDN</xn:iDPLNAA>
> <xn:ifCallIn>NO</xn:ifCallIn>
> <xn:ifCallOut>NO</xn:ifCallOut>
> <xn:ifRoamAnalysis>NO</xn:ifRoamAnalysis>
> <xn:isEarlyAssign>EARLYASN</xn:isEarlyAssign>
> <xn:laDegree>0</xn:laDegree>
> <xn:laMinute>0</xn:laMinute>
> <xn:laSecond>0</xn:laSecond>
> <xn:laiCategory>SAI</xn:laiCategory>
> <xn:laiType>HVLR</xn:laiType>
> <xn:latitudeType>NOR</xn:latitudeType>
> <xn:lgDegree>0</xn:lgDegree>
> <xn:lgMinute>0</xn:lgMinute>
> <xn:lgSecond>0</xn:lgSecond>
> <xn:locationIDName>INVALID</xn:locationIDName>
> <xn:locationNumber>117047007000000</xn:locationNumber>
> <xn:locationNumberName>INVALID</xn:locationNumberName>
> <xn:longitudeType>EAST</xn:longitudeType>
> <xn:mnc>FFF</xn:mnc>
> <xn:mscNumber>316530320000</xn:mscNumber>
> <xn:multiAreaStatName>AHPTMS1</xn:multiAreaStatName>
> <xn:radius>0</xn:radius>
> <xn:rncId1>112</xn:rncId1>
> <xn:svrName>AHPTMS1</xn:svrName>
> <xn:tZDSTName>INVALID</xn:tZDSTName>
> <xn:toneName>INVALID</xn:toneName>
> <xn:vlrNumber>316530320000</xn:vlrNumber>
> </xn:vsMscServerCell>
> </xn:attributes>
> </xn:VsDataContainer>
(Note: indentation mine)
> I am using the same code to print the values it shows nothing
> in the screen
>
> use strict;
> use warnings;
> use XML::Twig;
>
> my $twig = XML::Twig->new(TwigHandlers => {VsDataContainer =>
> \&on_VsDataContainer});
>
> sub on_VsDataContainer {
> my($twig, $dc)= @_;
> print $dc->id, "\n";
> my $gci_sai = $dc->field('gci_sai');
> print $gci_sai,"\n";
> my $locationNumber = $dc->field('locationNumber');
> print $locationNumber,"\n";
> $twig->purge;
> }
>
> $twig->parsefile("C:/Users/eamasar/Desktop/xnm/data/WA07B/input/MSCServerCell_201209050400.E2G.xml");
>
> And when I am removing the xn: from the begining of the line from XML file
> it prints the values on screen.
Disclaimer: I am not familiar with XML::Twig.
The xn: prefix is called a namespace prefix. Many XML parsers
handle these in a special way, requiring you to register the
namespaces with the parser.
Your XML snippet doesn't even contain an xmlns attribute to
define the namespace. I don't even know if that is still
considered well-formed XML.
I played around with your code a little bit and managed to get
some output. :)
First, I added the following attribute to the root element of the
XML data:
xmlns:xn="bar"
Normally the xmlns is a URI so that it's easy to be globally
unique. For my purposes anything will do. In your case, you
should determine what the xn: prefix represents and use the
appropriate URI. If it's your own namespace then make something
up. :) If the XML data is coming from a third party then you may
need to tell them to add the xmlns attribute. :) I don't know if
XML::Twig can work around it or not (again, I'm not familiar with
it).
I think you'll have to do a bit more work to get at the
descendants xn:gci_sai and xn:locationNumber. For now I wrote a
separate handler for each, which suffices to get the desired
output. :) You can continue on from there..
use strict;
use warnings;
use XML::Twig;
# Note the $config{map_xmlns} element.
my %config = (
map_xmlns => {
foo => 'bar'
# ^ ^ This needs to match the URI part in the XML
# | document.
# \ This can be anything we want. We use it in our code.
},
TwigHandlers => {
'foo:VsDataContainer' => \&on_VsDataContainer,
# ^ This matches the registered namespace prefix above.
'foo:gci_sai' => \&print_text,
'foo:locationNumber' => \&print_text,
}
);
my $twig = XML::Twig->new(%config);
sub on_VsDataContainer {
my ($twig, $dc) = @_;
print $dc->id, "\n";
}
sub print_text {
my ($twig, $element) = @_;
print trim($element->text()), "\n";
}
$twig->parsefile("bar.xml");
Regards,
--
Brandon McCaig <[email protected]> <[email protected]>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bamccaig.com/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'
signature.asc
Description: Digital signature
