Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Mac > Perl on OSX > Re: XML - Attri...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 5 Topic 972 of 1076
Post > Topic >>

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

by subs@[EMAIL PROTECTED] (Jim) Jun 13, 2007 at 05:32 PM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 do***entation references for XML::Twig you  
might point me to?

Thanks.

- -jim-




On Jun 12, 2007, at 7:48 PM, Bob Faist wrote:

> 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-----
>>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Darwin)

iD8DBQFGcIyz+LYMpAC2iPgRArmBAJ4ljsCpWMiDIiZvvp6Um8rE6CF6vwCfQg+5
bKWTjJqjzQEK54skpM/Dnm8=
=fFsi
-----END PGP SIGNATURE-----
 




 5 Posts in Topic:
XML - Attribute/Element names and XML::Simple module
subs@[EMAIL PROTECTED] (  2007-06-12 18:50:06 
Re: XML - Attribute/Element names and XML::Simple module
bob.faist@[EMAIL PROTECTE  2007-06-12 22:48:30 
Re: XML - Attribute/Element names and XML::Simple module
subs@[EMAIL PROTECTED] (  2007-06-13 17:32:51 
Re: XML - Attribute/Element names and XML::Simple module
ken@[EMAIL PROTECTED] (K  2007-06-13 20:58:55 
Re: XML - Attribute/Element names and XML::Simple module
bob.faist@[EMAIL PROTECTE  2007-06-13 21:46:01 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Dec 5 0:14:59 CST 2008.