Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Questions about using XMLin & XMLout

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
Dear Experts,

Here a piece of my codes:
Code:
use Data::Dumper;
use XML::Simple;

my $srce = 'env.xml';
my $data = XMLin($srce);

print Dumper( $data );

foreach my $k1 (keys(%{$data->{entry}})) {
  foreach my $k2 (keys(%{$data->{entry}->{$k1}})) {
    $data->{entry}->{$k1}->{$k2} =~ s/unassigned/cyan/g;
  }
}
my $dest = 'newenv.xml';
XMLout($data, NoAttr=>1, XMLDecl=>1, OutputFile=>$dest);
my $content1 = `cat $srce`;
my $content2 = `cat $dest`;
print "===================================\n";
print "$content1\n";
print "-----------------------------------\n";
print "$content2\n";
print "===================================\n";
And below is a test run:
Code:
% ./xml.pl
$VAR1 = {
          'entry' => {
                     'ec2-ssh-key-name' => {
                                           'content' => 'unassigned'
                                         },
                     'credentials-file' => {
                                           'content' => 'Credentials.properties'
                                         },
                     'ec2-instance' => {
                                       'content' => 'i-1ccb2973'
                                     },
                     'ec2-ssh-key-path' => {
                                           'content' => 'unassigned.pem'
                                         }
                   }
        };
===================================
<?xml version="1.0" [b]encoding="UTF-8"?[/b]>
<properties>
    <entry key="credentials-file">Credentials.properties</entry>
    <entry key="ec2-ssh-key-name">unassigned</entry>
    <entry key="ec2-ssh-key-path">unassigned.pem</entry>
    <entry key="ec2-instance">i-1ccb2973</entry>
</properties>

-----------------------------------
<?xml version='1.0' [b]standalone='yes'?[/b]>
<opt>
  <entry>
    <name>credentials-file</name>
    <content>Credentials.properties</content>
  </entry>
  <entry>
    <name>ec2-instance</name>
    <content>i-1ccb2973</content>
  </entry>
  <entry>
    <name>ec2-ssh-key-name</name>
    <content>cyan</content>
  </entry>
  <entry>
    <name>ec2-ssh-key-path</name>
    <content>cyan.pem</content>
  </entry>
</opt>

===================================

Qustions:

1) How do I retain all the original tags?
2) More importantly, why does the 1st line change? How to keep it the same?

Many thanks!!
 
the outputted XML different from the input XML because you told it to output without attributes, unlike the source file which has attributes.

Check the CPAN docs...
NoAttr => 1 # in+out - handy
When used with XMLout(), the generated XML will contain no attributes. All hash key/values will be represented as nested elements instead.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
Thank you, DMF.

I have tried using different arguments in XMLout and did notice the difference. But the results were still not what I expected.

For instance, if not set NoAttr:

1)
Code:
XMLout($data, XMLDecl=>1, OutputFile=>$dest);
#XMLout($data, OutputFile=>$dest);
my $content1 = `cat $srce`;
my $content2 = `cat $dest`;
print "===================================\n";
print "$content1\n";
print "-----------------------------------\n";
print "$content2\n";
print "===================================\n";

% ./xml.pl
===================================
[COLOR=red]<?xml version="1.0" encoding="UTF-8"?>
<properties>[/color]
    <entry key="credentials-file">Credentials.properties</entry>
    <entry key="ec2-ssh-key-name">unassigned</entry>
    <entry key="ec2-ssh-key-path">unassigned.pem</entry>
    <entry key="ec2-instance">i-1ccb2973</entry>
</properties>

-----------------------------------
[COLOR=red]<?xml version='1.0' standalone='yes'?>
<opt>[/color]
  <entry name="credentials-file">Credentials.properties</entry>
  <entry name="ec2-instance">i-1ccb2973</entry>
  <entry name="ec2-ssh-key-name">cyan</entry>
  <entry name="ec2-ssh-key-path">cyan.pem</entry>
</opt>

As you can see here, the lines in red are so different. And all <entry key> tags are replaced with <entry name> tags.

I'd guess that there must be certain arguments I can use in XMLout so that all tags and values are retained except the one I want to substitute.

Thanks.
 
dunno, bit odd if you are not using any argument that says it will do this.

Have you tried with an XML file that doesn't have an attribute called 'key' incase it's a reserved word or causing conflit.

Does it still rename the attribute?

I'm not an expert on XML, can attributes be anything or is there a particular set you should use to meet a particular standard?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top