I am trying to parse XML in Perl using XML::SAX parser. My query is regarding generating attributes values. Right now I am able to generate only values present inside the tag elements but my goal is to generate:
Element Name: Element Value:
Element Attribute Name: Element Attribute Value:
Element Child Name: Element Child Value
Element Child Attribute Name: Element Child Attribute Value
Here is my books1.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:books"
xmlns:bks="urn:books">
<xsd:element name="books" type="bks:BooksForm"/>
<xsd:complexType name="BooksForm">
<xsd:sequence>
<xsd:element name="book"
type="bks:BookForm"
minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BookForm">
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float" />
<xsd:element name="pub_date" type="xsd:date" />
<xsd:element name="review" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
Here is my sample Books.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2009 sp1 (http://www.altova.com)-->
<bks:books xsi:schemaLocation="urn:books Untitled1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bks="urn:books">
<book id="String">
<author>String</author>
<title>String</title>
<genre>String</genre>
<price>3.14159E0</price>
<pub_date>1967-08-13</pub_date>
<review>String</review>
</book>
</bks:books>
Here is my parser.pl
file:
#!usr/bin/perl -w
use XML::SAX::ParserFactory;
use MyHandler;
my $handler = MyHandler->new();
my $parser = XML::SAX::ParserFactory->parser(Handler => $handler);
$parser->parse_uri("books1.xml")
Here is my MyHandler.pm
module:
package MyHandler;
use base qw(XML::SAX::Base);
my $in_books = 0;
sub start_element {
my ($self,$data) = @_;
if($data->{Name} eq 'bks:books'){
$in_books++;
}
}
sub end_element {
my($self,$data) = @_;
if($data->{Name} eq 'bks:books){
$in_books--;
print "\n";
}
}
sub characters{
my($self,$data) = @_;
if($in_books){
print $data->{Data};
}
}
1;