php操作xml

发布时间:2021-07-16 17:15:38 阅读:1335次

php更新xml结点

<?php
$xml = new DOMDocument();
$xml->load('books.xml');
foreach($xml->getElementsByTagName('title') as $list)
{
$value = $list->nodeValue;
if ($value == 'asp')
{
$list->nodeValue = "asp程序设计";
}
echo $value."<br />";
}
$xml->save("books.xml");
?>
 

<?php
//$xml = new SimpleXMLElement("data.xml");
$doc   =   new   DOMDocument();
$doc-> load('data.xml');  
$xpath   =   new   DOMXPath($doc);

$nodes   =   $xpath-> evaluate('/graph/set');
$node   =   $nodes-> item(0);
$new   =   $doc-> createElement('set');
if($nodes-> item(0)-> getAttribute( 'name')=='Jan'){    
    $nodes-> item(0)-> setAttribute('value', 'yan');
    $nodes-> item(0)-> setAttribute('color', 'victor');
}
$doc-> save( 'data.xml ');
?>

data.xml

<?xml version="1.0" encoding="utf-8"?>
<graph caption="Monthly Unit Sales" xAxisName="Month" yAxisName="Units" decimalPrecision="0" formatNumberScale="0">
 <set name="Jan" value="test" color="victor"/>
 <set name="Feb" value="857" color="F6BD0F"/>
 <set name="Mar" value="671" color="8BBA00"/>
 <set name="Apr" value="494" color="FF8E46"/>
 <set name="May" value="761" color="008E8E"/>
 <set name="Jun" value="960" color="D64646"/>
 <set name="Jul" value="629" color="8E468E"/>
 <set name="Aug" value="622" color="588526"/>
 <set name="Sep" value="376" color="B3AA00"/>
 <set name="Oct" value="494" color="008ED6"/>
 <set name="Nov" value="761" color="9D080D"/>
 <set name="Dec" value="960" color="A186BE"/>
</graph>
 

php查找xml结点

<?
$doc = new DOMDocument();
$doc->Load("books.xml");
$id=$_GET["id"];
if($id==""){
 echo "id为空";
 exit;
}
$xpath = new DOMXPath($doc);
$query = "//book[@id = '".$id."']";
$entries = $xpath->query($query);

foreach ($entries as $entry) {
  echo "节点 ". $entry->nodeName;
  echo "<br/>------------------------";
  for($i=0; $i<$entry->childNodes->length; $i+=1) {
    $testNode = $entry->childNodes->item($i);
    echo "<br />节点 ".$testNode->nodeName;
    echo "<br />内容 ".$testNode->nodeValue;
 echo "<br/>--------------------------------";
  }
}
?>

books.xml

<?xml version="1.0" encoding="utf-8"?><books><book id="1"><author>test</author><title>asp</title><publisher>中国</publisher></book><book id="2"><author>siyuroom</author><title>php</title><publisher>china</publisher></book><book id="3"><author>test</author><title>php程序设计</title><publisher>清华大学</publisher></book></books>

php添加xml结点  

<?
$xmlDoc = new DOMDocument ();
$xmlDoc->load ('books.xml');
$utdm = $xmlDoc->getElementsByTagName ('books'); //获取
$info = $utdm->item(0);
$aaa = $xmlDoc->createElement("author","php程序设计");//可以换节点的名字和值
$bbb = $xmlDoc->createElement("title","php");
$ccc = $xmlDoc->createElement("publisher","北京大学");
$data = $xmlDoc->createElement("book");
$data->appendChild($aaa);
$data->appendChild($bbb);
$data->appendChild($ccc);
$info->appendChild($data);
$xmlDoc->save("books.xml");//保存的xml文件的名字可以换 
?>
books.xml

<?xml version="1.0" encoding="utf-8"?>
<books>
 <book>
  <author>123123 Herrington</author>
  <title>PHP Hacks</title>
  <publisher>O'Reilly</publisher>
 </book>
 <book>
  <author>df123123 asdfasdf</author>
  <title>Podcasting Hacks</title>
  <publisher>O'Reilly</publisher>
 </book>
 <book>
  <author>123 asdfasdf123123</author>
  <title>Podcasting Hacks</title>
  <publisher>O'Reilly</publisher>
 </book>
 <book>
  <author>sadf sadf</author>
  <title>Podcasting Hacks</title>
  <publisher>O'Reilly</publisher>
 </book>
 </books>

php读取xml数据  

<?php
 header("content-type:text/html;charset=utf-8");
 $doc = new DOMDocument();
 $doc->Load("books.xml");
 $books = $doc->getElementsByTagName("book");
 foreach($books as $book)
 {
  $author = $book->getElementsByTagName("author");
  $author = $author->item(0)->nodeValue;
  echo $author."<br/>";
 }
?>

books.xml

<?xml version="1.0" encoding="utf-8"?> 
<books>
 <book>
  <author>123123 Herrington</author>
  <title>PHP Hacks</title>
  <publisher>O'Reilly</publisher>
 </book>
 <book>
  <author>df123123 asdfasdf</author>
  <title>Podcasting Hacks</title>
  <publisher>O'Reilly</publisher>
 </book>
 <book>
  <author>123 asdfasdf123123</author>
  <title>Podcasting Hacks</title>
  <publisher>O'Reilly</publisher>
 </book>
 <book>
  <author>sadf sadf</author>
  <title>Podcasting Hacks</title>
  <publisher>O'Reilly</publisher>
 </book>
