Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1476467
  • 博文数量: 139
  • 博客积分: 10005
  • 博客等级: 中将
  • 技术积分: 4740
  • 用 户 组: 普通用户
  • 注册时间: 2005-03-01 14:39
文章分类

全部博文(139)

文章存档

2010年(63)

2009年(27)

2008年(49)

我的朋友

分类:

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;

阅读(963) | 评论(0) | 转发(0) |
0

上一篇:perl DBI使用详解

下一篇:perl 文件读写

给主人留下些什么吧!~~