程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 使用PHP 5.0 輕松解析XML文檔(2)

使用PHP 5.0 輕松解析XML文檔(2)

編輯:關於PHP編程

文件:SimpleDocumentParser.php
<?php
/**
*=========================================================
*
* @author hahawen(大齡青年)
* @since 2004-12-04
* @copyright Copyright (c) 2004, NxCoder Group
*
*=========================================================
*/
/**
* class SimpleDocumentParser
* use SAX parse xml file, and build SimpleDocumentObject
* all this pachage's is work for xml file, and method is action as DOM.
*
* @package SmartWeb.common.xml
* @version 1.0
*/
class SimpleDocumentParser
{
private $domRootObject = null;
private $currentNO = null;
private $currentName = null;
private $currentValue = null;
private $currentAttribute = null;
public function getSimpleDocument()
{
return $this->domRootObject;
}
public function parse($file)
{
$xmlParser = xml_parser_create();
xml_parser_set_option($xmlParser,XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xmlParser,XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_set_object($xmlParser, $this);
xml_set_element_handler($xmlParser, "startElement", "endElement");
xml_set_character_data_handler($xmlParser, "characterData");
if (!xml_parse($xmlParser, file_get_contents($file)))
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xmlParser)),
        xml_get_current_line_number($xmlParser)));
xml_parser_free($xmlParser);
}
private function startElement($parser, $name, $attrs)
{
$this->currentName = $name;
$this->currentAttribute = $attrs;
if($this->currentNO == null)
{
$this->domRootObject = new SimpleDocumentRoot($name);
$this->currentNO = $this->domRootObject;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved