• PHP代码示例 - 创建、读取、增加、删除、修改 xml


    这篇文章我们将讨论如何使用php对xml进行CRUD(创建、读取、更新、删除)操作,CRUD操作通常是数据库的基本数据操作。这里,我们将创建一个简单的PHP应用程序,在XML而不是数据库上执行所有这些操作。更多教程请访问码农之家  

     

    PHP创建XML文件

    php创建xml文件最常用的两种方法是使用SimpleXML和DomDocument。这里我们使用DomDocument来创建XML文件。

    使用DomDocument生成xml文件,需要用到以下几个方法

      1、创建节点:createElement

      2、创建节点文本:createTextNode

      3、创建节点属性:createAttribute

      4、添加节点、或节点文本、节点属性:appendChild

    代码示例:

    1. $xmlpatch = 'index.xml';
    2. $_id = '1';
    3. $_title = 'title1';
    4. $_content = 'content1';
    5. $_author = 'author1';
    6. $_sendtime = 'time1';
    7. $_htmlpatch = '1.html';
    8. $doc = new DOMDocument('1.0', 'utf-8');
    9. $doc -> formatOutput = true;
    10. $root = $doc -> createElement('root');
    11. $index = $doc -> createElement('index');
    12. $url = $doc -> createAttribute('url');
    13. $patch = $doc -> createTextNode($_htmlpatch);
    14. $url -> appendChild($patch);
    15. $id = $doc -> createAttribute('id');
    16. $newsid = $doc -> createTextNode($_id);
    17. $id -> appendChild($newsid);
    18. $title = $doc -> createAttribute('title');
    19. $newstitle = $doc -> createTextNode($_title);
    20. $title -> appendChild($newstitle);
    21. $content = $doc -> createTextNode($_content);
    22. $author = $doc -> createAttribute('author');
    23. $newsauthor = $doc -> createTextNode($_author);
    24. $author -> appendChild($newsauthor);
    25. $sendtime = $doc -> createAttribute('time');
    26. $newssendtime = $doc -> createTextNode($_sendtime);
    27. $sendtime -> appendChild($newssendtime);
    28. $index -> appendChild($id);
    29. $index -> appendChild($title);
    30. $index -> appendChild($content);
    31. $index -> appendChild($url);
    32. $index -> appendChild($author);
    33. $index -> appendChild($sendtime);
    34. $root -> appendChild($index);
    35. $doc -> appendChild($root);
    36. $doc -> save($xmlpatch);
    37. echo $xmlpatch . ' has create success';

    PHP 读取xml文件

    使用php读取xml文件有两种方法:

    1. 使用DomDocument
    2. 使用simplexml

    使用DomDocument读取xml文件代码如下:

    1. <?php
    2. $str = <<<XML
    3. <currencies>
    4. <currency name="US dollar" code_alpha="USD" code_numeric="840" />
    5. <currency name="Euro" code_alpha="EUR" code_numeric="978" />
    6. </currencies>
    7. XML;
    8. $dom = new DOMDocument();
    9. $dom->loadXML($str);
    10. foreach($dom->getElementsByTagName('currency') as $currency)
    11. {
    12. echo $currency->getAttribute('name'), "\n";
    13. echo $currency->getAttribute('code_alpha'), "\n";
    14. echo $currency->getAttribute('code_numeric'), "\n";
    15. echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n";
    16. }
    17. ?>

    使用simplexml读取xml文件代码如下:

    1. <?php
    2. $str = <<<XML
    3. <currencies>
    4. <currency name="US dollar" code_alpha="USD" code_numeric="840" />
    5. <currency name="Euro" code_alpha="EUR" code_numeric="978" />
    6. </currencies>
    7. XML;
    8. $currencies = new SimpleXMLElement($str);
    9. foreach($currencies as $currency)
    10. {
    11. echo $currency['name'], "\n";
    12. echo $currency['code_alpha'], "\n";
    13. echo $currency['code_numeric'], "\n";
    14. echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n";
    15. }
    16. ?>

    PHP 更新xml

    更新xml包括添加节点和修改节点。

    为xml添加节点示例代码如下:

    1. $xmlpatch = 'index.xml';
    2. $_id = '3';
    3. $_title = 'title3';
    4. $_content = 'content3';
    5. $_author = 'author3';
    6. $_sendtime = 'time3';
    7. $_htmlpatch = '3.html';
    8. $doc = new DOMDocument();
    9. $doc -> formatOutput = true;
    10. if($doc -> load($xmlpatch)) {
    11. $root = $doc -> documentElement;
    12. $index = $doc -> createElement('index');
    13. $url = $doc -> createAttribute('url');
    14. $patch = $doc -> createTextNode($_htmlpatch);
    15. $url -> appendChild($patch);
    16. $id = $doc -> createAttribute('id');
    17. $newsid = $doc -> createTextNode($_id);
    18. $id -> appendChild($newsid);
    19. $title = $doc -> createAttribute('title');
    20. $newstitle = $doc -> createTextNode($_title);
    21. $title -> appendChild($newstitle);
    22. $content = $doc -> createTextNode($_content);
    23. $author = $doc -> createAttribute('author');
    24. $newsauthor = $doc -> createTextNode($_author);
    25. $author -> appendChild($newsauthor);
    26. $sendtime = $doc -> createAttribute('time');
    27. $newssendtime = $doc -> createTextNode($_sendtime);
    28. $sendtime -> appendChild($newssendtime);
    29. $index -> appendChild($id);
    30. $index -> appendChild($title);
    31. $index -> appendChild($content);
    32. $index -> appendChild($url);
    33. $index -> appendChild($author);
    34. $index -> appendChild($sendtime);
    35. $root -> appendChild($index);
    36. $doc -> save($xmlpatch);
    37. echo $_id . ' has been added in ' . $xmlpatch;
    38. } else {
    39. echo 'xml file loaded error!';
    40. }
    41. ?>

    为xml修改节点示例代码如下:

    1. $xmlpatch = 'index.xml';
    2. $_id = '2';
    3. $_title = 'has been changed';
    4. $_content = 'has been changed';
    5. $doc = new DOMDocument();
    6. $doc -> formatOutput = true;
    7. if($doc -> load($xmlpatch)) {
    8. $root = $doc -> documentElement;
    9. $elm = $root -> getElementsByTagName('index');
    10. $checkexist = 0;
    11. foreach ($elm as $new) {
    12. if($new -> getAttribute('id') == $_id) {
    13. $new -> setAttribute('title', $_title);
    14. $new -> nodeValue = $_content;
    15. $checkexist = 1;
    16. }
    17. }
    18. if($checkexist == 0) {
    19. echo $_id . ' is not found in ' . $xmlpatch;
    20. } else {
    21. $doc -> save($xmlpatch);
    22. echo $_id . ' has been changed';
    23. }
    24. } else {
    25. echo 'xml file loaded error!';
    26. }
    27. ?>

    PHP删除xml节点

    php删除xml节点的方法:首先遍历所有bird节点以及每一个bird节点的所有属性;然后修改节点值;最后通过“removeChild”方法删除节点即可。

    1. $data='
    2. ';
    3. $doc=new SimpleXMLElement($data);
    4. foreach($doc->seg as $seg)
    5. {
    6. if($seg['id'] == 'A12') {
    7. $dom=dom_import_simplexml($seg);
    8. $dom->parentNode->removeChild($dom);
    9. }
    10. }
    11. echo $doc->asXml();

    输出结果:

    1. <?xml version="1.0"?>
    2. <data><seg id="A1"/><seg id="A5"/><seg id="A29"/><seg id="A30"/></data>

    注意:使用XPath选择特定节点要简单得多。

    1. $segs=$doc->xpath('//seq[@id="A12"]');
    2. if (count($segs)>=1) {
    3. $seg=$segs[0];
    4. }

    以上是本文的全部类容,感谢阅读,希望能帮到大家。更多教程请访问码农之家   

  • 相关阅读:
    网络连接删除了怎么弄好
    Spark SQL 每年的1月1日算当年的第一个自然周, 给出日期,计算是本年的第几周
    一致性协议-ChainPaxos详解
    蓝牙AOA融合定位技术汇总
    Go 语言 设计模式-桥接模式
    【FPGA教程案例9】基于vivado核的时钟管理器设计与实现
    day01 spring
    Atom CMS SQL注入漏洞 (CVE-2022-25488)
    Moonbeam:由开发者制作,为开发者而生
    【vite】vite.defineConfig is not a function/npm无法安装第三方包问题
  • 原文地址:https://blog.csdn.net/wuxiaopengnihao1/article/details/126462499