カスタム PHP クラス (検索/変更) XML document_PHP チュートリアル

WBOY
リリース: 2016-07-21 15:12:34
オリジナル
722 人が閲覧しました

最近、PHP の XML ドキュメント操作について説明した PHP 教育ビデオを見て、DOMDocument クラスについて少し学びました。説明書を自分で確認したところ、すべて英語でしたが理解できませんでした。ただし、XML ノードを検索してノード値を変更するクラスを自分で作成しました。背景の説明が完了しました。次のコードを見てみましょう:

コードをコピーします コードは次のとおりです:

/*

<クラス>
<学籍番号="101">
<名前>孫悟空
<名前>孫興哲
<年齢>猿霊猿精神

<名前 ;/年齢>変身<名前 ;/name>
200

;
public $encoding;
private $seachNode = '';
public $writeBytes = ''; ($xmlFile ='', $version ='1.0', $encoding = 'UTF-8'){
$this ->version = $version;
$this->encoding = $encoding; >xml = 新しい DOMDocument($version, $encoding);
if($xmlFile)$this->xml- >load($xmlFile);
}
function getRootEle($rootTag){
$this-> ;xmlRoot = $this->xml->getElementsByTagName($rootTag)->item(0);
}
function getSeachItem($itemsTag, $seachNode, $seachValue){
$this->items = $ this->xml->getElementsByTagName($itemsTag);
$this->items->length
for($i=0; $iitems->length; $i++ ){
$item = $this->items->item($i);//要素
$node = $item->getElementsByTagName($seachNode);//ノード
for($j = 0; $jlength; $j++){
$subNode = $node->item($j );
if($seachValue == $subNode->nodeValue){
$this->検索ノード = $subNode;
$this->seachItem = $item;
$this->seachValue = $subNode- >nodeValue;
}
}
return ($this-> ;seachNode) ? true : false;
}
関数 update($nodeValue, $nodeTag = '',$append = false , $index = 0){
if($append){
if($nodeTag)
$this ->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue += $nodeValue
else
$this->seachNode->nodeValue += $nodeValue;
}else {
if($nodeTag)
$this->seachItem->getElementsByTagName($nodeTag)->item( $index)->nodeValue = $nodeValue =
else
$this->seachNode->;ノード値 = $nodeValue
}
}
関数 save($filename){
$this->writeBytes = $this ->xml->save($filename); ? true : false;
}
}
$test = new xmlDom('student.xml');
$test->getSeachItem('Student','Age','103');// 朱八潔を検索します。 age=103
$test->update('こぶた Pig', 'Name', false, 1); // Zhu Bajie の 2 番目の名前を次のように変更します。こぶた Pig
$test->save('new .xml'); // 新しいファイルとして保存します





http://www.bkjia.com/PHPjc/326709.html

www.bkjia.com

tru​​e

http://www.bkjia.com/PHPjc/326709.html

技術記事

最近、PHP の XML ドキュメント操作について説明した PHP 教育ビデオを見て、DOMDocument クラスについて少し学びました。説明書を自分で確認したところ、すべて英語でしたが理解できませんでした。しかし、それでも実装するクラスを自分で書きました...





ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!