Converting a comma separated list data source to XML : SAX « XML « Perl

Home
Perl
1.Array
2.CGI
3.Class
4.Data Type
5.Database
6.File
7.GUI
8.Hash
9.Language Basics
10.Network
11.Regular Expression
12.Report
13.Statement
14.String
15.Subroutine
16.System Functions
17.Win32
18.XML
Perl » XML » SAX 
Converting a comma separated list data source to XML
  

#!/perl/bin/perl


use warnings;
use strict;

print <<'HEADER';
Content-Type: text/xml

<?xml version = "1.0"?>
HEADER

print"<contacts>\n\n" );

openNAMES, "names.txt" or die "Error opening names.txt" );

while <NAMES> ) {
   chomp;

   # escape any characters not allowed in XML content.
   s/&/&amp;/;
   s/</&lt;/;
   s/>/&gt;/;
   s/"/&quot;/;
   s/'/&apos;/;
   
   my $last, $first = split/, / );
   
   print"   <contact>\n",
          "      <LastName>$last</LastName>\n",
          "      <FirstName>$first</FirstName>\n",
          "   </contact>\n\n" );
}

closeNAMES );

print"</contacts>\n" );


#File: names.txt
#    Jack, John
#    Jason, Sue
#    Jodd, Bob

   
    
  
Related examples in the same category
1.Check error in SAX parser
2.Check node name in SAX paser
3.Register handlers to SAX parser
4.SAX parser handler
5.Stream XML::Parser
6.The XML::Parser module provides a framework for parsing XML.
7.Using XML::Parser to parse xml file
8.Using XML:Simple to read and store the document
9.XML::Parasr style: Tree
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.