Hello
I have two questions, and I hope someone can help me here, i'm quite stuck and haven't found anything yet in the previously asked questions (maybe I didn't dig deep enough though).
The outline: I'm creating an XML, send it somewhere and receive an answer back in XML. I then want to parse the returned XML to check if any error occured (it'd say so in the XML). So far so good, everything works fine until I want to parse the XML. I've taken the code snippets from the XML::Parser article which is posted here (sorry, can't remember the author), and everything worked fine when I had it in it's own script. I've now tried to get it into my main script, and the script breas with an error in XML::Parser :
syntax error at line 1, column 0, byte 0 at /usr/local/lib/perl5/site_perl/5.6.1/i686-linux/XML/Parser.pm line 185
The XML it returns looks something like the Following (sorry, I can't include the whole XML here):
<Item>
<Phone_no>123456789</Phone_no>
<Content>Message Sent</Content>
</Item>
<Item>
<Phone_no>12345</Phone_no>
<Content>Error: Incorrect phone number</Content>
</Item>
</Packet>
the code i use to parse the XML is the following:
sub parse_return_xml
{
logger("DEBUG","Sub Parse Return XML");
my $message;
my $i=0;
my @phone;
my @content;
my @bad;
my $xml = @_;
logger("DEBUG","$xml");
logger("DEBUG","Creating XML::Parser Object");
my $parser = new XML::Parser(Handlers => {
Start => \&start_tag,
End => \&end_tag,
Char => \&do_char
});
$parser->parse($xml);
sub start_tag {
logger("DEBUG","start_tag");
my ($p, $elt, %atts) = @_;
return unless $elt eq 'Content' || $elt eq 'Phone_no';
$atts{'_str'} = '';
$message = \%atts;
}
sub end_tag {
logger("DEBUG","end_tag");
my ($p, $elt) = @_;
format_message($message) if ($elt eq 'Content' || $elt
+ eq 'Phone_no') && $message;
}
sub do_char {
logger("DEBUG","do_char");
my ($p, $str) = @_;
$message->{'_str'} .= $str;
}
sub format_message
{
logger("DEBUG","Sub Format_message");
logger("DEBUG","\$i = $i");
my $atts = shift;
logger("DEBUG",$atts->{'_str'});
if ($atts->{'_str'} =~ /err/i)
{
$bad[$i] = $phone[$i];
} else {
$bad[$i] = 'holymoly';
}
if ($atts->{'_str'} =~ /[0..9]/)
{
$phone[$i] = $atts->{'_str'};
}
if ($atts->{'_str'} =~ /[a..zA..Z]/)
{
$content[$i] = $atts->{'_str'};
$i++;
}
}
}
the script breaks after calling $parser->parse($xml)..
could the problem be that I declare Subs within a sub already, and therefore it is unable to call these subs?
any help here is greatly Appreciated, and I hope I gave enough information to this
Thanks in Advance
Emanuel
emanuel@xconnect.ch
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.