Solved

XML::Simple - Force array

Posted on 2009-04-12
4
685 Views
Last Modified: 2012-05-06

Consider the data structure below.  The component A is a n array if there is more than one value.  
How do I use force array option to make A always be an array?

 ForceArray => [qw(A)] works for A, but its does not seem to work at all level.  E.g.  
ForceArray => [qw(C)]   does not work for C node.

How do I force array on C?

 


my $data =  {  'mydata' => {'A' => [{'end_date' => '2000-09-28','start_date' => '1999-12-14'},

                                                       {'end_date' => '2000-09-28','start_date' => '1999-12-14'} ]

                                           },

                                           {'B' => 'C' => 'D'}                                            },

                    }

Open in new window

0
Comment
Question by:cpeters5
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
 DB<46> x XMLin(XMLout({  'mydata' => {'A' => [{'end_date' => '2000-09-28','start_date' => '1999-12-14'} ] ,  C=>['B' ] }}),ForceArray=>[qw(A)])  
0  HASH(0x1ad3f34)
   'mydata' => HASH(0x1ad8ae8)
      'A' => ARRAY(0x1adbebc)
         0  HASH(0x19d0fe4)
            'end_date' => '2000-09-28'
            'start_date' => '1999-12-14'
      'C' => 'B'
                                                                                                                                                                                            DB<47> x XMLin(XMLout({  'mydata' => {'A' => [{'end_date' => '2000-09-28','start_date' => '1999-12-14'} ] ,  C=>['B' ] }}),ForceArray=>[qw(C)])  
0  HASH(0x1adb764)
   'mydata' => HASH(0x1ad6178)
      'A' => HASH(0x1acf9d0)
         'end_date' => '2000-09-28'
         'start_date' => '1999-12-14'
      'C' => ARRAY(0x1ad8b38)
         0  'B'
                                                                                                                                                                                            DB<48> x XMLin(XMLout({  'mydata' => {'A' => [{'end_date' => '2000-09-28','start_date' => '1999-12-14'} ] ,  C=>['B' ] }}),ForceArray=>[qw(A C)])
0  HASH(0x1ab9acc)
   'mydata' => HASH(0x1ae3ea8)
      'A' => ARRAY(0x1ad53a4)
         0  HASH(0x1ad828c)
            'end_date' => '2000-09-28'
            'start_date' => '1999-12-14'
      'C' => ARRAY(0x1aec3f4)
         0  'B'
                                           
0
 

Author Comment

by:cpeters5
Comment Utility
Thanks ozo for a quick response.  But I ma not sure if I understand your result.
Let me phrase the question this way.

With the default option, I get the parsed data as
$data = {
              'keywords' => {
                                    'keyword' => 'X'
                                  },
              'sites' => {
                                  'site' => { 'address' => 'some value',
                                                  'city' => 'Mo Town'}
                                                }
                             }
               }

Now, what option do I use to make it looks like the following.
$data = {
              'keywords' => {
                                      [ 'keyword' => 'X']
                                  },
              'sites' => {
                                  'site' => [{ 'address' => 'some value',
                                                  'city' => 'Mo Town'
                                                }]
                             }
               }
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
I assume you meant
              'keywords' => [
                                      { 'keyword' => 'X'}
                                  ],
since
              'keywords' => {
                                      [ 'keyword' => 'X']
                                  },
makes no sense


print Dumper(XMLin( <<END ,ForceArray=>[qw(keywords site)]));
<opt>
  <keywords>
    <keyword>X</keyword>
  </keywords>
  <sites>
    <site>
      <address>some value</address>
      <city>Mo Town</city>
    </site>
  </sites>
</opt>
END
0
 

Author Closing Comment

by:cpeters5
Comment Utility
Thanks again!
pax
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now