Solved

XML::Simple - Force array

Posted on 2009-04-12
4
717 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24127288
 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
ID: 24127413
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
ID: 24127513
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
ID: 31569381
Thanks again!
pax
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…

739 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