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->do***entElement();
my ($xPlatformSpecific) = $root->findnodes('platform_specific');
print "attribute value = ", $xPlatformSpecific->getAttribute('attribute'),
"\n";
print "platform value = ", $xPlatformSpecific->getAttribute('platform'),
"\n";
print $doc->toString();
On 6/12/07, Jim <subs@[EMAIL PROTECTED]
> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> 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@[EMAIL PROTECTED]
> 4ABC 177B 8352 2D6B 1E10 DAE5 7865 34D5 3139 5D2D
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.5 (Darwin)
>
> iD8DBQFGb01O+LYMpAC2iPgRAj43AJ4r0yZQcyB7DP4YLi4mDLJeVMhMxQCfS1DA
> Oa6fZNdAaAAIy1Dfngq0vf0=
> =9lo5
> -----END PGP SIGNATURE-----
>


|