Solved

XML::Simple - Force array

Posted on 2009-04-12
4
694 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

24 Experts available now in Live!

Get 1:1 Help Now