Home php教程 php手册 PHP操作XML功能齐全的xml操作类

PHP操作XML功能齐全的xml操作类

Jun 13, 2016 am 10:39 AM
class op php xml supply operate of kind programming

编程之家提供一个功能齐全的xml操作类



/* xml操作类 */
class operXml
{
    var $parser;

    public function __construct()
    {
        $this->parser = xml_parser_create();
        xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
        xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);

        xml_set_object($this->parser, $this);
        xml_set_element_handler($this->parser, "tag_open", "tag_close");
        xml_set_character_data_handler($this->parser, "cdata");
    }

    public function parse($xmlStr="",$endtag=true)
    {
     $this->endtag = $endtag;
     $this->xmlStr = $xmlStr;
     $this->tree = new stdClass;
     $this->tree->tag = "root";
     $this->tree->props = new stdClass;
     $this->tree->children = array();
     $this->tree->p = NULL;
     $this->tree->level = -1;
     $this->deep = 0;
     $this->pList = array($this->tree);
     xml_parse($this->parser, $this->xmlStr);
    if(count($this->tree->children)>0)
        $this->root = $this->tree->children[0];
    else
       $this->root = NULL;
    return $this;
   }

   public function tag_open($parser, $tag, $attributes)
   {
  $o = new stdClass;
  $o->p = $this->pList[$this->deep];
  $o->index = count($o->p->children);
  $o->level = $o->p->level 1;
  $o->tag = $tag;
  $o->props = new stdClass;
  while(list($key,$value)=each($attributes))
  $o->props->{$key} = $value;
  $o->value = "";
  $o->children = array();
  array_push($o->p->children,$o);
  $this->deep ;
  $this->pList[$this->deep] = $o;
    }

    public function cdata($parser, $cdata)
    {
        $this->pList[$this->deep]->value = $cdata;
    }

    public function tag_close($parser, $tag)
    {
  $this->deep--;
    }
 
 public function getNodeByProp() // 根据属性名称和值取得节点,
 {       // 参数:属性名称,属性值1,属性值2,属性值3,...
  $args = func_get_args();
  $node = $this->tree;
  for($i=1;$i  {
   $node = $this->_getNodeByProp($node,$args[0],$args[$i]);
   if($node==NULL) break;
  }
  return $node;
 }
 
 public function getChildByTag($node,$tag)  // 取得$node节点下标签为$tag的节点
 {
  for($i=0;$ichildren);$i )
  {
   if($node->children[$i]->tag==$tag)
    return $node->children[$i];
  }
  return NULL;
 }
 
 public function getChildsByTag($node,$tag) // 取得$node节点下标签为$tag的节点,返回节点列表数组
 {
  $rs = array();
  for($i=0;$ichildren);$i )
   if($node->children[$i]->tag==$tag)
    array_push($rs,$node->children[$i]);
  return $rs;
 }
 
 public function addRoot($tag)   // 添加根节点
 {
  $this->tree->children = array();
  $this->root = $this->addChild($this->tree,$tag);
  return $this->root;
 }
 
 public function addChild($node,$tag)  // 在$node节点下添加标签为$tag的节点,并返回添加的节点
 {
  $o = new stdClass;
  $o->p = $node;
  $o->level = $node->level 1;
  $o->index = count($node->children);
  $o->tag = $tag;
  $o->props = new stdClass;
  $o->value = "";
  $o->children = array();
  array_push($node->children,$o);
  return $o;
 }
 
 public function delete($node)  // 删除$node节点
 {
  $p = $node->p;
  array_splice($p->children,$node->index,1);
  for($i=0;$ichildren);$i )
   $p->children[$i]->index = $i;
 }
 
 public function move($dstNode,$srcNode) // 将srcNode移动到$dstNode下面
 {
  $this->delete($srcNode);
  $srcNode->p = $dstNode;
  $srcNode->level = $dstNode->level 1;
  $srcNode->index = count($dstNode->children);
  array_push($dstNode->children,$srcNode);
 }
 
 public function __toString()  // 返回xml格式串
 {
  $s = "";
  for($i=0;$itree->children);$i )
   $s .= $this->traversalNodeToXml($this->tree->children[$i],$this->endtag);
  return $s;
 }

 public function save($xmlFile)  // 保存成xml格式文件
 {
  $content = $this->__toString();
  
  $fp = @fopen($xmlFile,"w") or die("创建文件失败:".$xmlFile);
  @fwrite($fp,$content);
  @fclose($fp);
  @chmod($xmlFile,0777);
 }
 
 private function traversalNodeToXml($treeNode,$endtag)
 {
  $space = "";
  $space = str_pad($s,$treeNode->level*2," ",STR_PAD_LEFT);
  $s = $space."tag;
  while(list($key,$value)=each($treeNode->props))
   $s .= " $key="$value"";
  $childCount = count($treeNode->children);
  if($childCount==0)
  {
   if($treeNode->value!="" || $endtag)
    $s .= ">".$treeNode->value."".$treeNode->tag."> ";
   else
    $s .= "/> ";
   return $s;
  }
  
  $s .= "> ";
  for($i=0;$i   $s .= $this->traversalNodeToXml($treeNode->children[$i],$endtag);
  $s .= $space."".$treeNode->tag."> ";
  return $s;
 }
 
 private function _getNodeByProp($node,$propName,$propValue)
 {
  for($i=0;$ichildren);$i )
  {
   $anode = $node->children[$i];
   if(isset($anode->props) && isset($anode->

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

Java Made Simple: A Beginner's Guide to Programming Power Java Made Simple: A Beginner's Guide to Programming Power Oct 11, 2024 pm 06:30 PM

Java Made Simple: A Beginner's Guide to Programming Power Introduction Java is a powerful programming language used in everything from mobile applications to enterprise-level systems. For beginners, Java's syntax is simple and easy to understand, making it an ideal choice for learning programming. Basic Syntax Java uses a class-based object-oriented programming paradigm. Classes are templates that organize related data and behavior together. Here is a simple Java class example: publicclassPerson{privateStringname;privateintage;

Demystifying C: A Clear and Simple Path for New Programmers Demystifying C: A Clear and Simple Path for New Programmers Oct 11, 2024 pm 10:47 PM

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

See all articles