Perl XML parsing

C

citius21j

I would be obliged if you could guide me through this task.
I have several XML output files that I need to parse through perl to
read the data. I am using the XML::parser module but the problem is
that my script (see below) for the following XML gives output as
******************************************************************************************************
XML
**************************************************************************************************
- <principal_info customer_xref_id="HCuddebacke19370666"
relative_CB_num="01">
<CB_format_name>Cuddebacke, Harry</CB_format_name>
<SSN>444000666</SSN>
- <current_address>
<address>90345 Phocis Rd.</address>
<city>Powell</city>
<state>OH</state>
<zip>43065</zip>

**************************************************************************************************
OUTPUT

***************************************************************************************************
customer_xref_id=HCuddebacke19370666
relative_CB_num=01
Cuddebacke, Harry 444000666 90345 Phocis Rd. Powell OH 43065

How could I modify it to instead show
customer_xref_id=HCuddebacke19370666
relative_CB_num=01
CB_format_name=Cuddebacke, Harry
SSN=444000666
address=90345 Phocis Rd.
city=Powell
state=OH
zip=43065

****************************************************************************************************
Script is as follows
***************************************************************************************************
#!/usr/bin/perl

use strict;

use warnings;

use CGI qw:)cgi);

# ---------------------------------------------

my $req = new CGI;

print $req->header();



use XML::parser;

my $file = "output2.xml";

my $parser = new XML::parser(ErrorContext => 2);

$parser->setHandlers(Start => \&start_handler,

End => \&end_handler,

Char => \&char_handler);

$parser->parsefile($file);

# ---------------------------------------------

#

# The handlers for the XML Parser.

#

sub start_handler

{

my $expat = shift; my $element = shift;

# element is the name of the tag



# Handle the attributes

while (@_) {

my $att = shift;

my $val = shift;

i f($element eq "SOAP:Envelope"){

$att="";

$val="";

}

else{

print "$att=$val <br>";}

}

}

sub end_handler

{

my $expat = shift; my $element = shift;

print "\n\n";

}



sub char_handler

{

my ($p, $data) = @_;

print $data;

}******************************************************************************************************



I am looking forward to your help and am sorry to bother you in this
regard. Thanking you for your time. Kindly reply at your earliest
convenience.
 
C

citius21j

1. If I save the element name in global variable in the start_handler,
then print the element name and the data in char_handler, I get the
element name multiple times, of which only once it has the data, Also
this causes the element name to be printed after the attritube and
value

#snippet
use XML::parser;
my $parser=new XML::parser(ErrorContent=>2);

$parser->setHandlers(Start => \&start_handler,
End => \&end_handler,
Char => \&char_handler);

$parser->parsefile($file);

my $globalvar;
sub start_handler
{
my $expat = shift; my $element = shift;
$globalvar=$element;
while (@_) {
my $att = shift;
my $val = shift;
print "$att=$val <br>";
}

}
sub end_handler
{
my $expat = shift; my $element1 = shift;
}

sub char_handler
{
my ($p, $data) = @_;

print "$globalvar : $data <br> ";
}

#end snippet

Returns Output Snippet (see SSN: address: etc repeated)

num_principals=3
principal_list :
customer_xref_id=HCuddebacke19370666
relative_CB_num=01
principal_info :
CB_format_name : Cuddebacke, Harry
CB_format_name :
SSN : 444000666
SSN :
current_address :
address : 90345 Phocis Rd.
address :
city : Powell
city :
state : OH
state :
zip : 43065
zip :
zip :

For _DATA_ (input)
<?xml version="1.0" ?>
<principal_list num_principals="3">
- <principal_info customer_xref_id="HCuddebacke19370666"
relative_CB_num="01">
<CB_format_name>Cuddebacke, Harry</CB_format_name>
<SSN>444000666</SSN>
- <current_address>
<address>90345 Phocis Rd.</address>
<city>Powell</city>
<state>OH</state>
<zip>43065</zip>
</current_address>

_DATA_ (end of input)

Please advise. I apologize if the format is not as per the guidelines.
Will try to adapt to it soon.
Looking forward to your help.
 
C

citius21j

The server our webpage is hosted with doesnt have the XML::Simple
module installed. Please help me with the XML::parser module. Looking
forward to your reply
 
C

citius21j

The problem with the second method is
a)Since it is reading element from end handle...Elements like
current_address are output in the end. I would want them to show on
top..before the address
b) Also text in CDATA doesnt get parsed

_Output Snippet (Start)
CB_format_name : Cuddebacke, Harry
SSN : 444000666
address : 90345 Phocis Rd
city : Powell
state : OH
zip : 43065
current_address:

Output Snippet(End)

Please advise. Looking forward to your reply
 
C

citius21j

You may be getting multiple calls to char_handler, save the data from
char_handler, appending the data passed in
each call to a global variable. In end_handler, print the element name,

data, and then clear the data global variable.

When I follow this approach, Multiple calls to char_handler is
eleminated but as it is reading ElementTypes from end handler, the
parent element type is displayed after the child. Which is not how I
want the output displayed.
Also, data from "<![CDATA[ " is not captured.

Could someone please guide me how to parse xml data using the
XML::parser to display the parent element type followed by the
attribute and values and then the child element.

I apologize for not having posted a proper followup before.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top