LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2010, 10:53 AM   #1
Damon_Qu
LQ Newbie
 
Registered: Jun 2010
Posts: 2

Rep: Reputation: 0
XML::Simple


Hi, All
How can i read the xml file,modify it using XML::Simple and save it?
The xml file is as follows
<?xml version="1.0"?>
<configs>

<config name="Damon">
<timeout>300</timeout>
<logdir>/eweiquu/log/</logdir>
<scriptroot>/eweiquu/testscripts/</scriptroot>
<suits></suits>
<testscripts>
<testscript>ft_621.sh</testscript>
<testscript>ft_622.sh</testscript>
</testscripts>
<priority>0</priority>
<remoteaddress>10.33.42.76</remoteaddress>
<remoteuser>root</remoteuser>
<remotepasswd>shroot</remotepasswd>
</config>

<config name="Jack">
<timeout>200</timeout>
<logdir>/jack/log/</logdir>
<scriptroot>/jack/testscripts/</scriptroot>
<suits>
<suit>dhcp</suit>
<suit>ntp</suit>
</suits>
<testscripts>
<testscript>ft_621.sh</testscript>
<testscript>ft_622.sh</testscript>
</testscripts>
<priority>0</priority>
<remoteaddress>10.33.42.44</remoteaddress>
<remoteuser>root</remoteuser>
<remotepasswd>shroot</remotepasswd>
</config>

</configs>

First i use this subroutine to read the config file as follows
sub readConfig{
my ($self,$tester)=@_;
my $xmlfile="../config/OCSTA_config.xml";
my $configs = eval{XMLin($xmlfile)}||die "Error: $!";
my $config=$configs->{"config"}->{$tester};
if(defined $config){
return $config;
}else{
return {};
}
}

and i can change the xml as follows

$config->{'Jack'}->{'timeout'}=300;

but when I using XMLout to save the xml, the format is changed.
the write method is
sub writeConfig{
my($self,$config)=@_;
my $xml=eval{XMLout($config)}||die "Error: $!";
print "The xml is: \n$xml \n";
}

the result is :
<opt>
<Damon>
<logdir>/eweiquu/ocsta/log/</logdir>
<priority>0</priority>
<remoteaddress>10.33.42.76</remoteaddress>
<remotepasswd>shroot</remotepasswd>
<remoteuser>root</remoteuser>
<scriptroot>/eweiquu/ocsta/testscripts/</scriptroot>
<suits></suits>
<testscripts>
<testscript>ft_621.sh</testscript>
<testscript>ft_622.sh</testscript>
</testscripts>
<timeout>300</timeout>
</Damon>
<Jack timeout="300">
<logdir>/jack/ocsta/log/</logdir>
<priority>0</priority>
<remoteaddress>10.33.42.44</remoteaddress>
<remotepasswd>shroot</remotepasswd>
<remoteuser>root</remoteuser>
<scriptroot>/jack/ocsta/testscripts/</scriptroot>
<suits>
<suit>dhcp</suit>
<suit>ntp</suit>
</suits>
<testscripts>
<testscript>ft_621.sh</testscript>
<testscript>ft_622.sh</testscript>
</testscripts>
</Jack>
</opt>

what's wrong with that?

and How can i save $xml to a file ?

Damon
 
Old 07-07-2010, 01:28 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Moved to Programming.

Please put you code in [CODE] tags to make it easier to read. (Advanced editing mode-->Select the text-->click on the "#")
 
Old 07-07-2010, 01:35 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Damon_Qu View Post
... what's wrong with that? ...
First of all, how can we know what your or somebody else's expectations are ? I.e. why are you asking us what is wrong with a file you've produced ?

Also, entering into Yahoo

Perl write XML module

as the very first match yields http://perl-xml.sourceforge.net/faq/ .

You know the mankind invented the FAQ acronym for a reason. The page contains the word "write", so I think it's worth reading.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't locate XML/Simple.pm in @INC (@INC contains RaleighRalley Linux - Newbie 5 12-13-2013 05:43 PM
XML::Simple usage in perl bharatbsharma Programming 2 05-07-2010 05:57 AM
XML::Simple Jorophose Programming 2 09-26-2007 11:01 PM
LXer: XML::Simple for Perl developers LXer Syndicated Linux News 0 02-04-2007 08:21 AM
Perl XML::Simple - Hash Question smaida Programming 2 05-26-2004 04:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration