PHP封装的XML简单操作类完整实例

时间:2017-11-15

smp.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<root>
   <posts>
     <id>1</id>
     <title>标题一</title>
     <content>详细内容一</content>
   </posts>
   <posts>
     <id>2</id>
     <title>标题二</title>
     <content>详细内容二</content>
   </posts>
   <posts>
     <id>3</id>
     <title>标题三</title>
     <content>详细内容三</content>
   </posts>
</root>

index.php文件:

include("xml_dom.php");
$xml=new xml_dom("smp.xml");//载入xml文件
$xmlarr=$xml->getData();//读取xml文件内容
var_dump($xmlarr);

运行结果:

array(1) {
 ["posts"]=>
 array(3) {
  [0]=>
  array(3) {
   ["id"]=>
   string(1) "1"
   ["title"]=>
   string(9) "标题一"
   ["content"]=>
   string(15) "详细内容一"
  }
  [1]=>
  array(3) {
   ["id"]=>
   string(1) "2"
   ["title"]=>
   string(9) "标题二"
   ["content"]=>
   string(15) "详细内容二"
  }
  [2]=>
  array(3) {
   ["id"]=>
   string(1) "3"
   ["title"]=>
   string(9) "标题三"
   ["content"]=>
   string(15) "详细内容三"
  }
 }
}

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XML
http://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

  • 共2页:
  • 上一页
  • 2/2下一篇
    上一篇:PHP实现网站访问量计数器 下一篇:PHP开发中解决并发问题的几种实现方法分析

    相关文章

    最新文章