PHP5 で XML を簡単に解析する_PHP チュートリアル
用 sax 方式的时候,要自己构建3个函数,而且要直接用这三的函数来返回数据,要求较强的逻辑。在处理不同结构的 xml 的时候,还要重新进行构造这三个函数,麻烦!
用 dom 方式,倒是好些,但是他把每个节点都看作是一个 node,,操作起来要写好多的代码,麻烦!
网上有好多的开源的 xml 解析的类库,以前看过几个,但是心里总是觉得不踏实,感觉总是跟在别人的屁股后面。
这几天在搞 Java,挺累的,所以决定换换脑袋,写点 PHP 代码,为了防止以后 XML 解析过程再令我犯难,就花了一天的时间写了下面一个 XML 解析的类,于是就有了下面的东西。
实现方式是通过包装“sax方式的解析结果”来实现的。总的来说,对于我个人来说挺实用的,性能也还可以,基本上可以完成大多数的处理要求。
功能:
1\ 对基本的 XML 文件的节点进行 查询 / 添加 / 修改 / 删除 工作。
2\ 导出 XML 文件的所有数据到一个数组里面。
3\ 整个设计采用了 OO 方式,在操作结果集的时候,使用方法类似于 dom
缺点:
1\ 每个节点最好都带有一个id(看后面的例子),每个“节点名字”=“节点的标签_节点的id”,如果这个 id 值没有设置,程序将自动给他产生一个 id,这个 id 就是这个节点在他的上级节点中的位置编号,从 0 开始。
2\ 查询某个节点的时候可以通过用“|”符号连接“节点名字”来进行。这些“节点名字”都是按顺序写好的上级节点的名字。
使用说明:
运行下面的例子,在执行结果页面上可以看到函数的使用说明
代码是通过 PHP5 来实现的,在 PHP4 中无法正常运行。
由于刚刚写完,所以没有整理文档,下面的例子演示的只是一部分的功能,代码不是很难,要是想知道更多的功能,可以研究研究源代码。
目录结构:
test.xml
xml / SimpleDocumentBase.php
xml / SimpleDocumentNode.php
xml / SimpleDocumentRoot.php
xml / SimpleDocumentParser.php
北京长安街-9999号
文件:test.php
require_once "xml/SimpleDocumentBase.php";
require_once "xml/SimpleDocumentRoot.php";
require_once "xml/SimpleDocumentNode.php";
$test->parse("test.xml");
$dom = $test->getSimpleDocument();
";
";
echo "下面是通过函数getSaveData()返回的整个xml数据的数组";
echo "
";
print_r($dom->getSaveData());
";
echo "下面是通过setValue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容";
echo "
";
$dom->setValue("telphone", "123456789");
echo htmlspecialchars($dom->getSaveXml());
";
echo "下面是通过getNode()函数,返回某一个分类下的所有商品的信息";
echo "
";
$obj = $dom->getNode("cat_food");
$nodeList = $obj->getNode();
foreach($nodeList as $node){
$data = $node->getValue();
echo "商品名:".$data[name]."
";
print_R($data);
print_R($node->getAttribute());
}
";
echo "下面是通过findNodeByPath()函数,返回某一商品的信息";
echo "
";
$obj = $dom->findNodeByPath("cat_food|goods_food11");
if(!is_object($obj)){
echo "该商品不存在";
}else{
$data = $obj->getValue();
echo "商品名:".$data[name]."
";
print_R($data);
print_R($obj->getAttribute());
}
";
echo "下面是通过setValue()函数,给商品\"food11\"添加属性, 然后显示添加后的结果";
echo "
";
$obj = $dom->findNodeByPath("cat_food|goods_food11");
$obj->setValue("leaveword", array("value"=>"这个商品不错", "attrs"=>array("author"=>"hahawen", "date"=>date('Y-m-d'))));
echo htmlspecialchars($dom->getSaveXml());
";
echo "下面是通过removeValue()/removeAttribute()函数,给商品\"food11\"改变和删除属性, 然后显示操作后的结果";
echo "
";
$obj = $dom->findNodeByPath("cat_food|goods_food12");
$obj->setValue("name", "new food12");
$obj->removeValue("desc");
echo htmlspecialchars($dom->getSaveXml());
";
echo "下面是通过createNode()函数,添加商品, 然后显示添加后的结果";
echo "
";
$obj = $dom->findNodeByPath("cat_food");
$newObj = $obj->createNode("goods", array("id"=>"food13"));
$newObj->setValue("name", "food13");
$newObj->setValue("price", 100);
echo htmlspecialchars($dom->getSaveXml());
";
echo "下面是通过removeNode()函数,删除商品, 然后显示删除后的结果";
echo "
";
$obj = $dom->findNodeByPath("cat_food");
$obj->removeNode("goods_food12");
echo htmlspecialchars($dom->getSaveXml());
?>
*================================================
*
* @author hahawen(大龄青年)
* @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 $currentName = null;
private $currentValue = null;
private $currentAttribute = null;
function getSimpleDocument()
{
return $this->domRootObject;
}
{
$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_character_data_handler($xmlParser,
"characterData");
xml_get_current_line_number($xmlParser)));
{
$this->currentName = $name;
$this->currentAttribute = $attrs;
if($this->currentNO == null)
{
$this->domRootObject = new SimpleDocumentRoot($name);
}
else
{
$this->currentNO = $this->currentNO->createNode($name, $attrs);
}
{
if($this->currentName==$name)
$tag = $this->currentNO->getSeq();
$this->currentNO = $this->currentNO->getPNodeObject();
if($this->currentAttribute!=null && sizeof($this->currentAttribute)>0)
$this->currentNO->setValue($name, array('value'=>$this->currentValue, 'attrs'=>$this->currentAttribute));
else
$this->currentNO->setValue($name, $this->currentValue);
}
else
{
$this->currentNO = (is_a($this->currentNO, 'SimpleDocumentRoot'))? null:
$this->currentNO->getPNodeObject();
}
}
{
$this->currentValue = iconv('UTF-8', 'GB2312', $data);
}
function __destruct()
{
unset($this->domRootObject);
}
?>
*=================================================
*
* @author hahawen(大龄青年)
* @since 2004-12-04
* @copyright Copyright (c) 2004, NxCoder Group
*
*=================================================
*/
/**
* abstract class SimpleDocumentBase
* base class for xml file parse
* all this pachage's is work for xml file, and method is action as DOM.
*
* 1\ add/update/remove data of xml file.
* 2\ explode data to array.
* 3\ rebuild xml file
*
* @package SmartWeb.common.xml
* @abstract
* @version 1.0
*/
abstract class SimpleDocumentBase
{
private $values =
array();
{
$this->nodeTag = $nodeTag;
}
{
return $this->nodeTag;
}
$this->values = $values;
}
{
$this->values[$name] = $value;
}
{
return $name==null?
$this->values: $this->values[$name];
}
{
unset($this->values["$name"]);
}
$this->attributes = $attributes;
}
{
$this->attributes[$name] = $value;
}
{
return $name==null? $this->attributes:
$this->attributes[$name];
}
{
unset($this->attributes["$name"]);
}
{
return sizeof($this->nodes);
}
{
$this->nodes[$name]
= $nodeId;
}
{
return $name==null? $this->nodes: $this->nodes[$name];
}
{
$tmpObject = $rootNodeObj->createNodeObject($pId, $name, $attributes);
$key = isset($attributes[id])?
$name.'_'.$attributes[id]: $name.'_'.$this->getNodesSize();
$this->setNode($key, $tmpObject->getSeq());
return $tmpObject;
}
{
$rootNodeObj->removeNodeById($this->getNodeId($name));
if(sizeof($this->nodes)==1)
$this->nodes = array();
else
unset($this->nodes[$name]);
}
{
if($name==null)
{
$tmpList = array();
$tmpIds = $this->getNodeId();
foreach($tmpIds as $key=>$id)
$tmpList[$key] = $rootNodeObj->getNodeById($id);
return $tmpList;
}
else
{
$id = $this->getNodeId($name);
if($id===null)
{
$tmpIds = $this->getNodeId();
{
if(strpos($key, $name)==0)
{
$id = $tid;
break;
}
}
}
return $rootNodeObj->getNodeById($id);
}
}
{
$pos = strpos($path, '|');
if($pos<=0)
{
return $this->getNode($path);
}
else
{
$pos));
$tmpObj->findNodeByPath(substr($path,
$pos+1)):
null;
}
}
{
$data = $this->values;
if(sizeof($this->attributes)>0)
$nodeList = $this->getNode();
if($nodeList==null)
foreach($nodeList as $key=>$node)
{
$data[$key] = $node->getSaveData();
}
}
public function getSaveXml($level=0)
{
= str_pad("",
$level, "\t");
$str = "$prefixSpace<$this->nodeTag";
$str .= " $key=\"$value\"";
foreach($this->values as $key=>$value){
{
$str .= "$prefixSpace\t<$key";
}
else
$str .= "$prefixSpace\t<$key";
}
$tmpStr = trim(trim($tmpStr, "\r\n"));
$str .= $node->getSaveXml($level+1)."\r\n";
$str .= "$prefixSpace$this->nodeTag>";
}
{
unset($this->nodes, $this->attributes, $this->values);
?>
*==============================================
*
* @author hahawen(大龄青年)
* @since 2004-12-04
* @copyright Copyright (c) 2004, NxCoder Group
*
*==============================================
*/
/**
* class SimpleDocumentRoot
* xml root class, include values/attributes/subnodes.
* all this pachage's is work for xml file, and method is action as DOM.
*
* @package SmartWeb.common.xml
* @version 1.0
*/
{
private $prefixStr = '';
private $nodeLists = array();
{
parent::__construct($nodeTag);
}
{
$seq = sizeof($this->nodeLists);
$tmpObject = new SimpleDocumentNode($this,
$pNodeId, $name, $seq);
$tmpObject->setAttributes($attributes);
return $tmpObject;
}
{
if(sizeof($this->nodeLists)==1)
$this->nodeLists = array();
else
unset($this->nodeLists[$id]);
}
{
return $this->nodeLists[$id];
}
{
return $this->createNodeByName($this, $name, $attributes, -1);
}
{
return $this->removeNodeByName($this, $name);
}
{
return $this->getNodeByName($this, $name);
}
{
$prefixSpace = "";
$str = $this->prefixStr."\r\n";
return $str.parent::getSaveXml(0);
}
}
?>
*===============================================
*
* @author hahawen(大龄青年)
* @since 2004-12-04
* @copyright Copyright (c) 2004, NxCoder Group
*
*===============================================
*/
/**
* class SimpleDocumentNode
* xml Node class, include values/attributes/subnodes.
* all this pachage's is work for xml file, and method is action as DOM.
*
* @package SmartWeb.common.xml
* @version 1.0
*/
class SimpleDocumentNode extends SimpleDocumentBase
{
private $seq = null;
private $rootObject = null;
private $pNodeId = null;
{
parent::__construct($nodeTag);
$this->rootObject = $rootObject;
$this->pNodeId = $pNodeId;
$this->seq = $seq;
}
{
return ($this->pNodeId==-1)?
$this->rootObject:
$this->rootObject->getNodeById($this->pNodeId);
}
return $this->seq;
}
{
return $this->createNodeByName($this->rootObject,
$name, $attributes,
$this->getSeq());
}
{
return $this->removeNodeByName($this->rootObject, $name);
}
public function getNode($name=null)
{
return $this->getNodeByName($this->rootObject,
$name);
}
}
?>
下面是通过函数getSaveData()返回的整个xml数据的数组
Array
(
[name] => 华联
[address] => 北京长安街-9999号
[desc] => 连锁超市
[cat_food] => Array
(
[attrs] => Array
(
[id] => food
)
[goods_food11] => Array
(
[name] => food11
[price] => 12.90
[attrs] => Array
(
[id] => food11
)
)
[goods_food12] => Array
(
[name] => food12
[price] => 22.10
[desc] => Array
(
[value] => 好东西推荐
[attrs] => Array
(
[creator] => hahawen
)
)
[attrs] => Array
(
[id] => food12
)
)
)
[cat_1] => Array
(
[goods_tel21] => Array
(
[name] => tel21
[price] => 1290
[attrs] => Array
(
[id] => tel21
)
)
)
[cat_coat] => Array
(
[attrs] => Array
(
[id] => coat
)
[goods_coat31] => Array
(
[name] => coat31
[price] => 112
[attrs] => Array
(
[id] => coat31
)
)
[goods_coat32] => Array
(
[name] => coat32
[price] => 45
[attrs] => Array
(
[id] => coat32
)
)
)
[special_hot] => Array
(
[attrs] => Array
(
[id] => hot
)
[goods_0] => Array
(
[name] => hot41
[price] => 99
)
)
)
下面是通过setValue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容
北京长安街-9999号
下面是通过getNode()函数,返回某一个分类下的所有商品的信息
商品名:food11
Array
(
[name] => food11
[price] => 12.90
)
Array
(
[id] => food11
)
商品名:food12
Array
(
[name] => food12
[price] => 22.10
[desc] => Array
(
[value] => 好东西推荐
[attrs] => Array
(
[creator] => hahawen
)
)
)
Array
(
[id] => food12
)
下面是通过findNodeByPath()函数,返回某一商品的信息
商品名:food11
Array
(
[name] => food11
[price] => 12.90
)
Array
(
[id] => food11
)
下面是通过setValue()函数,给商品"food11"添加属性, 然后显示添加后的结果
北京长安街-9999号
下面是通过removeValue()/removeAttribute()函数,给商品"food11"改变和删除属性, 然后显示操作后的结果
北京长安街-9999号
下面是通过createNode()函数,添加商品, 然后显示添加后的结果
北京长安街-9999号
< /グッズ>
スペシャル>
ショップ>
以下はremoveNode()関数でプロダクトを削除し、削除後の結果を表示します
<ショップ>
<名前>花蓮名前>
<住所>北京長安街9999号住所>
< ;goods id="food13">
< id="tel21">
;
hot41名前>
99価格>
グッズ>
スペシャル>
ショップ>
www.bkjia.com

ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Go 言語は、クロージャとリフレクションという 2 つの動的関数作成テクノロジを提供します。クロージャを使用すると、クロージャ スコープ内の変数にアクセスでき、リフレクションでは FuncOf 関数を使用して新しい関数を作成できます。これらのテクノロジーは、HTTP ルーターのカスタマイズ、高度にカスタマイズ可能なシステムの実装、プラグイン可能なコンポーネントの構築に役立ちます。

