Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Perl / June 2007



Tip: Looking for answers? Try searching our database.

XML - Attribute/Element names and XML::Simple module

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim - 13 Jun 2007 02:50 GMT
Hi...

Here's hoping someone can come to the rescue on this issue for me.

I'm having a bear of a time figuring out why XML::Simple seems to  
want to make both the attribute and child element of a specific  
element into their own elements.  I'm starting to wonder if the XML  
supplied as input is illegally formatted?

As can be seen in my example here, the <platform_specific> element  
has an attribute named "attribute" and another named "platform".  It  
also has a child element named <platform>.  As can be seen in the  
example output, the attribute="something" is properly maintained as  
an atribute to <platform_specific>, while the platform="windows" is  
being forced into becoming a child element of <platform_specific>,  
resulting in two child <platform> elements with the same value.

I know that having an attribute and element of the same name and  
value is redundant, and I'm not certain if it is even legit XML (if  
not, someone let me know where I can find reference to this).   The  
input is not XML I control, so I'm looking at getting the provider to  
modify it before it comes to me if their XML is illegally formatted  
or some such.

My expectation is that XML::Simple would be keeping attributes where  
they started, and the contents of elements where they started.

I started also trying to use XML::Smart today, and got exactly the  
same behaviour, so I'm wondering if the issue is not with the perl  
modules, but possibly with one of their dependencies (they both  
depend upon XML::Parser and xpat right?).

My current script is attempting to simply read in the source xml  
file, and write out a new copy with the same xml.  My ultimate goal  
is, of course, to be able to modify items within this config file,  
but I need to figure out this attribute name versus element name  
issue first.

Any help anyone can provide would be GREATLY appreciated.

Thanks in advance, examples are below.

- - jim -

- -- my input XML file looks like this --

<?xml version="1.0" encoding="utf-8" ?>
<config>
    <platform_specific attribute="something" platform="Windows">
        <platform>Windows</platform>
    </platform_specific>
</config>

- -- my output XML looks like this --

<?xml version="1.0" encoding="utf-8" ?>
<config>
  <platform_specific attribute="something">
    <platform>Windows</platform>
    <platform>Windows</platform>
  </platform_specific>
</config>

- -- the basics of my perl script look like this--

    use XML::Simple ;# qw(:strict);                # use module
    $xml = new XML::Simple;                    # create object
    $data = $xml->XMLin($configFile,
                            KeepRoot         => 1,
                            ForceArray        => 'config',
                        ); # read xml file
                           
    use Data::Dumper;        # Using data dumper here is mostly useful for seeing
    print Dumper($data);        #     the xml struct we've read in while  
developing this
                           
    $output = XMLout($data,
                        XMLDecl     => '<?xml version="1.0" encoding="utf-8" ?>',
                        NoSort         => 0,
                        KeepRoot     => 1,
                    );

- -- the $data object ends up looking like this --

$VAR1 = {
          'config' => [
                        {
                          'platform_specific' => [
                                                   {
                                                     'attribute' =>  
'something',
                                                     'platform' => [
                                                                     
'Windows',
                                                                     
'Windows',
                                                                     
'Windows'
                                                                   ]
                                                   }
                                                 ]
                        }
                      ]
        };

- --------------------------------------------------------------------
JIm | subs@howlingduck.com
4ABC 177B 8352 2D6B 1E10  DAE5 7865 34D5 3139 5D2D
Bob Faist - 13 Jun 2007 03:48 GMT
Here is a script using XML::Twig:

#!/usr/bin/perl

use strict;
use XML::Twig;

my $config_file = $ARGV[0];

my $xTwig = XML::Twig->new( pretty_print => 'indented' );

$xTwig->safe_parsefile($config_file) or die "Failure to parse XML file";

my $xRoot = $xTwig->root();

my $xPlatformSpecific = $xRoot->first_child('platform_specific');

print "attribute value = ", $xPlatformSpecific->att('attribute'), "\n";
print "platform value = ", $xPlatformSpecific->att('platform'), "\n";

print $xTwig->sprint();

Here is a script using XML::LibXML:

#!/usr/bin/perl

use XML::LibXML;

my $config_file = $ARGV[0];

my $parser = XML::LibXML->new();

my $doc = $parser->parse_file($config_file);

my $root = $doc->documentElement();

my ($xPlatformSpecific) = $root->findnodes('platform_specific');

print "attribute value = ", $xPlatformSpecific->getAttribute('attribute'), "\n";
print "platform value = ", $xPlatformSpecific->getAttribute('platform'), "\n";

print $doc->toString();

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 115 lines]
> =9lo5
> -----END PGP SIGNATURE-----
Jim - 14 Jun 2007 01:32 GMT
Thanks Bob

Tried out your samples, and I think I'll probably roll with a Twig  
solution.

It looks a heckuva lot more complicated than XML::Simple, but it  
seems to actually work.  Hopefully I can get past the learning curve ;-)

Are there any really good documentation references for XML::Twig you  
might point me to?

Thanks.

- -jim-

> Here is a script using XML::Twig:
>
[quoted text clipped - 170 lines]
>> =9lo5
>> -----END PGP SIGNATURE-----
Bob Faist - 14 Jun 2007 02:46 GMT
These links contain all the documentation that I am aware of.

http://search.cpan.org/~mirod/XML-Twig-3.29/Twig.pm
http://xmltwig.com/xmltwig/

Hope that helps.

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 196 lines]
> =fFsi
> -----END PGP SIGNATURE-----
Ken Williams - 14 Jun 2007 02:58 GMT
> I'm having a bear of a time figuring out why XML::Simple seems to  
> want to make both the attribute and child element of a specific  
> element into their own elements.  I'm starting to wonder if the XML  
> supplied as input is illegally formatted?

There's nothing wrong with your XML, there's of course no prohibition  
against attributes and children with the same names.  You can run /
usr/bin/xmllint on it to be sure.  If XML::Simple is messing it up,  
that must be a bug.

 -Ken
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.