PHP xml_set_object() 函数

PHP 教程 · 2019-01-30 21:12:16

定义和用法

xml_set_object() 函数允许在对象中使用 XML 解析器。

如果成功,该函数则返回 TRUE。如果失败,则返回 FALSE。

语法

xml_set_object(parser,object)


参数 描述
parser 必需。规定要使用的 XML 解析器。
object 必需。规定设置解析器的对象。

实例

<?php
class XMLParser
{
var $xmlparser;
function XMLParser()
  {
  $this->xmlparser = xml_parser_create();
  xml_set_object($this->xmlparser, $this);
  xml_set_character_data_handler($this->xmlparser,"char");
  xml_set_element_handler($this->xmlparser, "start_tag","end_tag");
  }
function parse($data)
  {
  xml_parse($this->xmlparser, $data);
  }
function parse_File($xmlfile)
  {
  $fp = fopen($xmlfile, 'r');
  while ($xmldata = fread($fp, 4096))
    {
    if
    (!xml_parse($this->xmlparser, $xmldata))
      {
      //If error
      die( print "ERROR: "
      . xml_error_string(xml_get_error_code($this->xmlparser))
      . "<br />Line: "
      . xml_get_current_line_number($this->xmlparser)
      . "<br />Column: "
      . xml_get_current_column_number($this->xmlparser)
      . "<br />");
      }
    }
  }
function start_tag($xmlparser, $tag, $attributes)
  {
  print $tag . "<br />";
  }
function end_tag(){}
function char($xmlparser,$data)
  {
  echo $data . "<br />";
  }
function close_Parser()
  {
  xml_parser_free($this->xmlparser);
  }
}
$myxmlparser = new XMLParser();
$myxmlparser->parse_File("test.xml");
$myxmlparser->close_parser();
?>


点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html

查看所有标签

Learning Processing

Learning Processing

Daniel Shiffman / Morgan Kaufmann / 2008-08-15 / USD 49.95

Book Description Teaches graphic artists the fundamentals of computer programming within a visual playground! Product Description This book introduces programming concepts in the context of c......一起来看看 《Learning Processing》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具