C++ 関数の名前付けでは、読みやすさを向上させ、エラーを減らし、リファクタリングを容易にするために、パラメーターの順序を考慮することが重要です。一般的なパラメータの順序規則には、アクション-オブジェクト、オブジェクト-アクション、意味論的な意味、および標準ライブラリへの準拠が含まれます。最適な順序は、関数の目的、パラメーターの種類、潜在的な混乱、および言語規約によって異なります。

効率的で保守しやすい Java 関数を作成するための鍵は、シンプルに保つことです。意味のある名前を付けてください。特殊な状況に対処します。適切な可視性を使用してください。

C++ 関数のデフォルト パラメーターの利点には、呼び出しの簡素化、可読性の向上、エラーの回避などがあります。欠点は、柔軟性が限られていることと、名前の制限があることです。可変引数パラメーターの利点には、無制限の柔軟性と動的バインディングが含まれます。欠点としては、複雑さの増大、暗黙的な型変換、デバッグの難しさなどが挙げられます。

1. SUM 関数は、列またはセルのグループ内の数値を合計するために使用されます (例: =SUM(A1:J10))。 2. AVERAGE 関数は、列またはセルのグループ内の数値の平均を計算するために使用されます (例: =AVERAGE(A1:A10))。 3. COUNT 関数。列またはセルのグループ内の数値またはテキストの数をカウントするために使用されます。例: =COUNT(A1:A10)。 4. IF 関数。指定された条件に基づいて論理的な判断を行い、結果を返すために使用されます。対応する結果。

