我想使用php修改xml文件中的现有数据 [英] I want to modify the existing data in xml file using php
本文介绍了我想使用php修改xml文件中的现有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的xml文件
<书><author>Jack Herrington</author><title>PHP Hacks</title><publisher>O'Reilly</publisher>书><书><author>Jack Herrington</author><title>播客黑客</title><publisher>O'Reilly</publisher>书></书籍>
我需要使用 php 脚本将第二个标题
编辑为
.>
任何人都可以给我建议这样做.谢谢
解决方案
file 'test.php':
load('books.xml');//2.修改标题for($i=0,$ms=count($arr);$i<$ms;$i++){if($arr[$i]['fields']['title']=='Podcasting Hacks'){$arr[$i]['fields']['title']='Podcasting Pops';}}//3.将数组保存到 xml$b->save('out.xml',$arr);?>
文件books.php":
load($fname)) $res=$this->parse($doc);else throw new Exception('error load XML');返回 $res;}私有函数解析($doc){$xpath = new DOMXpath($doc);$items = $xpath->query("book");$result = array();foreach($items 作为 $item){$result[]=array('fields'=>$this->parse_fields($item));}返回 $result;}私有函数 parse_fields($node){$res=array();foreach($node->childNodes as $child){if($child->nodeType==XML_ELEMENT_NODE){$res[$child->nodeName]=$child->nodeValue;}}返回 $res;}//将数组保存到xml公共函数保存($fname,$rows){$doc = new DOMDocument('1.0','utf-8');$doc->formatOutput = true;$books = $doc->appendChild($doc->createElement('books'));foreach($rows 作为 $row){$book=$books->appendChild($doc->createElement('book'));foreach($row['fields'] as $field_name=>$field_value){$f=$book->appendChild($doc->createElement($field_name));$f->appendChild($doc->createTextNode($field_value));}}file_put_contents($fname, $doc->saveXML());}}?>
Here is my xml file
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
I need edit the second title <title>Podcasting Hacks</title>
to <title>Podcasting Pops</title>
using php script.
Anyone give me a suggestion to do this. Thanks
解决方案
file 'test.php':
<?php
include 'books.php';
$b=new books();
//1. load books from xml to array
$arr=$b->load('books.xml');
//2. modify title
for($i=0,$ms=count($arr);$i<$ms;$i++)
{
if($arr[$i]['fields']['title']=='Podcasting Hacks')
{
$arr[$i]['fields']['title']='Podcasting Pops';
}
}
//3. save array to xml
$b->save('out.xml',$arr);
?>
file 'books.php':
<?php
class books
{
//load books from xml to array
public function load($fname)
{
$doc=new DOMDocument();
if($doc->load($fname)) $res=$this->parse($doc);
else throw new Exception('error load XML');
return $res;
}
private function parse($doc)
{
$xpath = new DOMXpath($doc);
$items = $xpath->query("book");
$result = array();
foreach($items as $item)
{
$result[]=array('fields'=>$this->parse_fields($item));
}
return $result;
}
private function parse_fields($node)
{
$res=array();
foreach($node->childNodes as $child)
{
if($child->nodeType==XML_ELEMENT_NODE)
{
$res[$child->nodeName]=$child->nodeValue;
}
}
return $res;
}
//save array to xml
public function save($fname, $rows)
{
$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
$books = $doc->appendChild($doc->createElement('books'));
foreach($rows as $row)
{
$book=$books->appendChild($doc->createElement('book'));
foreach($row['fields'] as $field_name=>$field_value)
{
$f=$book->appendChild($doc->createElement($field_name));
$f->appendChild($doc->createTextNode($field_value));
}
}
file_put_contents($fname, $doc->saveXML());
}
}
?>
这篇关于我想使用php修改xml文件中的现有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文