</books>

php读数据库生成xml文件  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta   HTTP-EQUIV="Pragma"   CONTENT="no-cache" />  
<meta   HTTP-EQUIV="Cache-Control"   CONTENT="no-cache" />  
<meta   HTTP-EQUIV="Expires"   CONTENT="0" />  
<title>生成xml文件</title>
</head>
<ul class="scare-buying-items">
<?
$conn=mysql_connect("localhost","root","");
$db=mysql_select_db("xiaolajia");
$sql="select * from books";
mysql_query("set names utf8");
$query=mysql_query($sql);
while($myrow=mysql_fetch_array($query)){

$id=$myrow["id"];

 $author=$myrow["author"];
 $title=$myrow["title"];
 $publisher=$myrow["publisher"];
 $xml.="<book id=\"$id\"><author>".$author."</author><title>".$title."</title><publisher>".$publisher."</publisher></book>";
?>
<? }?>
<?
$xml_title='<?xml version="1.0" encoding="utf-8"?>';
$xml_data=$xml_title."<books>".$xml."</books>";
$fh=fopen('test.xml',"w");
fwrite($fh,$xml_data);
?>

php操作xml  

属性:    
Attributes     存储节点的属性列表(只读)   
childNodes     存储节点的子节点列表(只读)   
dataType     返回此节点的数据类型   
Definition     以DTD或XML模式给出的节点的定义(只读)   
Doctype     指定文档类型节点(只读)   
documentElement     返回文档的根元素(可读写)   
firstChild     返回当前节点的第一个子节点(只读)   
Implementation     返回XMLDOMImplementation对象   
lastChild     返回当前节点最后一个子节点(只读)   
nextSibling     返回当前节点的下一个兄弟节点(只读)   
nodeName     返回节点的名字(只读)   
nodeType     返回节点的类型(只读)   
nodeTypedValue     存储节点值(可读写)   
nodeValue     返回节点的文本(可读写)   
ownerDocument     返回包含此节点的根文档(只读)   
parentNode     返回父节点(只读)   
Parsed     返回此节点及其子节点是否已经被解析(只读)   
Prefix     返回名称空间前缀(只读)   
preserveWhiteSpace     指定是否保留空白(可读写)   
previousSibling     返回此节点的前一个兄弟节点(只读)   
Text     返回此节点及其后代的文本内容(可读写)   
url     返回最近载入的XML文档的URL(只读)   
Xml     返回节点及其后代的XML表示(只读)

方法:    
appendChild     为当前节点添加一个新的子节点,放在最后的子节点后   
cloneNode     返回当前节点的拷贝   
createAttribute     创建新的属性   
createCDATASection     创建包括给定数据的CDATA段   
createComment     创建一个注释节点   
createDocumentFragment     创建DocumentFragment对象   
createElement     创建一个元素节点   
createEntityReference     创建EntityReference对象   
createNode     创建给定类型,名字和命名空间的节点   
createPorcessingInstruction     创建操作指令节点   
createTextNode     创建包括给定数据的文本节点   
getElementsByTagName     返回指定名字的元素集合   
hasChildNodes     返回当前节点是否有子节点   
insertBefore     在指定节点前插入子节点   
Load     导入指定位置的XML文档   
loadXML     导入指定字符串的XML文档   
removeChild     从子结点列表中删除指定的子节点   
replaceChild     从子节点列表中替换指定的子节点   
Save     把XML文件存到指定节点   
selectNodes     对节点进行指定的匹配,并返回匹配节点列表   
selectSingleNode     对节点进行指定的匹配,并返回第一个匹配节点   
transformNode     使用指定的样式表对节点及其后代进行转换  

实例获取标签属性.值:

Me.xml
<?xml version="1.0" encoding="utf-8"?>    
<phplamp>    
    <post>    
        <title id="1">PHP XML处理介绍一</title>    
        <details>详细内容一</details>    
    </post>    
    <post>    
        <title id="2">PHP XML处理介绍二</title>    
        <details>详细内容二</details>    
    </post>    
    <post>    
        <title id="3">PHP XML处理介绍三</title>    
        <details>详细内容三</details>    
    </post>    
</phplamp>      
// 首先要建一个DOMDocument对象     
$xml = new DOMDocument();        
// 加载Xml文件     
$xml->load("me.xml");         
// 获取所有的post标签     
$postDom = $xml->getElementsByTagName("post");         
// 循环遍历post标签     
foreach($postDom as $post){     
    // 获取Title标签Node     
    $title = $post->getElementsByTagName("title");         
    /**    
      * 要获取Title标签的Id属性要分两部走    
      * 1. 获取title中所有属性的列表也就是$title->item(0)->attributes    
      * 2. 获取title中id的属性,因为其在第一位所以用item(0)    
      *    
      * 小提示:    
      * 若取属性的值可以用item(*)->nodeValue    
      * 若取属性的标签可以用item(*)->nodeName    
      * 若取属性的类型可以用item(*)->nodeType    
      */    
    echo "Id: " . $title->item(0)->attributes->item(0)->nodeValue . "<br />";     
    echo "Title: " . $title->item(0)->nodeValue . "<br />";     
    echo "Details: " . $post->getElementsByTagName("details")->item(0)->nodeValue . "<br /><br />";     
}

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:php操作xml 出自老鄢博客 | 欢迎分享