PHP的dom读写XML~

images.xml文件的格式:

<?xml version="1.0" encoding="utf-8"?>
<root imageWidth="680" imageHeight="345">
  <menu url="a1.html" frame="_blank" imageUrl="http://localhost/jiao/images/top/06.jpg"/>
  <menu url="a2.html" frame="_blank" imageUrl="http://localhost/jiao/images/top/05.jpg"/>
  <menu url="a3.html" frame="_blank" imageUrl="http://localhost/jiao/images/top/04.jpg"/>
  <menu>
   <url>a4.html</url>
 <frame>_blank</frame>
 <imageUrl>http://localhost/jiao/images/top/06.jpg</imageUrl>
  </menu>
  <menu>
   <url>a5.html</url>
 <frame>_blank</frame>
 <imageUrl>http://localhost/jiao/images/top/06.jpg</imageUrl>
  </menu>
  <menu>
   <url>a6.html</url>
 <frame>_blank</frame>
 <imageUrl>http://localhost/jiao/images/top/06.jpg</imageUrl>
  </menu>
</root>

读取:

<?php
$doc = new DOMDocument();
$doc->load( 'images.xml' );
$root = $doc -> documentElement;
echo $root-> getAttribute("imageWidth")."<br>";
echo $root-> getAttribute("imageHeight")."<br>";

$menus = $root->getElementsByTagName_r( "menu" );
//只能读取前三条,后三条读不到
foreach( $menus as $menu )

  $url1 = $menu-> getAttribute('url');
  $frame1 = $menu-> getAttribute('frame');
  $imageUrl1 = $menu-> getAttribute('imageUrl');
 
  echo "$imageUrl1 - $url1 - $frame1 <br>";
  echo "..........<br>";
}

 

//只能读取后三条,前三条读不到
foreach( $menus as $menu )
{
  $urls = $menu->getElementsByTagName_r( "url" );
  $url = $urls->item(0)->nodeValue;

  $frames = $menu->getElementsByTagName_r( "frame" );
  $frame = $frames->item(0)->nodeValue;

  $imageUrls = $menu->getElementsByTagName_r( "imageUrl" );
  $imageUrl = $imageUrls->item(0)->nodeValue;

 

  echo "$imageUrl1 - $url1 - $frame1 <br>";
  echo "..........<br>";
}
?>

 

XML文件的创建、添加、修改:

<?php
$xmlpatch = 'index.xml';
$_id = '1';
$_title = 'title1';
$_content = 'content1';
$_author = 'author1';
$_sendtime = 'time1';
$_htmlpatch = '11111.html';
$doc = new DOMDocument('1.0', 'utf-8');
$doc -> formatOutput = true;

$root = $doc -> createElement_x_x_x('root');//新建节点
$index = $doc -> createElement_x_x_x('index');//新建节点

$url = $doc -> createAttribute('url');//新建属性
$patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
$url -> a($patch);//将$patch文本设为$url属性的值

$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> a($newsid);

$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> a($newstitle);

//$content = $doc -> createTextNode($_content);//节点值

$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> a($newsauthor);

$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> a($newssendtime);

$index -> a($id);//将$id设为index节点的属性,以下类同
$index -> a($title);
//$index -> a($content);
$index -> a($url);
$index -> a($author);
$index -> a($sendtime);

$root -> a($index);//设置index为root字节点

$doc -> a($root);//设置root为跟节点

$doc -> save($xmlpatch);//保存文件
echo $xmlpatch . ' has create success';
?>

创建出的XML文件如下:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <index id="1" title="title1" url="11111.html" author="author1" time="time1"/>
</root>

 

添加:

<?php
//add.php 增加功能(跟index.php文件差不多,主要就是加个load载入跟 $root = $doc -> documentElement获得跟节点
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'title2';
$_content = 'content2';
$_author = 'author2';
$_sendtime = 'time2';
$_htmlpatch = '2.html';
$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
 $root = $doc -> documentElement;//获得根节点(root)
 $index = $doc -> createElement_x_x_x('index');
 $url = $doc -> createAttribute('url');
 $patch = $doc -> createTextNode($_htmlpatch);
 $url -> a($patch);
 $id = $doc -> createAttribute('id');
 $newsid = $doc -> createTextNode($_id);
 $id -> a($newsid);
 $title = $doc -> createAttribute('title');
 $newstitle = $doc -> createTextNode($_title);
 $title -> a($newstitle);
 $content = $doc -> createTextNode($_content);
 $author = $doc -> createAttribute('author');
 $newsauthor = $doc -> createTextNode($_author);
 $author -> a($newsauthor);
 $sendtime = $doc -> createAttribute('time');
 $newssendtime = $doc -> createTextNode($_sendtime);
 $sendtime -> a($newssendtime);
 $index -> a($id);
 $index -> a($title);
 $index -> a($content);
 $index -> a($url);
 $index -> a($author);
 $index -> a($sendtime);
 $root -> a($index);
 $doc -> save($xmlpatch);
 echo $_id . ' has been added in ' . $xmlpatch;
} else {
 echo 'xml file loaded error!';
}
?>

 

修改:

<?php
//edit.php 修改功能(这里只修改title属性值 跟节点值)
$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_r('index');
 $checkexist = 0;
 foreach ($elm as $new) {
    if($new -> getAttribute('id') == $_id) {
  $new -> setAttribute('title', $_title);
  $new -> nodeValue = $_content;//修改节点值,真是太意外了,没想到跟JS一样直接能赋值...
  //$new -> removeChild($new -> nodevalue);
  $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
//del.php 删除功能
$xmlpatch = 'index.xml';
$_id = '2';
$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
 $root = $doc -> documentElement;
 $elm = $root -> getElementsByTagName_r('index');
 foreach ($elm as $new) {
    if($new -> getAttribute('id') == $_id) {
  if($root -> removeChild($new)) {
   echo $_id . ' has been deleted';
  } else {
   echo $_id . ' delete failed';
  }
    }
 }
 $doc -> save($xmlpatch);
} else {
 echo 'xml file loaded error!';
}
?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值