我想使用php修改xml文件中的现有数据 [英] I want to modify the existing data in xml file using php

查看:21
本文介绍了我想使用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 脚本将第二个标题 Podcasting Hacks 编辑为 Podcasting Pops</code>.</code>><p>任何人都可以给我建议这样做.谢谢</p><div class="h2_lin"> 解决方案 </div><p>file 'test.php':</p><pre><code><?php包括books.php";$b=新书();//1.将书籍从 xml 加载到数组$arr=$b->load('books.xml');//2.修改标题for($i=0,$ms=count($arr);$i<$ms;$i++){if($arr[$i]['f​​ields']['title']=='Podcasting Hacks'){$arr[$i]['f​​ields']['title']='Podcasting Pops';}}//3.将数组保存到 xml$b->save('out.xml',$arr);?></code></pre><p>文件books.php":</p><pre><code><?php课本{//从xml加载书籍到数组公共函数加载($fname){$doc=new DOMDocument();if($doc->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());}}?></code></pre><p><p>Here is my xml file</p><pre><code><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> </code></pre><p>I need edit the second title <code><title>Podcasting Hacks</title></code> to <code><title>Podcasting Pops</title></code> using php script.</p> <p>Anyone give me a suggestion to do this. Thanks</p><div class="h2_lin"> 解决方案 </div><p>file 'test.php':</p><pre><code><?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); ?> </code></pre><p>file 'books.php':</p><pre><code><?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()); } } ?> </code></pre><p> <p>这篇关于我想使用php修改xml文件中的现有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!</p> </div> <div class="arc-body-main-more"> <span onclick="unlockarc('2501993');">查看全文</span> </div> </div> <div> </div> <div class="wwads-cn wwads-horizontal" data-id="166" style="max-width:100%;border: 4px solid #666;"></div> </div> </article> <div id="arc-ad-2" class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="widget bgwhite radius-1 mb-1 shadow widget-rel"> <h5>相关文章</h5> <ul> <li> <a target="_blank" title="如何使用php将xml数据附加到xml文件而不覆盖现有数据?" href="/2501987.html"> 如何使用php将xml数据附加到xml文件而不覆盖现有数据?; </a> </li> <li> <a target="_blank" title="我无法使用 php 从 xml 文件中获取所有数据" href="/2503595.html"> 我无法使用 php 从 xml 文件中获取所有数据; </a> </li> <li> <a target="_blank" title="如何使用 DOM 追加现有 XML 文件而不覆盖现有数据?在java中" href="/2854076.html"> 如何使用 DOM 追加现有 XML 文件而不覆盖现有数据?在java中; </a> </li> <li> <a target="_blank" title="如何追加使用DOM而不覆盖现有数据的现有XML文件?在java中" href="/225486.html"> 如何追加使用DOM而不覆盖现有数据的现有XML文件?在java中; </a> </li> <li> <a target="_blank" title="通过php表单修改我的xml文件" href="/2503559.html"> 通过php表单修改我的xml文件; </a> </li> <li> <a target="_blank" title="如何使用PHP修改xml文件" href="/1741606.html"> 如何使用PHP修改xml文件; </a> </li> <li> <a target="_blank" title="在Groovy中修改现有的xml文件" href="/837513.html"> 在Groovy中修改现有的xml文件; </a> </li> <li> <a target="_blank" title="如何使用PHP修改现有的excel文件?" href="/694764.html"> 如何使用PHP修改现有的excel文件?; </a> </li> <li> <a target="_blank" title="使用PHP将表单数据保存到现有XML文件中" href="/1824273.html"> 使用PHP将表单数据保存到现有XML文件中; </a> </li> <li> <a target="_blank" title="如何使用ELKI中的现有数据" href="/1797104.html"> 如何使用ELKI中的现有数据; </a> </li> <li> <a target="_blank" title="如何在不覆盖现有数据的情况下使用 DOM 附加现有 XML 文件?在 Java 中" href="/2565279.html"> 如何在不覆盖现有数据的情况下使用 DOM 附加现有 XML 文件?在 Java 中; </a> </li> <li> <a target="_blank" title="我想每天清除sqlserver表中的所有数据" href="/1386180.html"> 我想每天清除sqlserver表中的所有数据; </a> </li> <li> <a target="_blank" title="修改现有的xml" href="/1281971.html"> 修改现有的xml; </a> </li> <li> <a target="_blank" title="如何使用java从PostgreSQL数据库中的现有数据库创建新的XML文件" href="/2635061.html"> 如何使用java从PostgreSQL数据库中的现有数据库创建新的XML文件; </a> </li> <li> <a target="_blank" title="如何使用Java从PostgreSQL数据库中的现有数据库中创建新的XML文件" href="/1731359.html"> 如何使用Java从PostgreSQL数据库中的现有数据库中创建新的XML文件; </a> </li> <li> <a target="_blank" title="如何使用php写入现有的xml文件" href="/1744312.html"> 如何使用php写入现有的xml文件; </a> </li> <li> <a target="_blank" title="检查现有数据" href="/1065460.html"> 检查现有数据; </a> </li> <li> <a target="_blank" title="编辑plist中的现有数据" href="/1753379.html"> 编辑plist中的现有数据; </a> </li> <li> <a target="_blank" title="在我的现有数据库504表中" href="/1395159.html"> 在我的现有数据库504表中; </a> </li> <li> <a target="_blank" title="如何以窗口形式修改现有XML文件中的XML元素值" href="/1248134.html"> 如何以窗口形式修改现有XML文件中的XML元素值; </a> </li> <li> <a target="_blank" title="如何使用的libxml2来修改现有的xml文件?" href="/358679.html"> 如何使用的libxml2来修改现有的xml文件?; </a> </li> <li> <a target="_blank" title="使用 iOS 应用程序中的现有数据" href="/2500025.html"> 使用 iOS 应用程序中的现有数据; </a> </li> <li> <a target="_blank" title="Reg:数据库中的现有数据" href="/1267112.html"> Reg:数据库中的现有数据; </a> </li> <li> <a target="_blank" title="如何检查Firebase中的现有数据?" href="/758345.html"> 如何检查Firebase中的现有数据?; </a> </li> <li> <a target="_blank" title="使用数据集追加到现有的xml文件中" href="/2034888.html"> 使用数据集追加到现有的xml文件中; </a> </li> </ul> </div> <div class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="side"> <div class="widget widget-side bgwhite mb-1 shadow"> <h5>PHP最新文章</h5> <ul> <li> <a target="_blank" title="请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers" href="/558143.html"> 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers; </a> </li> <li> <a target="_blank" title="路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误" href="/548154.html"> 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误; </a> </li> <li> <a target="_blank" title="什么是NCFB和NOFB模式?" href="/681571.html"> 什么是NCFB和NOFB模式?; </a> </li> <li> <a target="_blank" title="警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是)" href="/594668.html"> 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是); </a> </li> <li> <a target="_blank" title="如何处理致命错误:cURL错误7:无法连接到xxxx端口443" href="/589688.html"> 如何处理致命错误:cURL错误7:无法连接到xxxx端口443; </a> </li> <li> <a target="_blank" title="参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串" href="/748988.html"> 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串; </a> </li> <li> <a target="_blank" title="phpMyAdmin的#2054无法登录到MySQL服务器" href="/218086.html"> phpMyAdmin的#2054无法登录到MySQL服务器; </a> </li> <li> <a target="_blank" title="SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败" href="/215284.html"> SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败; </a> </li> <li> <a target="_blank" title="在PHPExcel中设置字体颜色,字体和字体大小" href="/511258.html"> 在PHPExcel中设置字体颜色,字体和字体大小; </a> </li> <li> <a target="_blank" title="如何解决cURL错误(7):无法连接到主机?" href="/588378.html"> 如何解决cURL错误(7):无法连接到主机?; </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门教程 </h5> <ul> <li> <a target="_blank" title="Java教程" href="/OnLineTutorial/java/index.html"> Java教程 </a> </li> <li> <a target="_blank" title="Apache ANT 教程" href="/OnLineTutorial/ant/index.html"> Apache ANT 教程 </a> </li> <li> <a target="_blank" title="Kali Linux教程" href="/OnLineTutorial/kali_linux/index.html"> Kali Linux教程 </a> </li> <li> <a target="_blank" title="JavaScript教程" href="/OnLineTutorial/javascript/index.html"> JavaScript教程 </a> </li> <li> <a target="_blank" title="JavaFx教程" href="/OnLineTutorial/javafx/index.html"> JavaFx教程 </a> </li> <li> <a target="_blank" title="MFC 教程" href="/OnLineTutorial/mfc/index.html"> MFC 教程 </a> </li> <li> <a target="_blank" title="Apache HTTP客户端教程" href="/OnLineTutorial/apache_httpclient/index.html"> Apache HTTP客户端教程 </a> </li> <li> <a target="_blank" title="Microsoft Visio 教程" href="/OnLineTutorial/microsoft_visio/index.html"> Microsoft Visio 教程 </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门工具 </h5> <ul> <li> <a target="_blank" title="Java 在线工具" href="/Onlinetools/details/4"> Java 在线工具 </a> </li> <li> <a target="_blank" title="C(GCC) 在线工具" href="/Onlinetools/details/6"> C(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="PHP 在线工具" href="/Onlinetools/details/8"> PHP 在线工具 </a> </li> <li> <a target="_blank" title="C# 在线工具" href="/Onlinetools/details/1"> C# 在线工具 </a> </li> <li> <a target="_blank" title="Python 在线工具" href="/Onlinetools/details/5"> Python 在线工具 </a> </li> <li> <a target="_blank" title="MySQL 在线工具" href="/Onlinetools/Dbdetails/33"> MySQL 在线工具 </a> </li> <li> <a target="_blank" title="VB.NET 在线工具" href="/Onlinetools/details/2"> VB.NET 在线工具 </a> </li> <li> <a target="_blank" title="Lua 在线工具" href="/Onlinetools/details/14"> Lua 在线工具 </a> </li> <li> <a target="_blank" title="Oracle 在线工具" href="/Onlinetools/Dbdetails/35"> Oracle 在线工具 </a> </li> <li> <a target="_blank" title="C++(GCC) 在线工具" href="/Onlinetools/details/7"> C++(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="Go 在线工具" href="/Onlinetools/details/20"> Go 在线工具 </a> </li> <li> <a target="_blank" title="Fortran 在线工具" href="/Onlinetools/details/45"> Fortran 在线工具 </a> </li> </ul> </div> </div> </div> <script type="text/javascript">var eskeys = '我,想,使用,php,修改,xml,文件,中,的,现有,数据'; var cat = 'cc';';//php</script> </div> <div id="pop" onclick="pophide();"> <div id="pop_body" onclick="event.stopPropagation();"> <h6 class="flex flex101"> 登录 <span onclick="pophide();">关闭</span> </h6> <div class="pd-1"> <div class="wxtip center"> <span>扫码关注<em>1秒</em>登录</span> </div> <div class="center"> <img id="qr" src="https://huajiakeji.com/Content/Images/qrydx.jpg" alt="" style="width:150px;height:150px;" /> </div> <div style="margin-top:10px;display:flex;justify-content: center;"> <input type="text" placeholder="输入验证码" id="txtcode" autocomplete="off" /> <input id="btngo" type="button" onclick="chk()" value="GO" /> </div> <div class="center" style="margin: 4px; font-size: .8rem; color: #f60;"> 发送“验证码”获取 <em style="padding: 0 .5rem;">|</em> <span style="color: #01a05c;">15天全站免登陆</span> </div> <div id="chkinfo" class="tip"></div> </div> </div> </div> <script type="text/javascript" src="https://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/highlight.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/base.js?v=0.22"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/tui.js?v=0.11"></script> <footer class="footer"> <div class="container"> <div class="flink mb-1"> 友情链接: <a href="https://www.it1352.com/" target="_blank">IT屋</a> <a href="https://huajiakeji.com/" target="_blank">Chrome插件</a> <a href="https://www.cnplugins.com/" target="_blank">谷歌浏览器插件</a> </div> <section class="copyright-section"> <a href="https://www.it1352.com" title="IT屋-程序员软件开发技术分享社区">IT屋</a> ©2016-2022 <a href="http://www.beian.miit.gov.cn/" target="_blank">琼ICP备2021000895号-1</a> <a href="/sitemap.html" target="_blank" title="站点地图">站点地图</a> <a href="/Home/Tags" target="_blank" title="站点标签">站点标签</a> <a target="_blank" alt="sitemap" href="/sitemap.xml">SiteMap</a> <a href="/1155981.html" title="IT屋-免责申明"><免责申明></a> 本站内容来源互联网,如果侵犯您的权益请联系我们删除. </section> <!--统计代码--> <script type="text/javascript"> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?0c3a090f7b3c4ad458ac1296cb5cc779"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script type="text/javascript"> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </div> </footer> </body> </html>