XML::Simple oddity

Discussion in 'Perl Misc' started by Rob, Feb 15, 2006.

  1. Rob

    Rob Guest

    Hi,

    I just found out that in my xml file, if I have one
    ELEMENT with an ATTRIBUTE, the XML::Simple treats the
    ATTRIBUTE as an ELEMENT....so:

    <?xml version="1.0" standalone="no"?>
    <!DOCTYPE site-catalog SYSTEM "xml_header.dtd">
    <site-catalog>
    <site>
    <name>VNC</name>
    <header>
    <date id = "2006-01-04T23:14:23">
    <fullname>UNKN </fullname>
    <domes>NONE</domes>
    <lat>UNKN</lat>
    </date>
    </header>
    </site>
    </site-catalog>

    here, the DATA::Dumper prints:
    $VAR1 = {
    'site' => {
    'name' => 'VNC',
    'header' => {
    'date' => {
    'lat' => 'UNKN',
    'domes' => 'NONE',
    'id' => '2006-01-04T23:14:23',
    'fullname' => 'UNKN ',
    }
    }
    }
    };



    But, if I have 2 ELEMENTS w/ATTRIBUTES,

    <?xml version="1.0" standalone="no"?>
    <!DOCTYPE site-catalog SYSTEM "xml_header.dtd">
    <site-catalog>
    <site>
    <name>VANCOUVER</name>
    <header>
    <date id = "2005-10-07T10:14:30">
    <fullname>UNKN </fullname>
    <domes>NONE</domes>
    <lat>UNKN</lat>
    </date>
    <date id = "2006-01-04T23:14:23">
    <fullname>UNKN </fullname>
    <domes>NONE</domes>
    <lat>UNKN</lat>
    </date>
    </header>
    </site>
    </site-catalog>

    In this case, the DATA::Dumper prints:
    $VAR1 = {
    'site' => {
    'name' => 'VANCOUVER',
    'header' => {
    'date' => {
    '2005-10-07T10:14:30' => {
    'lat' => 'UNKN',
    'domes' => 'NONE',
    'fullname' => 'UNKN',
    },
    '2006-01-04T23:14:23' => {
    'lat' => 'UNKN',
    'domes' => 'NONE',
    'fullname' => 'UNKN'
    }
    }
    }
    }
    };


    How can I get the XML::Simple to print like the 2nd
    case if I have the xml like the first case?
    ....thanx for any suggestions...


    --Rob
     
    Rob, Feb 15, 2006
    #1
    1. Advertising

  2. Rob

    J. Gleixner Guest

    Rob wrote:

    > How can I get the XML::Simple to print like the 2nd
    > case if I have the xml like the first case?
    > ...thanx for any suggestions...


    Start with the documentation.

    perldoc XML::Simple

    "ForceArray => 1 # in - important

    This option should be set to '1' to force nested elements
    to be represented as arrays even when there is only one.
    ...
    "
     
    J. Gleixner, Feb 16, 2006
    #2
    1. Advertising

  3. Rob wrote:
    > Hi,
    >
    > I just found out that in my xml file, if I have one
    > ELEMENT with an ATTRIBUTE, the XML::Simple treats the
    > ATTRIBUTE as an ELEMENT....so:


    .... snipped code and output

    >
    > How can I get the XML::Simple to print like the 2nd
    > case if I have the xml like the first case?
    > ...thanx for any suggestions...


    I think what you are seeing is a result of the fact that XML::Simple
    treats 'id' as a default KeyAttr. Please read the Note 1 in the
    description of 'KeyAttr => [ list ] # in+out - important' at (or
    perldoc):

    http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm

    The documentation suggests that you set KeyAttr to an empty list if you
    do not want 'id' to be treated as a KeyAttr.

    HTH,
    Mahesh.
     
    Mahesh Asolkar, Feb 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    521
  2. Edwin Knoppert
    Replies:
    0
    Views:
    370
    Edwin Knoppert
    Dec 29, 2005
  3. Mark Johnson

    XML Oddity

    Mark Johnson, Mar 30, 2005, in forum: XML
    Replies:
    3
    Views:
    482
    Richard Light
    Mar 31, 2005
  4. darrel

    XML writer oddity

    darrel, May 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    317
    darrel
    May 23, 2006
  5. Erik Wasser
    Replies:
    5
    Views:
    465
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page