Solved

XML::Simple - Force array

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filtering a file to table 9 101
rename outfile before writing 2 72
instmodsh - listing perl modules 2 78
Vb script to unzip a files and rename the files 5 90
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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