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

How to create an XML file using perl.

crazy4perl
P: 20
Hi All,

I have some doubt related to xml. Actually I want to update a file which is in some format. So I am converting that file using Tap3edit perl module in a hash. Now I m trying to create a XML file using this hash so that I can edit that file and convert it back to the same format (might be there are some better way to edit... any suggestions are welcome).

As there are so many xml modules I m not sure which one will be the best n easy to use. I tried with XML-Dumper . But when I m converting it back using xml2pl it is giving me following error:-

not well-formed (invalid token) at line 186, column 40, byte 7090 at C:/Perl/lib/XML/Parser.pm line 187

I believe I have to specify some schema or DTD for xml but dont know how as I m not much familiar with xml. Here is my peice of code

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Data::Dumper;
  5. use XML::Dumper;
  6. use TAP3::Tap3edit;
  7.  
  8. $Data::Dumper::Indent=1;
  9. $Data::Dumper::Useqq=1;
  10.  
  11. my $dump = new XML::Dumper;
  12. my $filename=shift;
  13. if ( ! $filename ) {
  14.     die "Usage: $0 filename\n";
  15. }
  16. if($filename =~ m/\.xml$/)
  17. {
  18.     print("$filename is an xml file");
  19.  
  20.     # put code to process xml files.
  21.     my $xml = $filename;
  22.     my $abc = $dump->xml2pl( $xml );
  23.     print Dumper ($abc);
  24. }
  25. else
  26. {
  27.     # code to process tap files.
  28.  
  29.     my  $perl = TAP3::Tap3edit->new();
  30.  
  31.     $perl->decode($filename) || die $perl->error;
  32.     my $struct = $perl->structure;
  33.  
  34.     # printing the file
  35.     print Dumper ($struct);
  36.  
  37.     # write to xml file
  38.     my $file = "tap2xml.xml";
  39.     $dump->pl2xml($struct,$file );
  40. }
  41.  
Feb 25 '08 #1
Share this Question
Share on Google+
3 Replies


crazy4perl
P: 20
Actually in the xml file there are some junk characters at line 186.
Feb 25 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
if your XML file is in binary encoding you have to convert it to ASCII before parsing it with XML::Dumper. This is covered in the XML::Dumper documentation. I am not sure if that is the problem or not. Also read the XML::Parser documentation because XML::Dumper calls it and the XML::Parser module is whats retuning the error message.
Feb 25 '08 #3

crazy4perl
P: 20
if your XML file is in binary encoding you have to convert it to ASCII before parsing it with XML::Dumper. This is covered in the XML::Dumper documentation. I am not sure if that is the problem or not. Also read the XML::Parser documentation because XML::Dumper calls it and the XML::Parser module is whats retuning the error message.
Hi Kevin,

Thanks for replying. Actually the xml file contains some junk characters. when I am processing it, after removing those junk character manually, it is processing it sucessfully.

So my question is:- Is there any way to remove (or escape) those junk characters while creating XML file.

Thanks
Raj
Feb 27 '08 #4

Post your reply

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