Perl XML: SAX Parsing Error -> Attribute value not printing
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;
I can see a couple of things that might be wrong with your code segment:
- In your
start_element
method, you refer to an undeclared variable$in_books
. This should probably be$in_productOffering
. Tip: if you includeuse strict;
at the top of your module, perl will give an error if you accidentally use an undeclared variable - Your
start_element
method checks forbooks
, but the XML file only hasbks:books
orbook
elements - Your script starts with
#!usr/bin/perl -w
, but this probably requires a slash as third character, i.e. #!**/**usr/bin/perl -w - The SAX parser does not require an XSD file
It looks like you want to print a subset of the DOM tree. Use XML::DOM. See also Why does my XSD file fail to parse with XML::LibXML?
精彩评论