ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP代码示例 - 创建、读取、增加、删除、修改 xml

2022-08-22 21:04:19  阅读:345  来源: 互联网

标签:xml appendChild index doc 示例 echo PHP id


这篇文章我们将讨论如何使用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文件有两种方法:

  1. 使用DomDocument
  2. 使用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];
}

以上就是本文的全部内容,希望对大家的学习有所帮助。更多教程请访问码农之家

标签:xml,appendChild,index,doc,示例,echo,PHP,id
来源: https://www.cnblogs.com/myhomepages/p/16614203.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有