这篇文章我们将讨论如何使用php对xml进行CRUD(创建、读取、更新、删除)操作,CRUD操作通常是数据库的基本数据操作。这里,我们将创建一个简单的PHP应用程序,在XML而不是数据库上执行所有这些操作。
PHP创建XML文件
php创建xml文件最常用的两种方法是使用SimpleXML和DomDocument。这里我们使用DomDocument来创建XML文件。
使用DomDocument生成xml文件,需要用到以下几个方法
1、创建节点:createElement
2、创建节点文本:createTextNode
3、创建节点属性:createAttribute
4、添加节点、或节点文本、节点属性:appendChild
代码示例:
<?php
$xmlpatch = 'index.xml';
$_id = '1';
$_title = 'title1';
$_content = 'content1';
$_author = 'author1';
$_sendtime = 'time1';
$_htmlpatch = '1.html';
$doc = new DOMDocument('1.0', 'utf-8');
$doc -> formatOutput = true;
$root = $doc -> createElement('root');
$index = $doc -> createElement('index');
$url = $doc -> createAttribute('url');
$patch = $doc -> createTextNode($_htmlpatch);
$url -> appendChild($patch);
$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
$content = $doc -> createTextNode($_content);
$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
$sendtime = $doc -> createAttribute('time');
$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 -> appendChild($root);
$doc -> save($xmlpatch);
echo $xmlpatch . ' has create success';
PHP 读取xml文件
使用php读取xml文件有两种方法:
- 使用DomDocument
- 使用simplexml
使用DomDocument读取xml文件代码如下:
<?php
$str = <<<XML
<currencies>
<currency name="US dollar" code_alpha="USD" code_numeric="840" />
<currency name="Euro" code_alpha="EUR" code_numeric="978" />
</currencies>
XML;
$dom = new DOMDocument();
$dom->loadXML($str);
foreach($dom->getElementsByTagName('currency') as $currency)
{
echo $currency->getAttribute('name'), "/n";
echo $currency->getAttribute('code_alpha'), "/n";
echo $currency->getAttribute('code_numeric'), "/n";
echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+/n";
}
?>
使用simplexml读取xml文件代码如下:
<?php
$str = <<<XML
<currencies>
<currency name="US dollar" code_alpha="USD" code_numeric="840" />
<currency name="Euro" code_alpha="EUR" code_numeric="978" />
</currencies>
XML;
$currencies = new SimpleXMLElement($str);
foreach($currencies as $currency)
{
echo $currency['name'], "/n";
echo $currency['code_alpha'], "/n";
echo $currency['code_numeric'], "/n";
echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+/n";
}
?>
PHP 更新xml
更新xml包括添加节点和修改节点。
为xml添加节点示例代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '3';
$_title = 'title3';
$_content = 'content3';
$_author = 'author3';
$_sendtime = 'time3';
$_htmlpatch = '3.html';
$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentElement;
$index = $doc -> createElement('index');
$url = $doc -> createAttribute('url');
$patch = $doc -> createTextNode($_htmlpatch);
$url -> appendChild($patch);
$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
$content = $doc -> createTextNode($_content);
$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
$sendtime = $doc -> createAttribute('time');
$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 . ' has been added in ' . $xmlpatch;
} else {
echo 'xml file loaded error!';
}
?>
为xml修改节点示例代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'has been changed';
$_content = 'has been changed';
$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentElement;
$elm = $root -> getElementsByTagName('index');
$checkexist = 0;
foreach ($elm as $new) {
if($new -> getAttribute('id') == $_id) {
$new -> setAttribute('title', $_title);
$new -> nodeValue = $_content;
$checkexist = 1;
}
}
if($checkexist == 0) {
echo $_id . ' is not found in ' . $xmlpatch;
} else {
$doc -> save($xmlpatch);
echo $_id . ' has been changed';
}
} else {
echo 'xml file loaded error!';
}
?>
PHP删除xml节点
php删除xml节点的方法:首先遍历所有bird节点以及每一个bird节点的所有属性;然后修改节点值;最后通过“removeChild”方法删除节点即可。
$data='<data>
<seg id="A1"/>
<seg id="A5"/>
<seg id="A12"/>
<seg id="A29"/>
<seg id="A30"/>
</data>';
$doc=new SimpleXMLElement($data);
foreach($doc->seg as $seg)
{
if($seg['id'] == 'A12') {
$dom=dom_import_simplexml($seg);
$dom->parentNode->removeChild($dom);
}
}
echo $doc->asXml();
输出结果:
<?xml version="1.0"?>
<data><seg id="A1"/><seg id="A5"/><seg id="A29"/><seg id="A30"/></data>
注意:使用XPath选择特定节点要简单得多。
$segs=$doc->xpath('//seq[@id="A12"]'); if (count($segs)>=1) { $seg=$segs[0]; }
以上就是本文的全部内容,希望对大家的学习有所帮助。更多教程请访问码农之家
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/281719.html