Maxim has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks, I have a problem with XML::SIMPLE to write a XML file but the only problem is that it change complete the structure of the original XML file and I don't know how it is possible.
The original file is this:
<?xml version="1.0" encoding="us-ascii"?> <?xml-stylesheet type="text/xsl" href="Adminproduct.xsl"?> <category name="Coffee"> <heading>150 years of know how at the service of a same strate +gy: Taste</heading> <photo>images/bag_legal.jpg</photo> <comments> <paragraph>Roaster since 1851</paragraph> <paragraph>A revolution in the packaging arena</paragraph> </comments> <readmore> <subcategory name="Prestige"> <comments><paragraph>Prestige is a very traditional French + recipe</paragraph> </comments> <photo>images/prestige.jpg</photo> <product>Coffee <channel>D</channel> <channel>A</channel> <product_id>800.55.01</product_id> <brand>Legal</brand> <description>Legal Prestige</description> <conditionning unit="gr">250</conditionning> <packaging_qty>12</packaging_qty> <recipe>/recipes/coffee1.html</recipe> </product> <product>Coffee <channel>D</channel> <channel>A</channel> <product_id>801.55.01</product_id> <brand>Legal</brand> <description>Legal Prestige Boite Ar&#xF4;me</descript +ion> </readmore> <conditionning unit="gr">500</conditionning> <packaging_qty>6</packaging_qty> </product> </subcategory> </category>
I use this:
use strict; use CGI ':standard'; use XML::Simple; my @data = ('C:\coffee.xml'); my $category = XMLin((join'', @data)); XMLout($xml, rootname =>'category', outputfile =>('c:\test4.xml'));
After transforming
<category name="Coffee" heading="150 years of know how at the service +of a same strategy: Taste" photo="images/bag_legal.jpg"> <comments> <paragraph>Roaster since 1851</paragraph> <paragraph>A revolution in the packaging arena</paragraph> </comments> <subcategory name="Brésil"> <comments paragraph="Hearty, smooth and fascinating. Its original +aroma is complex and full of colors." /> <product brand="Legal" description="Brésil" packaging_qty="12" pro +duct_id="802.55.04">Coffee <channel>A</channel> <channel>S</channel> <conditionning unit="gr">250</conditionning> </product> </subcategory> </category>
I am not sure if I have to use other module to write XML File so I can keep the original file structure. Thanks in advance. Maxim

Replies are listed 'Best First'.
Re: Write XML file
by ikegami (Patriarch) on Sep 28, 2004 at 22:07 UTC

    Your answer is in the docs for XML::Simple. It's even marked as "'important' - don't use the module until you understand this one". Specifically, check out ForceArray. You will lose the <?xml...> lines, though. I don't think those are kept in memory.

      Well I try guys. It still the same as before. I will something else. Than XML::simple.

        Here's a working exaple:

        use XML::Simple; my $data = <<'__EOI__'; <?xml version="1.0" encoding="us-ascii"?> <?xml-stylesheet type="text/xsl" href="Adminproduct.xsl"?> <category name="Coffee"> <heading>150 years of know how at the service of a same strate +gy: Taste</heading> <photo>images/bag_legal.jpg</photo> <comments> <paragraph>Roaster since 1851</paragraph> <paragraph>A revolution in the packaging arena</paragraph> </comments> <subcategory name="Prestige"> <comments><paragraph>Prestige is a very traditional French + recipe</paragraph> </comments> <photo>images/prestige.jpg</photo> <product>Coffee <channel>D</channel> <channel>A</channel> <product_id>800.55.01</product_id> <brand>Legal</brand> <description>Legal Prestige</description> <conditionning unit="gr">250</conditionning> <packaging_qty>12</packaging_qty> <recipe>/recipes/coffee1.html</recipe> </product> <product>Coffee <channel>D</channel> <channel>A</channel> <product_id>801.55.01</product_id> <brand>Legal</brand> <description>Legal Prestige Boite Ar&#xF4;me</descript +ion> <conditionning unit="gr">500</conditionning> <packaging_qty>6</packaging_qty> </product> </subcategory> </category> __EOI__ # In older versions: $category = XMLin($data, forcearray=>1); XMLout($category, rootname =>'category', outputfile => \*STDOUT); # In newer versions: #$category = XMLin($data, ForceArray=>1); #XMLout($category, RootName =>'category', OutputFile => \*STDOUT); __END__ output ====== <category name="Coffee"> <subcategory name="Prestige"> <photo>images/prestige.jpg</photo> <product>Coffee <packaging_qty>12</packaging_qty> <product_id>800.55.01</product_id> <conditionning unit="gr">250</conditionning> <brand>Legal</brand> <channel>D</channel> <channel>A</channel> <recipe>/recipes/coffee1.html</recipe> <description>Legal Prestige</description> </product> <product>Coffee <packaging_qty>6</packaging_qty> <product_id>801.55.01</product_id> <conditionning unit="gr">500</conditionning> <brand>Legal</brand> <channel>D</channel> <channel>A</channel> <description>Legal Prestige Boite Ar¶me</description> </product> <comments> <paragraph>Prestige is a very traditional French recipe</paragra +ph> </comments> </subcategory> <photo>images/bag_legal.jpg</photo> <heading>150 years of know how at the service of a same strategy: Ta +ste</heading> <comments> <paragraph>Roaster since 1851</paragraph> <paragraph>A revolution in the packaging arena</paragraph> </comments> </category>
Re: Write XML file
by eric256 (Parson) on Sep 28, 2004 at 22:01 UTC

    You could try an option of NoAttr => 1. That will help make it look right. In general I don't think that XML::Simple is suited well for cases where you want input and output to retain the same format.


    ___________
    Eric Hodges