カスタム PHP 関数と定義済み関数の違いは次のとおりです。 スコープ: カスタム関数はその定義のスコープに限定されますが、事前定義関数はスクリプト全体からアクセスできます。定義方法: カスタム関数は function キーワードを使用して定義されますが、事前定義関数は PHP カーネルによって定義されます。パラメータの受け渡し: カスタム関数はパラメータを受け取りますが、事前定義された関数はパラメータを必要としない場合があります。拡張性: カスタム関数は必要に応じて作成できますが、事前定義された関数は組み込みで変更できません。

C++ の例外処理は、特定のエラー メッセージ、コンテキスト情報を提供し、エラーの種類に基づいてカスタム アクションを実行するカスタム例外クラスを通じて強化できます。 std::Exception から継承した例外クラスを定義して、特定のエラー情報を提供します。カスタム例外をスローするには、throw キーワードを使用します。 try-catch ブロックでdynamic_castを使用して、キャッチされた例外をカスタム例外タイプに変換します。実際の場合、open_file 関数は FileNotFoundException 例外をスローします。例外をキャッチして処理すると、より具体的なエラー メッセージが表示されます。

このチュートリアルでは、PHPを使用してXMLドキュメントを効率的に処理する方法を示しています。 XML(拡張可能なマークアップ言語)は、人間の読みやすさとマシン解析の両方に合わせて設計された多用途のテキストベースのマークアップ言語です。一般的にデータストレージに使用されます
