By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

How to get input xml file as XMLout. output.

P: 10
Hi ,
I am trying to get an reference of a XML formatted data using XML::Simple::XMLin

and again trying to retrive the XML data as it was before using XML::Simple::XMLout.

But finding a deviation in the format can any pls help me out.


Input file sr.xml
===========
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <scenario>
  3. <counter>
  4.   <counterdef name="HbH-counter" init="1000"> </counterdef>
  5.   <counterdef name="EtE-counter" init="2000"> </counterdef>
  6.   <counterdef name="session-counter" init="10005"> </counterdef>
  7. </counter>
  8. </scenario>
perl code
=======
Expand|Select|Wrap|Line Numbers
  1.  my $data='';
  2.  $file_to_parse='sr.xml';
  3.  my $xml = new XML::Simple;
  4.  my $config = eval { XML::Simple::XMLin("$file_to_parse") };
  5.  $data = $xml->XML::Simple::XMLin("$file_to_parse",keeproot => 1);
  6.  print $xml->XMLout($data,outputfile =>'/var/tmp/Call',keeproot => 1);
  7.  
outputfile('/var/tmp/Call')
==================
Expand|Select|Wrap|Line Numbers
  1. <scenario>
  2.   <counter name="counterdef">
  3.     <EtE-counter init="2000" />
  4.     <HbH-counter init="1000" />
  5.     <session-counter init="10005" />
  6.   </counter>
  7. </scenario>


My expectation was as it was before :

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <scenario>
  3. <counter>
  4.   <counterdef name="HbH-counter" init="1000"> </counterdef>
  5.   <counterdef name="EtE-counter" init="2000"> </counterdef>
  6.   <counterdef name="session-counter" init="10005"> </counterdef>
  7. </counter>
  8. </scenario>
Apr 6 '10 #1
Share this Question
Share on Google+
4 Replies


Expert
P: 70
I can not get XML::Simple to do what you want, but this is simple to do with XML::Twig:

http://search.cpan.org/~mirod/XML-Twig-3.35/

Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3.  
  4. my $file_to_parse = <<EOF;
  5. <?xml version="1.0" encoding="ISO-8859-1" ?>
  6. <scenario>
  7. <counter>
  8. <counterdef name="HbH-counter" init="1000"> </counterdef>
  9. <counterdef name="EtE-counter" init="2000"> </counterdef>
  10. <counterdef name="session-counter" init="10005"> </counterdef>
  11. </counter>
  12. </scenario>
  13. EOF
  14.  
  15. use XML::Twig;
  16. my $t = XML::Twig->new(pretty_print => 'indented');
  17. $t->parse($file_to_parse);
  18. $t->print();
  19.  
  20. __END__
  21.  
  22. <?xml version="1.0" encoding="ISO-8859-1"?>
  23. <scenario>
  24.   <counter>
  25.     <counterdef init="1000" name="HbH-counter"> </counterdef>
  26.     <counterdef init="2000" name="EtE-counter"> </counterdef>
  27.     <counterdef init="10005" name="session-counter"> </counterdef>
  28.   </counter>
  29. </scenario>
Jun 5 '10 #2

P: 10
Thanks for the solution
Jun 29 '10 #3

P: 10
@toolic
I need a solution for below scenario .Is it possible to do the below one without using any Module.




Hi Iam new to perl and need a help to send inputs to an running application :



I am running an application XXXX , and when it starts i am passing a file name called "connect.spb" and after the application connects to the server i am sending filename "accept.spb" as a parameter.

BL31DL385:$ ./XXXX

This is XXXX (Reproducer by Order or RelaY)


XXXX> @connect.spb

XXXX>Opening connect.spb

Script> SET TCPIP /LOCAL_PORT=15331
Initialised TCP Listener on BL31DL385 (port: 15331)


>@accept.spb

XXXX> Opening accept.spb

So basically Input parameters are @connect.spb and @accept.spb.
After getting first string application will connect to the client ,
Then wait for the input and when accept parameter is given communication established succesfully.



MyQuestion##can we automate this using a script which will first run the application XXXX, and then pass the connect.spb and accept.spb as input.
Jun 29 '10 #4

P: n/a
Regarding your first problem re:How to get input xml file as XMLout. output.

Add the parameter "#KeyAttr=>[]," to your Simple::XMLin function call and you should get the same output as your input.

ie.
my $xml = new XML::Simple;
my $config = eval { XML::Simple::XMLi("$file_to_parse") };
$data = $xml->XML::Simple::XMLin("$file_to_parse", KeyAttr=>[], keeproot => 1);
print $xml->XMLout($data,keeproot => 1,XMLDecl => "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
Nov 3 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.