ホームページ  >  記事  >  バックエンド開発  >  XML に対する追加、削除、変更、クエリ操作を実装する PHP メソッド

XML に対する追加、削除、変更、クエリ操作を実装する PHP メソッド

墨辰丷
墨辰丷オリジナル
2018-05-22 14:45:351667ブラウズ

この記事では、XML での PHP の追加、削除、変更、クエリ操作の実装を主に紹介し、具体的なケースに基づいて、PHP の XML 形式ファイルの追加、削除、変更、クエリ操作の関連実装テクニックを分析します。詳細は次のとおりです:

ケース:

index.php

<?php
header("content-type:text/html;charset=utf-8");
$xmldom = new DOMDocument();
$xmldom->load("demo2.xml");
//查询学生信息
$stus = $xmldom->getElementsByTagName("学生");
for ($i=0;$i<$stus->length;$i++){
  $stu = $stus->item($i);
  getxmlnode($stu, "姓名");
  getxmlnode($stu, "年龄");
  getxmlnode($stu, "性别");
  getxmlnode($stu, "介绍");
}
function getxmlnode(&$stu,$tagname){
  echo $stuname = $stu->getElementsByTagName($tagname)->item(0)->nodeValue."<br/>";
}
//添加一个学生信息
//addxml($xmldom);
function addxml($xmldom){
  $root = $xmldom->getElementsByTagName("班级")->item(0);
  $ostus = $xmldom->createElement_x_x("学生");
  //添加属性
  $ostus->setAttribute("恋爱状况","热恋中");
  //$ostus->nodeValue="\r\n";
  $root->a($ostus);
  $ostu_name = $xmldom->createElement_x_x("姓名");
  $ostus->a($ostu_name);
  $ostu_name->nodeValue="小娜";
  $ostu_sex = $xmldom->createElement_x_x("性别");
  $ostus->a($ostu_sex);
  $ostu_sex->nodeValue="女";
  $ostu_age = $xmldom->createElement_x_x("年龄");
  $ostus->a($ostu_age);
  $ostu_age->nodeValue="23";
  $ostu_intro = $xmldom->createElement_x_x("介绍");
  $ostus->a($ostu_intro);
  $ostu_intro->nodeValue="高一美女";
  $xmldom->save("demo2.xml");
}
//删除一个学生信息
//del_element($xmldom);
function del_element($xmldom){
  $dstus = $xmldom->getElementsByTagName("学生");
  $laststu = $dstus->item($dstus->length-1);
  $laststu->parentNode->removeChild($laststu);
}
//修改一个学生信息
//update_element($xmldom);
function update_element($xmldom){
  $ustus = $xmldom->getElementsByTagName("学生");
  $ustu = $ustus->item(0);
  $ustu_age = $ustu->getElementsByTagName("年龄")->item(0);
  $ustu_age->nodeValue+=10;
}
//写会到文件中
$xmldom->save("demo2.xml");
?>

demo2.xml

<?xml version="1.0" encoding="UTF-8"?>
<班级>
</班级>

関連する推奨事項:

オブジェクトベースのPHP指向のmysqli拡張ライブラリ

の追加と削除変更と確認操作ツール

php7の操作MongoDB追加、削除、変更、確認詳細な手順

PDO オンmysqlデータベース追加、削除、変更、確認操作手順の詳細な説明

以上がXML に対する追加、削除、変更、クエリ操作を実装する PHP メソッドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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