首页 > 编程语言 >PHP代码示例 - 创建、读取、增加、删除、修改 xml

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

时间:2022-08-22 20:57:48浏览次数:87  
标签: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
From: https://www.cnblogs.com/myhomepages/p/16614203.html

相关文章

  • PHP 发送邮件
    在php网站开发中,发送电子邮件是一个非常普片的需求。比如网站注册功能,当用户注册完成后需要发送电子邮件给用户,提示用户注册成功或者发送验证链接,另外,用户修改账号密码也需......
  • php判断多维数组中是否含有某个值
    在php开发中,我们有时候需要检查PHP多维数组中是否存在某一值Value。在本教程中,我们将演示并描述了如何检查多维数组中是否存在某一值的几种方法。我们将使用以下2种方法来......
  • PHP 多文件上传功能实例讲解
    PHP文件上传功能是非常普片的需求,而在网站开发过程中,我们可以需要同时上传多个文件,这个时候就需要开发出php多文件上传的功能。在本教程中,我将向您展示如何使用PHP实现多个......
  • PHP数组输出为xml的两种常见方法
    很多时候,我们需要将数据以XML格式存储到数据库或文件中,以备后用。为了满足此需求,我们将需要将数据转换为XML并保存XML文件。在本教程中,我们将讨论如何使用PHP将数组转化为x......
  • PHP判断远程文件是否存在的四种方法
    PHPfile_exists()函数用于检查服务器上是否存在一个文件或目录。但是,如果要检查远程服务器上的文件是否存在,file_exists()函数将不可用。本文将介绍php中如何判断远程文......
  • Docke 搭建 apache2 + php8 + MySQL8 环境
    Docker安装执行Docker安装命令curl-fsSLhttps://get.docker.com/|sh启动Docker服务sudoservicedockerstart查看Docker是否正常工作sudo......
  • JS判断是手机访问还是电脑访问,并自动跳转 可在PHP中使用 (2014-12-02 14:41:26)
    方法一、在head内调用以下代码《uaredirect.js》该文件下载地址:http://siteapp.baidu.com/static/webappservice/uaredirect.js百度网盘下载地址:http://pan.baidu.com/......
  • &;转义符在非xml文件中使用后报错
    过去运用xml文件对Mysql数据库进行配置时,在url字段经常需要使用&amp;对&进行转义,而在yml文件或properties文件中,直接用&进行url不同参数间连接即可,无需加转义符示例......
  • Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.
    maven构建报错:Causedby:org.apache.maven.plugin.MojoExecutionException:ErrorassemblingWAR:webxmlattributeisrequired(orpre-existingWEB-INF/web.xmli......
  • Php语法知识点小结
    参考https://www.cnblogs.com/zyf-zhaoyafei/p/4828358.html#tree19isset()、empty()与is_null的区别1、当变量未定义时,is_null()和“参数本身”是不允许作为参数判......