Check error in SAX parser : 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 




Check error in SAX parser
   

use XML::Parser;

$currentLine = 0;

$parser = new XML::Parser(Handlers => {Start => \&start_handler,
        End   => \&end_handler,
        Char  => \&char_handler,
        Proc  => \&proc_handler,
        XMLDecl => \&XMLDecl_handler,
        Final => \&final_handler});
          
$file = "yourName.xml";

eval {
    $parser->parsefile($file);
};

if($@) {
    print "Error in $file: " (substr $@, 0, index($@, ", byte")) "\n";
    exit(1);    
};
  
sub XMLDecl_handler
{
    $xmlString[$currentLine++"<?xml version=\"$_[1]\"?>";
}

sub start_handler
{
    $xmlString[$currentLine= $indent . "<$_[1]";
    for ($i = 2; $i <= $#_ - 1; $i += 2){
        $xmlString[$currentLine.= " " . $_[$i"=\"". $_[$i + 1"\"";
    }
    $xmlString[$currentLine++.= ">";
    $indent .= "    ";
}

sub end_handler
{
    $indent = substr($indent, 0, length($indent4);
    $xmlString[$currentLine++= $indent . "</$_[1]>";
}

sub char_handler
{
    if($_[1=~ /[^ \n\t\r]/g) {
        $xmlString[$currentLine++= $indent . "$_[1]";
    }
}

sub proc_handler
{
    $xmlString[$currentLine++"<?$_[1] $_[2]?>";
}

sub final_handler
{
    for ($i = 0; $i < $currentLine; $i++){
        print $xmlString[$i"\n";
    }
}

   
    
    
  














Related examples in the same category
1.Check node name in SAX paser
2.Register handlers to SAX parser
3.SAX parser handler
4.Converting a comma separated list data source to XML
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.