Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

XML::Simple is doing too much for me! Help please.

by jdtoronto (Prior)
on Jul 15, 2005 at 18:48 UTC ( #475324=perlquestion: print w/ replies, xml ) Need Help??
jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,

I have a simple sub in my module which looks like this:

sub _view_xml { my $self = shift; my $xml = XMLin( "ttscall1.xml" ); print Dumper $xml; my $xml = XMLout( $xml, Rootname => 'campaign' ); print $xml; return $xml; }
The file contains some XML which is in exactly the structure specified for the particular API I need to send it to:
<campaign menuid="10118-2" action="0" > <phonenumbers > <phonenumber number="4167781583" callid="1234abc" > <prompts> <prompt promptid="2" tts="Hello, this is a test." /> </prompts > </phonenumber> </phonenumbers> </campaign>
When I dumpt the hash I get this:
$VAR1 = { 'menuid' => '10118-2', 'action' => '0', 'phonenumbers' => { 'phonenumber' => { 'number' => '4167781583', 'prompts' => { 'prompt' => { 'tts' => 'Hello, this is a test.', 'promptid' => '2' } }, 'callid' => '1234abc' } } };
Which is kinda what I expected. But when I ask for the XML back again, It gets compressed or something, and produces:
<campaign action="0" menuid="10118-2"> <phonenumbers name="phonenumber" callid="1234abc" number="4167781583 +"> <prompts name="prompt" promptid="2" tts="Hello, this is a test." / +> </phonenumbers> </campaign>
How do I get XML::Simple to output the XML in the same form it was to begin with?

What happens when I submit the XML to the API? I get this response:

<?xml version="1.0"?><campaign errorid="16" comment="XML parse error: +PHONENUMBER tag missing or misplaced." />
. And I figure the same thing will happen for the <prompt> as well except it doesn;t get so far as parsing that yet.

Your assistance is appreciated! jdtoronto

Comment on XML::Simple is doing too much for me! Help please.
Select or Download Code
Re: XML::Simple is doing too much for me! Help please.
by shiza (Hermit) on Jul 15, 2005 at 19:01 UTC
    Try using keeproot (if you aren't):

    Example:
    my $xml_obj = XML::Simple->new(Keeproot => 1);
    With Keeproot:
    Without keeproot:
      That changes the look of the HASH, but the resultant XML still comes out 'squashed'. I was explicity forcing the root to be named, so that wasn't an issue. The issue is the phonenumbers - have a look at the original XML, then what gets produced!
      $VAR1 = { 'campaign' => [ { 'menuid' => '10118-2', 'action' => '0', 'phonenumbers' => { 'phonenumber' => { 'number' => '4167781583', 'prompts' => { 'prompt' => { 'tts' => 'Hello, this is a test.', 'promptid' => '2' } }, 'callid' => '1234abc' } } } ] };
      <campaign action="0" menuid="10118-2"> <phonenumbers name="phonenumber" callid="1234abc" number="4167781583 +"> <prompts name="prompt" promptid="2" tts="Hello, this is a test." / +> </phonenumbers> </campaign>
      But thanks for the suggestion! jdtoronto
        Try setting the ForceArray options like this:
        $xml = XMLin("teste.xml", KeepRoot => 1, ForceArray => [ 'phonenumber', 'prompt' ],);
        izut

        surrender to perl. your code, your rules.

Re: XML::Simple is doing too much for me! Help please.
by runrig (Abbot) on Jul 15, 2005 at 19:29 UTC
    Read up on the KeyAttr option (I seem unable to link to the specific section, but just click on the KeyAttr link in the index).
Re: XML::Simple is doing too much for me! Help please.
by merlyn (Sage) on Jul 15, 2005 at 19:56 UTC
Re: XML::Simple is doing too much for me! Help please.
by GrandFather (Sage) on Jul 16, 2005 at 06:45 UTC

    Hold on here. Isn't there something a little dodgy with this code:

    my $xml = XMLin( "ttscall1.xml" ); print Dumper $xml; my $xml = XMLout( $xml, Rootname => 'campaign' );

    $xml declared twice? use strict; helps with this sort of thing.


    Perl is Huffman encoded by design.
      Yes! And although it worked okay in the particular example, I did spot the error and corrected it shortly afterwards.

      jdtoronto

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://475324]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2015-07-07 12:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls