検索
ホームページphp教程php手册XMLドキュメントを解析するための単純なPHPクラス

xml

//原创,可以自由使用,欢迎提出改进意见,

//xml 内の要素
class XMLTag
{
var $parent;//親ノード
var $child;//子ノード
var $attribute;//このノードの属性
var $data; //このノードのデータ
var $TagName;//このノードの名前
var $ Depth;//このノードの深さ、ルートノードは1です
function XMLTag($tag='')
{
$this- >属性 = array();
$this->child = array();
$this->gt;深さ = 0;
$this->parent = null;
$this->data = '' ;
$ this->TagName = $tag;
}
function SetTagName($tag)
{
$this->TagName = $tag;
}
function SetParent(&$parent)
{
$this- >parent = &$parent;
}
function SetAttribute($name,$value)
{
$this->attribute[$name] = $value;
}
function AppendChild(&$child)
{
$i = count($this->child);
$this->child[$i] = &$child;
}
function SetData($data)
{
$this->data= $data ;
}
関数 GetAttr()
{
return $this->attribute;
}
function GetProperty($name)
{
return $this->attribute[$name];
}
function GetData()
{
return $this->data;
}
function GetParent()
{
return $this->gt;parent;
}
function GetChild()
{
return $this->child;
}
function GetChildByName( $name)
{
$total = count($this->child);
for($i=0;$i {
if($this->child) [$i ]->attribute['name'] == $name)
{
return $this->child[$i];
}
}
return null;
}
//タグノードを取得する
関数 GetelementsBytagname($tag) {
$vector = Array();
$tree = & $this;
}
関数 _GetElementByTagName($tree,$tag,&$vector)
{
if($ tree->タグ名 == $tag) array_push($vector,$tree);
$total = count($tree->child) ;
for($i = 0; $i $this->_GetElementByTagName($tree->child[$i],$tag,$vector);
}
}
//xml 文書解析
class XMLDoc
{
var $parser;//xml解析ポインタ
var $XMLTree;//生成されたxmlツリー
var $XMLFile;//解析対象のxmlドキュメント
var $XMLData;// 解析対象のxmlデータ
var $error;//エラーメッセージ
var $NowTag ;//現在指しているノード
var $TreeData;//生成されたxmlツリーのトラバースを待機しているデータ
var $MaxDepth;//このツリーの最大値
の深さ var $encode;//xmlのエンコード方法document
var $chs;//文字変換 ​​
function XMLDoc()
{
//デフォルトの ISO-8859-1 を使用
$this->parser = xml_parser_create ();
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING,0);
xml_set_object($this->parser,&$this);
xml_set_element_handler($this->parser,'_StartElement','_EndElement' );
xml_set_character_data_handler($this->parser,' _CData');
$this->stack = array();
$this->gt;XMLTree = null;
$this->NowTag = null;
$this->MaxDepth = 0;
}
関数LoadFromFile($file)
{
$this->XMLFile = fopen($file,'r');
if(!$this->XMLFile)
{
$this->error = 'xml を開けませんfile';
return false;
}
$this->XMLData = '';
$this->TreeData = '';
return true;
}
function SetXMLData($data)
{
if($ this->XMLFile) fclose($this->XMLFile);
$this->XMLData = $data;
$this->TreeData = '' ;
}
// 新しいノードをツリーに追加します
function AppendChild(&$child)
{
if($this->gt;XMLTree == null)
{
$child-> Depth = 1;
$ this->XMLTree = &$child;
$this ->NowTag = &$this->XMLTree;
}
else
{
   $i = count($this->NowTag->child);
   $this->NowTag->child[$i] = &$child;
            $child->parent = &$this->NowTag;
   $child->depth = $this->NowTag->depth+1;
   unset($this->NowTag);
   $this->NowTag = &$child;
  }
  $this->MaxDepth = ($this->MaxDepth NowTag->depth)?$this->NowTag->depth:$this->MaxDepth;
 }

 //产生一个新的节点
 function &CreateElement($tag)
 {
  $element = new XMLTag($tag);
  return $element;
 }
 function _StartElement($parser,$element,$attribute)
 {
  $tag = new XMLTag();
  $tag->TagName = $element;
  $tag->attribute = $attribute;
  if($this->XMLTree == null)
  {
   $tag->parent = null;
   $tag->depth = 1;
   $this->XMLTree = &$tag;
   $this->NowTag = &$tag;
  }
  else
  {
   $i = count($this->NowTag->child);
   $this->NowTag->child[$i] = &$tag;
   $tag->parent = &$this->NowTag;
   $tag->depth = $this->NowTag->depth+1;
   unset($this->NowTag);
   $this->NowTag = &$tag;
  }
  $this->MaxDepth = ($this->MaxDepth NowTag->depth)?$this->NowTag->depth:$this->MaxDepth;
 }
 function _CData($paraser,$data)
 {
  $this->NowTag->data = $data;
 }
 function _EndElement($parser,$element)
 {
  $parent = &$this->NowTag->parent;
  unset($this->NowTag);
  $this->NowTag = &$parent;
 }
 //开始解析xml文档
 function parse()
 {
  if($this->XMLFile)
  {
   $this->XMLData = '';
   while(!feof($this->XMLFile))
   {
    $this->XMLData .= fread($this->XMLFile,4096);
   }
  }
  fclose($this->XMLFile);
  if($this->XMLData)
  {
   //$this->JudgeEncode();
   if (!xml_parse($this->parser, $this->XMLData))
   {
    $code = xml_get_error_code($this->parser);
    $col = xml_get_current_column_number($this->parser);
    $line = xml_get_current_line_number($this->parser);
          $this->error = "XML error: $col at line $line:".xml_error_string($code);
          return false;
   }
  }
  xml_parser_free($this->parser);
  return true;
    }
    //确定编码方式
    function JudgeEncode()
    {
     $start = strpos($this->XMLData,'     $end = strpos($this->XMLData,'>');
     $str = substr($this->XMLData,$start,$end-$start);
     $pos = strpos($str,'encoding');
     if($pos !== false)
     {
      $str = substr($str,$pos);
      $pos = strpos($str,'=');
      $str = substr($str,$pos+1);
      $pos = 0;
      while(empty($str[$pos])) $pos++;
      $this->encode = '';
      while(!empty($str[$pos]) && $str[$pos] != '?')
      {
       if($str[$pos] != '"' && $str[$pos] != "'")
        $this->encode .= $str[$pos];
       $pos++;
      }
     }
     $this->chs = new Chinese('UTF-8',$this->encode);
    }

    //根据节点名称修改某个节点的值
    function ChangeValueByName($name,$name,$value)
    {
     return $this->_ChangeValueByName($this->XMLTree,$name,$value);
    }
    function _ChangeValueByName($tree,$name,$value)
    {
     if(is_array($tree->attribute))
     {
      while (list($k,$v) = each($tree->attribute))
      {
       if($k = 'name' && $v = $name)
       {
        $tree->data = $value;
        return true;
       }
      }
     }
     $total = count($tree->child);
     for($i = 0;$i     {
      $result = $this->_ChangeValueByName($tree->child[$i],$name,$value);
      if($result == true) break;
     }
     return $result;
    }

通称 // ノード名に従ってツリー内のノードの属性を変更します

Function Changeattrbyname ($ Name, $ Attr, $ Value) {
Return $ This-& Ltree, $ name, $ attr ,$value) ;
}
function _ChangeAttrByName(&$tree,$name,$attr,$value)
{
if(is_array($tree->attribute))
{
while(list($k,$ v) = each ($ tree> atttibution)

$ t = $ this-> _changeattrbyname($ tree>関数getDocumenteLement(){
$ $ this-> xmltree; this-> = 'タグ名.' ';
if(is_array($tree->attribute))
{
while(list($key,$value) = each($ツリー->属性 ))
$this-> total = count($tree->child);
for($i=0;$i {
$this-> _WalkTree($tree->child[$i]) } ->error;
}
’ $head='')
{
$fp = fopen($file,'w');
if(!$ fp)
{
$this->error = '書き込み用にファイルを開けません';
return false ;
}
if(empty($this->TreeData)) $this->WalkTree();
$head = empty($head)?'':$head;
fwrite($fp,$head);
fwrite( $fp,$this->TreeData);
fclose($fp);
return true;
}
}
?>







声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール