nodeValue=$_content;"."/> nodeValue=$_content;".">

Maison  >  Article  >  développement back-end  >  Comment modifier XML en php

Comment modifier XML en php

藏色散人
藏色散人original
2020-11-20 09:59:001820parcourir

Comment modifier XML en PHP : créez d'abord un fichier d'exemple de code ; puis modifiez la valeur du nœud via la méthode "$new->nodeValue=$_content;".

Comment modifier XML en php

Recommandé : "Tutoriel vidéo PHP"

création, ajout, suppression de php, Modifier XML

Créer une méthode XML :

<?php
$xmlpatch = &#39;index.xml&#39;;
$_id = &#39;1&#39;;
$_title = &#39;title1&#39;;
$_content = &#39;content1&#39;;
$_author = &#39;author1&#39;;
$_sendtime = &#39;time1&#39;;
$_htmlpatch = &#39;1.html&#39;;
 
$doc = new DOMDocument(&#39;1.0&#39;, &#39;utf-8&#39;);
$doc -> formatOutput = true;
 
$root = $doc -> createElement(&#39;root&#39;);//新建节点
 
$index = $doc -> createElement(&#39;index&#39;);//新建节点
 
$url = $doc -> createAttribute(&#39;url&#39;);//新建属性
$patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
$url -> appendChild($patch);//将$patch文本设为$url属性的值
 
$id = $doc -> createAttribute(&#39;id&#39;);
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
 
$title = $doc -> createAttribute(&#39;title&#39;);
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
 
$content = $doc -> createTextNode($_content);//节点值
 
$author = $doc -> createAttribute(&#39;author&#39;);
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
 
$sendtime = $doc -> createAttribute(&#39;time&#39;);
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);
 
$index -> appendChild($id);//将$id设为index节点的属性,以下类同
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
 
$root -> appendChild($index);//设置index为root字节点
 
$doc -> appendChild($root);//设置root为跟节点
 
$doc -> save($xmlpatch);//保存文件
 
echo $xmlpatch . &#39; has create success&#39;;
 
?>

Ajouter un nœud XML

 <?php 
$xmlpatch = &#39;index.xml&#39;; 
$_id = &#39;3&#39;; 
$_title = &#39;title3&#39;; 
$_content = &#39;content3&#39;; 
$_author = &#39;author3&#39;; 
$_sendtime = &#39;time3&#39;; 
$_htmlpatch = &#39;3.html&#39;; 
$doc = new DOMDocument(); 
$doc -> formatOutput = true; 
if($doc -> load($xmlpatch)) { 
$root = $doc -> documentElement;//获得根节点(root) 
$index = $doc -> createElement(&#39;index&#39;); 
$url = $doc -> createAttribute(&#39;url&#39;); 
$patch = $doc -> createTextNode($_htmlpatch); 
$url -> appendChild($patch); 
$id = $doc -> createAttribute(&#39;id&#39;); 
$newsid = $doc -> createTextNode($_id); 
$id -> appendChild($newsid); 
$title = $doc -> createAttribute(&#39;title&#39;); 
$newstitle = $doc -> createTextNode($_title); 
$title -> appendChild($newstitle); 
$content = $doc -> createTextNode($_content); 
$author = $doc -> createAttribute(&#39;author&#39;); 
$newsauthor = $doc -> createTextNode($_author); 
$author -> appendChild($newsauthor); 
$sendtime = $doc -> createAttribute(&#39;time&#39;); 
$newssendtime = $doc -> createTextNode($_sendtime); 
$sendtime -> appendChild($newssendtime); 
$index -> appendChild($id); 
$index -> appendChild($title); 
$index -> appendChild($content); 
$index -> appendChild($url); 
$index -> appendChild($author); 
$index -> appendChild($sendtime); 
$root -> appendChild($index); 
$doc -> save($xmlpatch); 
echo $_id . &#39; has been added in &#39; . $xmlpatch; 
} else { 
echo &#39;xml file loaded error!&#39;; 
} 
?>

Supprimer un nœud XML

<?php 
$xmlpatch = &#39;index.xml&#39;; 
$_id = &#39;3&#39;; 
$doc = new DOMDocument(); 
$doc -> formatOutput = true; 
if($doc -> load($xmlpatch)) { 
$root = $doc -> documentElement; 
$elm = $root -> getElementsByTagName(&#39;index&#39;); 
foreach ($elm as $new) { 
if($new -> getAttribute(&#39;id&#39;) == $_id) { 
if($root -> removeChild($new)) { 
echo $_id . &#39; has been deleted&#39;; 
} else { 
echo $_id . &#39; delete failed&#39;; 
} 
} 
} 
$doc -> save($xmlpatch); 
} else { 
echo &#39;xml file loaded error!&#39;; 
} 
?>

Modifier un nœud XML

 <?php 
$xmlpatch = &#39;index.xml&#39;; 
$_id = &#39;2&#39;; 
$_title = &#39;has been changed&#39;; 
$_content = &#39;has been changed&#39;; 
$doc = new DOMDocument(); 
$doc -> formatOutput = true; 
if($doc -> load($xmlpatch)) { 
$root = $doc -> documentElement; 
$elm = $root -> getElementsByTagName(&#39;index&#39;); 
$checkexist = 0; 
foreach ($elm as $new) { 
if($new -> getAttribute(&#39;id&#39;) == $_id) { 
$new -> setAttribute(&#39;title&#39;, $_title); 
$new -> nodeValue = $_content;//修改节点值,真是太意外了,没想到跟JS一样直接能赋值... 
//$new -> removeChild($new -> nodevalue); 
$checkexist = 1; 
} 
} 
if($checkexist == 0) { 
echo $_id . &#39; is not found in &#39; . $xmlpatch; 
} else { 
$doc -> save($xmlpatch); 
echo $_id . &#39; has been changed&#39;; 
} 
} else { 
echo &#39;xml file loaded error!&#39;; 
} 
?>

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn