XML::Simple oddity

R

Rob

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
 
J

J. Gleixner

Rob said:
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.
...
"
 
M

Mahesh Asolkar

Rob said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top