I try to parse an xml document using XML::Simple perl parsing.
I noticed, if the document looks like:
<?xml version="1.0" encoding="UTF-8"?>
<fields>
<field>
<f1>1234</f1>
<name>MyName1</name>
</field>
</fields>
the result of print(Dumper($ref)); looks as expected:
$VAR1 = {
'field' => {
'f1' => '1234',
'name' => 'MyName1'
}
};
while if I have more than one list in the document:
<?xml version="1.0" encoding="UTF-8"?>
<fields>
<field>
<f1>1234</f1>
<name>MyName1</name>
</field>
<field>
<f1>567</f1>
<name>MyName2</name>
</field>
</fields>
the results looks like:
$VAR1 = {
'field' => {
'MyName1' => {
'f1' => '1234'
},
'MyName2' => {
'f1' => '567'
}
}
};
while expected results would be:
$VAR1 = { [
'field' => {
'f1' => '1234',
'name' => 'MyName1'
},
'field' => {
'f1' => '567',
'name' => 'MyName2'
}
]
};
what options of XML::Simple parser will prevent tag content substitution with the tag reference and use an array of <field> instead?
By default. XML::Simple names hash keys by the values of tags
<name>,<key>and<id>. You can customize this behavior via KeyAttr setting.Thus, the code which produces the structure closest to what you expect is:
And here is the output: