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

I feel shy that I am asking for such stupid question, but my mind is sometimes blocked to understand man XML::Simple.
I am hacking some code, where following xml
... <httpd><![CDATA[ ...1st piece of httpd configuration ]]></httpd> ... <httpd><![CDATA[ ...2nd piece of httpd configuration ]]></httpd> ...
is used in perl via
XML::Simple->new(ForceArray=>['httpd']); ... foreach {@{$conf->{httpd}} #do something }
And now I need add the attribute into httpd element, to distinguish between them, but keep the element order in perl processing.

Replies are listed 'Best First'.
Re: HOWTO XML::Simple
by davidrw (Prior) on Oct 21, 2005 at 12:18 UTC
    Seems like this will do the trick.. But be sure to check the caveats (e.g. ordering) in the XML::Simple for when not to use that module. Also, I came up with the code below by first making the DATA contain <httpd foo="bar"> and Data::Dumper'ing $conf to see what XML::Simple's data structure was.
    use strict; use warnings; use XML::Simple; my $xs = XML::Simple->new(ForceArray=>['httpd']); my $s = do { local $/=undef; <DATA> }; my $conf = $xs->XMLin($s); my $ct = 0; foreach my $httpd ( @{$conf->{httpd}} ){ $ct++; if( ref($httpd) eq 'HASH' ){ $httpd->{number} = $ct; }else{ $httpd = { content=>$httpd, number=>$ct }; } } my $xml = $xs->XMLout($conf); print $xml; __DATA__ <x> <httpd><![CDATA[ ...1st piece of httpd configuration ]]></httpd> <httpd><![CDATA[ ...2nd piece of httpd configuration ]]></httpd> </x>

      The equivalent code in XML::Twig is below. Note that you could write very similar code with XML::LibXML (the difference being that you would use XPath to find the httpd elements and one (or more likely several!) DOM method(s) to add the attribute. If your initial file is big and you don't want to load it entirely in memory you can switch to using XML::Twig twig_handlers or twig_roots options, see the docs if necessary.

      #!/usr/bin/perl -w use strict; use warnings; use XML::Twig; # keep_spaces is not completely necessary, it's just # there to keep the initial formating of the file my $twig= XML::Twig->new( keep_spaces => 1) ->parse( \*DATA) ; my $i=0; foreach my $httpd ( $twig->root->children( 'httpd')) { my $config= $httpd->text; # do something with $config $httpd->set_att( config => ++$i); } $twig->print; __DATA__ <file> <httpd><![CDATA[ ...1st piece of httpd configuration ]]></httpd> <not_httpd /> <httpd><![CDATA[ ...2nd piece of httpd configuration ]]></httpd> </file>
      Thanks, but, accidentally, Dumper($_) positioned in foreach body shows me variable with CDATA content only, the attribute is lost.
      I wish so simple thing, but I am not able to force XML::Simple to put both content and attribute of <httpd> element to some structure, which is an item of array @{$conf->{httpd}} :(
      Perhaps I am too tired or too dumb now, but my XML::Trivial is much more simple to use... I publish it after some real experiences, luckilly to avoid headache of some programmers similar to me.
Re: HOWTO XML::Simple
by planetscape (Chancellor) on Oct 21, 2005 at 12:54 UTC
Re: HOWTO XML::Simple
by murugu (Curate) on Oct 21, 2005 at 11:34 UTC

    Hi pajout,

    Try XML::Twig

    You do have method set_att using that you can set attributes to an element.

    regards,
    Murugesan kandasamy.

      Thanks for advice, but I need to hack existing code only, I cannot do such great jump :)
Re: HOWTO XML::Simple
by pajout (Curate) on Oct 25, 2005 at 11:43 UTC
    Resolution:
    my $xml = XML::Simple->new(ForceArray=>['httpd'], ContentKey=>'-content'); ... my $parsed = ...; foreach {@{$parsed->{httpd}} if (ref($_) eq 'HASH') { #element has attibute(s) #do something with $_->{attr_name} and/or $_->{content} } else { #element has text node only #do something with $_, which is scalar text } }

      I'm pleased you found a solution to your problem. You might be interested in this tutorial which compares using XML::Simple to XML::LibXML and demonstrates that the libxml code can be simpler and more powerful:

        Many thanks for reply. I like libxml & libxslt, but now I have no real experience with it - we GingerAlliance guys use XML::Sablotron :))
        Now I deside to include libxml(2) into my own project, instead of sablotron, so I will study XML::LibXML intensively.
        My wish is to have very simple tool giving parsed short xml configuration files as perl structure, for read-only random access. So, firstly parse all syntacticall objects presented (elements, text nodes, notes, CDATAs, PIs,...) and check well-formedness, optionally check validity (RelaxNG rather than dtd) and inject it into perl without lose of informations. And perl subroutines must know, where are the relevant information pieces, traversing via very simple API, without any expectations about parameters of xml to perl structure transformation.
        XML::Simple is great, but too great to use it with my requirement, imho...