• 技术文章 >php教程 >php手册

    用PHP读取和编写XML及DOM

    2016-06-13 10:40:10原创625
      用PHP读取和编写可扩展标记语言(XML)看起来可能有点恐怖。实际上,XML 和它的所有相关技术可能是恐怖的,但是用PHP读取和编写XML不一定是项恐怖的任务。首先,需要学习一点关于XML的知识:它是什么,用它做什么。然后,需要学习如何用PHP读取和编写XML,而有许多种方式可以做这件事。

      什么是XML?

      XML是一种数据存储格式。它没有定义保存什么数据,也没有定义数据的格式。XML只是定义了标记和这些标记的属性。格式良好的XML标记看起来像这样:

    1. <name>This test for php100name>

    这个标记包含一些文本:Jack Herrington。不包含文本的 XML 标记看起来像这样:

    1. <powerUp />

    用 XML 对某件事进行编写的方式不止一种。例如,这个标记形成的输出与前一个标记相同:

    1. <powerUp>powerUp>

    也可以向 XML 标记添加属性。例如,这个 标记包含 first 和 last 属性:

    1. <name first="Jack" last="Herrington" />

    也可以用 XML 对特殊字符进行编码。例如,& 符号可以像这样编码:

    1. &

    包含标记和属性的 XML 文件如果像示例一样格式化,就是格式良好的,这意味着标记是对称的,字符的编码正确。清单 1 是一份格式良好的 XML 的示例。

    清单 1. XML图书列表示例

    1. <books>
    2. <book>
    3. <author>Jack Herringtonauthor>
    4. <title>PHP Hackstitle>
    5. <publisher>OReillypublisher>
    6. book>
    7. <book>
    8. <author>Jack Herringtonauthor>
    9. <title>Podcasting Hackstitle>
    10. <publisher>OReillypublisher>
    11. book>
    12. books>

    清单 1 中的 XML 包含一个图书列表。父标记 包含一组 标记,每个 标记又包含 和 <publisher> 标记。当 XML 文档的标记结构和内容得到外部模式文件的验证后,XML 文档就是正确的。模式文件可以用不同的格式指定。对于本文来说,所需要的只是格式良好的 XML。</p> <p>如果觉得 XML 看起来很像超文本标记语言(HTML),那么就对了。XML 和 HTML 都是基于标记的语言,它们有许多相似之处。但是,要着重指出的是:虽然 XML 文档可能是格式良好的 HTML,但不是所有的 HTML 文档都是格式良好的 XML。换行标记(br)是 XML 和 HTML 之间区别的一个好例子。这个换行标记是格式良好的 HTML,但不是格式良好的 XML:</p> <pre class="brush:php;toolbar:false"><ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">p</span><span class="tag">></span><span>This is a paragraph</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span> </span></span></li><li><span>With a line break</span><span class="tag"></</span><span class="tag-name">p</span><span class="tag">></span><span> </span></li></ol></pre> <p>这个换行标记是格式良好的 XML 和 HTML:</p> <pre class="brush:php;toolbar:false"><ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">p</span><span class="tag">></span><span>This is a paragraph</span><span class="tag"><</span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span> </span></span></li><li><span>With a line break</span><span class="tag"></</span><span class="tag-name">p</span><span class="tag">></span><span> </span></li></ol></pre> <p>如果要把 HTML 编写成同样是格式良好的 XML,请遵循 W3C 委员会的可扩展超文本标记语言(XHTML)标准(参见 参考资料)。所有现代的浏览器都能呈现 XHTML。而且,还可以用 XML 工具读取 XHTML 并找出文档中的数据,这比解析 HTML 容易得多。<div class='bg-white layui-clear' style="margin:15px 0;"><span style="color:red;">声明:</span>本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。</div><div class="wwads-cn wwads-vertical" data-id="164" style="max-width:640px"></div><div style="margin-left:-10px;"><div class="_ukog6mn8oy"></div><script type="text/javascript"> (window.slotbydup = window.slotbydup || []).push({ id: "u6821789", container: "_ukog6mn8oy", async: true }); </script></div><div class='tags bg-white layui-clear pt-5'><span>专题推荐:</span><a href="//m.sbmmt.com/m/search?word=用" target="_blank">用</a> <a href="//m.sbmmt.com/m/search?word=php" target="_blank">PHP</a> <a href="//m.sbmmt.com/m/search?word=读取" target="_blank">读取</a> <a href="//m.sbmmt.com/m/search?word=和" target="_blank">和</a> <a href="//m.sbmmt.com/m/search?word=编写" target="_blank">编写</a> <a href="//m.sbmmt.com/m/search?word=xml" target="_blank">XML</a> <a href="//m.sbmmt.com/m/search?word=及" target="_blank">及</a> <a href="//m.sbmmt.com/m/search?word=dom" target="_blank">DOM</a> <a href="//m.sbmmt.com/m/search?word=用" target="_blank">用</a> <a href="//m.sbmmt.com/m/search?word=php" target="_blank">PHP</a> <a href="//m.sbmmt.com/m/search?word=读取" target="_blank">读取</a> <a href="//m.sbmmt.com/m/search?word=和" target="_blank">和</a> <a href="//m.sbmmt.com/m/search?word=编写" target="_blank">编写</a> <a href="//m.sbmmt.com/m/search?word=可扩展" target="_blank">可扩展</a> <a href="//m.sbmmt.com/m/search?word=标记" target="_blank">标记</a> <a href="//m.sbmmt.com/m/search?word=语言" target="_blank">语言</a> <a href="//m.sbmmt.com/m/search?word=xml" target="_blank">XML</a> <a href="//m.sbmmt.com/m/search?word=看起来" target="_blank">看起来</a> <a href="//m.sbmmt.com/m/search?word=可能" target="_blank">可能</a> <a href="//m.sbmmt.com/m/search?word=有" target="_blank">有</a></div><div><span style="display:inline-block; width: 100%; margin-top: 10px;"> 上一篇:<a style="color: #00AEEF;" href="//m.sbmmt.com/m/article/183961.html">PHP之isset()与empty()函数区别介绍</a></span><span style="display:inline-block; width: 100%"> 下一篇:<a style="color: #00AEEF;" href="//m.sbmmt.com/m/article/183966.html">php取得系统启动时间长短实例代码</a></span></div></article><!--<div class="selectButton"><button type="button">点击阅读全文</button></div>--><!--<a href="javascript:;" onclick="location.href='https://m.xp.cn/'" rel="nofollow"><img src="//m.sbmmt.com/m/static/images/articlead.jpg" width="100%" alt="phpstudy集成环境下载"></a>--><a href="//m.sbmmt.com/m/mk.html" target="_blank"><img src="//m.sbmmt.com/img/upload/aroundimg/000/000/001/62b28d06f0da4985.png" width="100%" alt="20期PHP线上班"/></a><!--<a href="//m.sbmmt.com/m/vip_mobile.html" rel="nofollow"><img src="//m.sbmmt.com/m/static/images/articlead1.gif" width="100%" alt="会员特权"></a>--></div><div class='course-list bg-white layui-clear mt-10'><h4>相关文章推荐</h4><a href="//m.sbmmt.com/jump/go.php?url=https%3A%2F%2Fm.sbmmt.com%2Fvip_mobile.html" style="color: red;" rel="nofollow" target="_blank" title='精选22门好课,价值3725元,开通VIP免费学习!' class='alist'>精选22门好课,价值3725元,开通VIP免费学习!</a><a href='//m.sbmmt.com/m/article/165723.html' title="PHP数组和explode函数示例总结,数组explode" class='alist'>• PHP数组和explode函数示例总结,数组explode</a><a href='//m.sbmmt.com/m/article/176205.html' title="php 连接 mysql数据库操作类" class='alist'>• php 连接 mysql数据库操作类</a><a href='//m.sbmmt.com/m/article/182830.html' title="基于DB2及PHP的应用系统跨平台迁移详细步骤(二)" class='alist'>• 基于DB2及PHP的应用系统跨平台迁移详细步骤(二)</a><a href='//m.sbmmt.com/m/article/204033.html' title="php编写一个简单的路由类" class='alist'>• php编写一个简单的路由类</a><a href='//m.sbmmt.com/m/article/205176.html' title="PHP过滤器的实现方法第1/2页" class='alist'>• PHP过滤器的实现方法第1/2页</a></div><div class='course-list bg-white layui-clear mt-10 mb-30'><ul ><h4>相关课程推荐</h4><li class='layui-clear' style="padding-left: 0px;"><a href="//m.sbmmt.com/m/course/386.html" class="course-arctic-img"><img src='//m.sbmmt.com/img/upload/course/000/000/001/5d1c6df423564706.jpg' height="100%"></a><div class='course-intro' style="margin-left: 0px;"><h2><a href="//m.sbmmt.com/m/course/386.html">独孤九贱(3)_JavaScript视频教程</a></h2><p class='course-intro-info'>javascript是运行在浏览器上的脚本语言,连续多年,被评为全球最受欢迎的编程语言。是前端开发必备三大法器中,最具杀伤力。如果前端开发是降龙十八掌,好么javascript就是第18掌:亢龙有悔。没有它,你的前端生涯是不完整的。《php.cn独孤九贱(3)-JavaScript视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了javascript知识。</p><p><a href="//m.sbmmt.com/m/course/list/17.html"><span class='level'>JavaScript教程</span></a><span class='count'>129387次播放</span></p></div></li><hr><li class='layui-clear' style="padding-left: 0px;"><a href="//m.sbmmt.com/m/course/582.html" class="course-arctic-img"><img src='//m.sbmmt.com/img/upload/course/000/000/001/5d1c6e0d2b744633.jpg' height="100%"></a><div class='course-intro' style="margin-left: 0px;"><h2><a href="//m.sbmmt.com/m/course/582.html">独孤九贱(6)_jQuery视频教程</a></h2><p class='course-intro-info'>jQuery是一个快速、简洁的JavaScript框架。设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。 核心特性可以总结为:具有独特的链式语法和短小清晰的多功能接口;具有高效灵活的css选择器,并且可对CSS选择器进行扩展;拥有便捷的插件扩展机制和丰富的插件。兼容各种主流浏览器,如IE 6.0+、FF 1.5+、Safari 2.0+、Opera 9.0+等,是全球最流行的前端开发框架之一。PHP中文网根据最新版本,独家录制jQuery最新视频教程,回馈PHP中文网的新老用户。</p><p><a href="//m.sbmmt.com/m/course/list/19.html"><span class='level'>jQuery教程</span></a><span class='count'>106875次播放</span></p></div></li><hr><li class='layui-clear' style="padding-left: 0px;"><a href="//m.sbmmt.com/m/course/1195.html" class="course-arctic-img"><img src='//m.sbmmt.com/img/upload/course/000/000/015/612f16c1e1c8e978.jpg' height="100%"></a><div class='course-intro' style="margin-left: 0px;"><h2><a href="//m.sbmmt.com/m/course/1195.html">jQuery与Ajax基础与实战</a></h2><p class='course-intro-info'>jQuery是最流行的JS函数库,封装了许多实用的功能,其中最引人入胜的就是Ajax。 jQuery中的Ajax操作,语法简单,操作方便,使Ajax从未如此轻松,前端人员从此不再为与服务器异步交互而发愁,本套课程,精选了最常用的几个方法,从基本的语法到每个参数,再到具体实例进行了全面的讲解。</p><p><a href="//m.sbmmt.com/m/course/list/25.html"><span class='level'>AJAX教程</span></a><span class='count'>7399次播放</span></p></div></li><hr><li class='layui-clear' style="padding-left: 0px;"><a href="//m.sbmmt.com/m/course/1313.html" class="course-arctic-img"><img src='//m.sbmmt.com/img/upload/course/000/000/015/617b8f8af2b97409.jpg' height="100%"></a><div class='course-intro' style="margin-left: 0px;"><h2><a href="//m.sbmmt.com/m/course/1313.html">Git教程(60分钟全程无废话版)</a></h2><p class='course-intro-info'>Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。 Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。 Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持</p><p><a href="//m.sbmmt.com/m/course/list/17.html"><span class='level'>JavaScript教程</span></a><span class='count'>6376次播放</span></p></div></li><hr></ul></div><!-- <div class='discuss layui-clear bg-white'><h3>全部评论<span id='discuss-article'>我要评论</span></h3></div><div class='discuss-submit bg-white' url="//m.m.sbmmt.com/"><li class='info layui-clear'><span class='f-left' id='discuss-submit-close'>取消</span><span class='f-left'>发布评论</span><span class='f-right article-reply-publish' id="183963">发送</span></li><li class='layui-clear'><textarea type='text' rows="6" placeholder="请输入要评论的内容" class="article-reply-content-text"></textarea></li><li class='layui-clear'><button class="layui-btn layui-btn-danger f-right article-reply-publish-button">发布</button></li></div>--><div class='lock-screen' id='lock-screen'></div><div class='popimg-setting'><div>1/1</div><img src="" width='100%'></div><script src="//m.sbmmt.com/m/static/ueditor/third-party/SyntaxHighlighter/shCore.js"></script><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><footer><a href='//m.sbmmt.com/m/' ><i class='layui-icon layui-icon-home '></i>首页</a><a href='//m.sbmmt.com/m/course.html' ><i class='layui-icon layui-icon-play'></i>视频</a><a href='//m.sbmmt.com/m/mk.html?t=2'><img src="//m.sbmmt.com/m/static/images/gif_live.gif" />直播</a><!-- <a href='//m.sbmmt.com/m/app/'><img src="//m.sbmmt.com/img/upload/article/000/000/003/6108e2ad07387877.png" style="width:20px;height:20px;" /><div style="margin-top:-5px;">下载</div></a>--><a href='//m.sbmmt.com/m/wenda.html' ><i class='layui-icon layui-icon-code-circle'></i>社区</a><a href='//m.sbmmt.com/m/login' ><i class='layui-icon layui-icon-username'></i>我的</a></footer><div class="headerMask"></div><div class="right_menu"><div class="right_menu_con"><h2 class="hjclass-txt"><i class="layui-icon layui-icon-shrink-right hjclass-txt-i"></i>PHP中文网</h2><div class="menu-list"><a href="//m.sbmmt.com/m/"><span class="item-icon item-1"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb65f99ae2304.png" alt="首页"></span> 首页</a><a href="//m.sbmmt.com/m/course.html"><span class="item-icon item-7"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb82bd09ad628.png" alt="课程"></span> 课程</a><a href="//m.sbmmt.com/m/article.html"><span class="item-icon item-4"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb7e111b3b425.png" alt="文章"></i></span>文章</a><a href="//m.sbmmt.com/m/wenda.html"><span class="item-icon item-2"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb83f04e24328.png" alt="问答"></span> 问答</a><a href="//m.sbmmt.com/m/blog.html"><span class="item-icon item-0"><img src="//m.sbmmt.com/m/static/images/ico/blog.png" alt="博客"></span>博客</a><a href="//m.sbmmt.com/m/dic.html"><span class="item-icon item-9"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb85047b25758.png" alt="词典"></span> 词典</a><a href="//m.sbmmt.com/m/course/type/99.html"><span class="item-icon item-3"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb8736ea90300.png" alt="手册"></span> 手册</a><a href="//m.sbmmt.com/m/xiazai/"><span class="item-icon item-5"><img src="//m.sbmmt.com/m/static/images/ico/resources.png" alt="资源"></span> 资源</a><a href="//m.sbmmt.com/m/search"><span class="item-icon item-6"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb880b460a204.png" alt="搜索"></span> 搜索</a><a href="//m.sbmmt.com/m/app/"><span class="item-icon item-6"><img src="//m.sbmmt.com/img/upload/article/000/000/003/608bbfa30d9cc398.png" style="width:35px;height:35px;" alt="APP下载"></span> APP下载</a></div></div></div><script>isLogin = 0;</script><script type="text/javascript" src="//m.sbmmt.com/m/static/js/jquery.min.js"></script><script type="text/javascript" src="//m.sbmmt.com/m/static/layui/layui.js"></script><script type="text/javascript" src="//m.sbmmt.com/m/static/js/global.js?4.9.47"></script><script>(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);})();var _hmt = _hmt || [];(function() {var hm = document.createElement("script");hm.src = "https://m.sbmmt.com/hm.js?43f47cabf6856204df6d083dd89ae407";var s = document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm, s);})();</script><link rel='stylesheet' id='_main-css' href='//m.sbmmt.com/m/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='//m.sbmmt.com/m/static/js/viewer.min.js?1'></script><script type='text/javascript' src='//m.sbmmt.com/m/static/js/jquery-viewer.min.js'></script><script> // function called if wwads is blocked function ABDetected() { document.getElementsByClassName("wwads-cn")[0].insertAdjacentHTML("beforeend", "<style>.wwads-horizontal,.wwads-vertical{background-color:#f4f8fa;padding:5px;min-height:120px;margin-top:20px;box-sizing:border-box;border-radius:3px;font-family:sans-serif;display:flex;min-width:150px;position:relative;overflow:hidden;}.wwads-horizontal{flex-wrap:wrap;justify-content:center}.wwads-vertical{flex-direction:column;align-items:center;padding-bottom:32px}.wwads-horizontal a,.wwads-vertical a{text-decoration:none}.wwads-horizontal .wwads-img,.wwads-vertical .wwads-img{margin:5px}.wwads-horizontal .wwads-content,.wwads-vertical .wwads-content{margin:5px}.wwads-horizontal .wwads-content{flex:130px}.wwads-vertical .wwads-content{margin-top:10px}.wwads-horizontal .wwads-text,.wwads-content .wwads-text{font-size:14px;line-height:1.4;color:#0e1011;-webkit-font-smoothing:antialiased}.wwads-horizontal .wwads-poweredby,.wwads-vertical .wwads-poweredby{display:block;font-size:11px;color:#a6b7bf;margin-top:1em}.wwads-vertical .wwads-poweredby{position:absolute;left:10px;bottom:10px}.wwads-horizontal .wwads-poweredby span,.wwads-vertical .wwads-poweredby span{transition:all 0.2s ease-in-out;margin-left:-1em}.wwads-horizontal .wwads-poweredby span:first-child,.wwads-vertical .wwads-poweredby span:first-child{opacity:0}.wwads-horizontal:hover .wwads-poweredby span,.wwads-vertical:hover .wwads-poweredby span{opacity:1;margin-left:0}.wwads-horizontal .wwads-hide,.wwads-vertical .wwads-hide{position:absolute;right:-23px;bottom:-23px;width:46px;height:46px;border-radius:23px;transition:all 0.3s ease-in-out;cursor:pointer;}.wwads-horizontal .wwads-hide:hover,.wwads-vertical .wwads-hide:hover{background:rgb(0 0 0 /0.05)}.wwads-horizontal .wwads-hide svg,.wwads-vertical .wwads-hide svg{position:absolute;left:10px;top:10px;fill:#a6b7bf}.wwads-horizontal .wwads-hide:hover svg,.wwads-vertical .wwads-hide:hover svg{fill:#3E4546}</style><a href='https://wwads.cn/page/whitelist-wwads' class='wwads-img' target='_blank' rel='nofollow'><img src='https://creatives-1301677708.file.myqcloud.com/images/placeholder/wwads-friendly-ads.png' width='130'></a><div class='wwads-content'><a href='https://wwads.cn/page/whitelist-wwads' class='wwads-text' target='_blank' rel='nofollow'>为了本站的长期运营,请将我们的网站加入广告拦截器的白名单,感谢您的支持!</a><a href='https://wwads.cn/page/end-user-privacy' class='wwads-poweredby' title='万维广告 ~ 让广告更优雅,且有用' target='_blank'><span>万维</span><span>广告</span></a></div><a class='wwads-hide' onclick='parentNode.remove()' title='隐藏广告'><svg xmlns='http://www.w3.org/2000/svg' width='6' height='7'><path d='M.879.672L3 2.793 5.121.672a.5.5 0 11.707.707L3.708 3.5l2.12 2.121a.5.5 0 11-.707.707l-2.12-2.12-2.122 2.12a.5.5 0 11-.707-.707l2.121-2.12L.172 1.378A.5.5 0 01.879.672z'></path></svg></a>"); }; //check document ready function docReady(t) { "complete" === document.readyState || "interactive" === document.readyState ? setTimeout(t, 1) : document.addEventListener("DOMContentLoaded", t); } //check if wwads' fire function was blocked after document is ready with 3s timeout (waiting the ad loading) docReady(function () { setTimeout(function () { if( window._AdBlockInit === undefined ){ ABDetected(); } }, 3000); }); </script><script>$('article').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}});</script><script type="application/ld+json">{"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld","@id": "//m.m.sbmmt.com/article/183963.html","appid": "1549852514326010","title": "用PHP读取和编写XML及DOM","images": [""],"description": "用PHP读取和编写XML及DOM。用PHP读取和编写可扩展标记语言(XML)看起来可能有点恐怖。实际上,XML 和它的所有相关技术可能是恐怖的,但是用PHP读取和编写XML不一定是","pubDate": "2016-06-13T10:40:10","data": {"WebPage": {"headline": "用PHP读取和编写XML及DOM","wapUrl": "//m.m.sbmmt.com/article/183963.html","pcUrl": "//m.sbmmt.com/m/php-notebook-183963.html","fromSrc": "php中文网","domain": "电子科技","category": ["问答"],"isDeleted": 0},"Question": [{"acceptedAnswer":"用PHP读取和编写XML及DOM。用PHP读取和编写可扩展标记语言(XML)看起来可能有点恐怖。实际上,XML 和它的所有相关技术可能是恐怖的,但是用PHP读取和编写XML不一定是"}],"ImageObject": [{"contentUrl": "","scale": "5:2"}],"Author": [{"name": "php中文网","jobTitle": ["php公益学习平台"],"headPortrait": "https://img.php.cn/upload/article/000/000/003/5d1b23156bf94358.png"}]}}</script><script>$('.selectButton').hide();</script><script> var href = window.location.href; var title = document.title; var num = Math.floor(Math.random() * 10000 + 1); var newscript = document.createElement('script'); newscript.setAttribute('type','text/javascript'); newscript.setAttribute('src',"https://analyze.xm6wpp.com/index.php/api/statistics/phpcn?url="+encodeURIComponent(href)+"&title="+encodeURIComponent(title)+"&"+num); var head = document.getElementsByTagName('head')[0]; head.appendChild(newscript); </script></body></html>