分类:
2009-07-23 14:02:17
use strict;
use XML::DOM;
use XML::DOM::XPath;
######################## global variable ####################################
print $XML::DOM::VERSION."\n";
my$na = ">";
my$rel = XML::DOM::isValidName($na);
if($rel){
print "$rel\n";
}
else{
print "$na invalid\n";
}
######################## Verify the xml file#################################
my$xml_file = "t:/stocks.xml";
unless ((-e $xml_file)){
print "pls input a xml path and file name:\n";
$xml_file =
}
if ($xml_file !~ /\.xml$/) {
die "need a xml file\n";
}
######################## Initialize ########################################
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ($xml_file);
############################ DOCUMENT ######################################
my$element = $doc->getDocumentElement(); #type: ELEMENT_NODE
my @nodes = $doc->findnodes("//stock_quote");
print $_->getNodeName, "\n" foreach (@nodes);
########################### Node ###########################################
$rel = $element->hasChildNodes();
if( $rel ) { print "has children\n";}
#print $element->toString()."\n";
my@list = $element->getChildNodes();
$rel = @list;
print "children amount is $rel\n";
$element->normalize();
foreach my$ea(@list){
if( TEXT_NODE == $ea->getNodeType()){
#$rel = $ea->getNodeName(); # "#text"
$rel = $ea->getNodeValue(); # error
#$rel = $ea->getData(); # error
print "$rel\n" if( $rel ); # not well
}
else{
#print $ea->getNodeName()."\n";
print $ea->getTagName()."\n";
}
}
print $element->getNodeValue(); #not well
#my$type = $element->getNodeType();
#print "node type is $type\n";
#print $element->getNodeName();
######################## Destory ###########################################
# Avoid memory leaks - cleanup circular references for garbage collection
$doc->dispose;
undef $doc;
undef $parser;