I want to modify the existing data in xml file using php
这是我的 xml 文件
1
2 3 4 5 6 7 8 9 10 11 12 |
<books>
<book> Jack Herrington</author> PHP Hacks <publisher>O‘Reilly</publisher> </book> <book> Jack Herrington</author> Podcasting Hacks <publisher>O’Reilly</publisher> </book> </books> |
我需要使用 php 脚本将第二个标题
任何人都可以给我一个建议。谢谢
文件//’test.php//’:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php include ‘books.php’; $b=new books(); //1. load books from xml to array $arr=$b->load(‘books.xml’); //2. modify title //3. save array to xml |
文件//’books.php//’:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php
class books { //load books from xml to array public function load($fname) { $doc=new DOMDocument(); if($doc->load($fname)) $res=$this->parse($doc); return $res; private function parse($doc) private function parse_fields($node) //save array to xml $books = $doc->appendChild($doc->createElement(‘books’)); foreach($rows as $row) file_put_contents($fname, $doc->saveXML()); } |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268547.html