跳转到主内容
极星编程网:以代码为星,赴技术山海!

PHP SimpleXML怎么用 SimpleXML解析XML数据教程

SimpleXML 是 PHP 内置轻量级 XML 解析扩展,将 XML 转为对象,支持字符串、文件、URL 加载,需注意类型转换和命名空间处理。 SimpleXML 是 PHP 内置的轻量级 XML 解析扩展,适合读取结构清晰、不带命名空间或命名空间简单的 XML 数据。它把 XML 转成对象,用属性和方法直接访问节点,比 DOM 或 XMLReader 更简洁直观。 加载 XML 数据 支持从字符串、文件或 URL 加载: 从字符串解析 :用
simplexml_load_string($xmlStr)
,适合 API 返回的 XML 响应 从文件加载 :用
simplexml_load_file('data.xml')
,自动读取并解析本地 XML 文件 从 URL 获取 :传入 HTTP 地址(需开启
allow_url_fopen
),如
simplexml_load_file('https://api.example.com/feed.xml')
失败时返回
false
,建议加判断:
$xml = simplexml_load_string($str); if ($xml === false) { throw new Exception('XML 解析失败'); }
访问元素和属性 节点名直接作为对象属性,属性名加前缀
attributes()
: 立即学习 “ PHP免费学习笔记(深入) ”;
$xml->book
→ 获取第一个 zuojiankuohao php cnbook> 元素
$xml->book[0]
→ 显式取第一个(避免 隐式转换 歧义)
$xml->book->title
→ 取子元素 的文本内容 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">$xml->book['id']</code></div> → 取 <book id="123"> 中的 id 属性值 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">(string)$xml->book->price</code></div> → 强制转字符串,避免对象残留 遍历多个同名节点 当有多个相同标签(如多本书),用 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">foreach</code></div> 遍历: PHP 8.5.5 PHP 8.5.5 是 PHP 8.5 分支的维护更新版本。该版本延续了“小步快跑”的迭代逻辑,通过深度错误修复、底层性能微调以及安全加固,旨在为开发者提供一个更健壮、更高效的运行环境。该版本严格遵守语义化版本规范,不包含破坏性变更。 下载 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">foreach ($xml->book as $book) { echo (string)$book->title . ' - ' . (string)$book->author . "\n"; // 注意:每个 $book 是 SimpleXMLElement 对象,需 (string) 转换文本 }</code></div> 也可以用 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">count($xml->book)</code></div> 获取数量,或 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">array_values(iterator_to_array($xml->book))</code></div> 转为索引数组(兼容性更强)。 处理命名空间(简单情况) 如果 XML 含命名空间(如 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto"><rss xmlns:dc="http://purl.org/dc/elements/1.1/"></code></div> ),先用 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">getNamespaces()</code></div> 查看,再用 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">children('dc', true)</code></div> 进入: <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">$namespaces = $xml->getNamespaces(true); // 返回 ['dc' => 'http://...']</code></div> <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">$dc = $xml->channel->item->children('dc', true);</code></div> <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">echo (string)$dc->creator; // 访问 dc:creator</code></div> 注意:SimpleXML 不支持跨命名空间 XPath 查询,复杂场景建议切换到 DOMDocument + DOMXPath。 基本上就这些。SimpleXML 不复杂但容易忽略类型转换和命名空间细节,用对了效率高、代码干净。 </div> <!-- 文章导航 --> <nav class="jxgpc-article-nav" aria-label="文章导航"> <a href="http://www.jxgpc.com/pc/10967.html" class="jxgpc-article-nav-link jxgpc-article-nav-prev"> <span>‹</span> <div> <div class="jxgpc-article-nav-label">上一篇</div> <div class="jxgpc-article-nav-title">这个网站的前台你们还有参考?</div> </div> </a> <a href="http://www.jxgpc.com/pc/10973.html" class="jxgpc-article-nav-link jxgpc-article-nav-next"> <div> <div class="jxgpc-article-nav-label">下一篇</div> <div class="jxgpc-article-nav-title">高精度印度除法</div> </div> <span>›</span> </a> </nav> </div> </article> <!-- 相关文章列表 --> <section class="jxgpc-content-section" style="margin-top: 20px;"> <h2 class="jxgpc-widget-title" style="margin-bottom: 20px;">相关文章</h2> <ul class="jxgpc-article-list"> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/10967.html" class="jxgpc-article-link">这个网站的前台你们还有参考?</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/10955.html" class="jxgpc-article-link">怎么创建这种唯一的KEY</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/10954.html" class="jxgpc-article-link">如何利用 RAM 加速视频处理中的图像帧处理?</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/10952.html" class="jxgpc-article-link">smarty模板中如何生成随机数</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/10948.html" class="jxgpc-article-link">Go 中为何在原子操作后需调用 runtime.Gosched 实现协程让出?</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/10973.html" class="jxgpc-article-link">高精度印度除法</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/10979.html" class="jxgpc-article-link">Python中怎样实现分布式计算?</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/11038.html" class="jxgpc-article-link">怎样用 PHP 创建自定义的错误处理机制?</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/11041.html" class="jxgpc-article-link">php中常用的预定义变量小结_PHP</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/11043.html" class="jxgpc-article-link">PHP图像处理入门:如何使用imagecreatefromjpeg函数打开JPEG图像文件</a> <div class="jxgpc-article-meta">2026-09-22</div> </li> </ul> </section> </div> <!-- 右侧边栏 --> <aside class="jxgpc-sidebar jxgpc-sidebar-right" aria-label="侧边栏"> <!-- 推荐阅读 --> <div class="jxgpc-widget"> <h3 class="jxgpc-widget-title">推荐阅读</h3> <div class="jxgpc-recommend-grid"> <a href="http://www.jxgpc.com/pc/12882.html" class="jxgpc-recommend-item"> <div class="jxgpc-recommend-thumb"> <img src="http://www.jxgpc.com/static/v3/images/article-6.jpg" alt="零基础怎么购买PEPE币 新手购买PEPE币全流程指南"> </div> <div class="jxgpc-recommend-title">零基础怎么购买PEPE币 新手购买PEPE币全流程指南</div> </a> <a href="http://www.jxgpc.com/pc/12874.html" class="jxgpc-recommend-item"> <div class="jxgpc-recommend-thumb"> <img src="http://www.jxgpc.com/static/v3/images/article-6.jpg" alt="mysql - php随机生成200万条记录中有很多重复的"> </div> <div class="jxgpc-recommend-title">mysql - php随机生成200万条记录中有很多重复的</div> </a> <a href="http://www.jxgpc.com/pc/12866.html" class="jxgpc-recommend-item"> <div class="jxgpc-recommend-thumb"> <img src="http://www.jxgpc.com/static/v3/images/article-6.jpg" alt="Docker 容器化 Golang 应用时如何减小镜像体积"> </div> <div class="jxgpc-recommend-title">Docker 容器化 Golang 应用时如何减小镜像体积</div> </a> <a href="http://www.jxgpc.com/pc/12861.html" class="jxgpc-recommend-item"> <div class="jxgpc-recommend-thumb"> <img src="http://www.jxgpc.com/static/v3/images/article-6.jpg" alt="求一个在windows系统的xampp集成包安装pdo_mysql扩展程序"> </div> <div class="jxgpc-recommend-title">求一个在windows系统的xampp集成包安装pdo_mysql扩展程序</div> </a> </div> </div> <!-- 最新文章 --> <div class="jxgpc-widget"> <h3 class="jxgpc-widget-title">最新文章</h3> <ul class="jxgpc-article-list"> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/12882.html" class="jxgpc-article-link">零基础怎么购买PEPE币 新手购买PEPE币全流程指南</a> <div class="jxgpc-article-meta">2026-09-25</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/12874.html" class="jxgpc-article-link">mysql - php随机生成200万条记录中有很多重复的</a> <div class="jxgpc-article-meta">2026-09-25</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/12866.html" class="jxgpc-article-link">Docker 容器化 Golang 应用时如何减小镜像体积</a> <div class="jxgpc-article-meta">2026-09-25</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/12861.html" class="jxgpc-article-link">求一个在windows系统的xampp集成包安装pdo_mysql扩展程序</a> <div class="jxgpc-article-meta">2026-09-25</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/12858.html" class="jxgpc-article-link">php 对象数组怎么转json字符串数组</a> <div class="jxgpc-article-meta">2026-09-25</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/12854.html" class="jxgpc-article-link">nice服务端架构重构与演进</a> <div class="jxgpc-article-meta">2026-09-25</div> </li> </ul> </div> <!-- 热门标签 --> <div class="jxgpc-widget"> <h3 class="jxgpc-widget-title">热门标签</h3> <div class="jxgpc-article-tag-list"> <a href="http://www.jxgpc.com/tag/7589" class="jxgpc-article-tag-item">隐式转换</a> <a href="http://www.jxgpc.com/tag/3147" class="jxgpc-article-tag-item">字符串解析</a> <a href="http://www.jxgpc.com/tag/2372" class="jxgpc-article-tag-item">xml解析</a> <a href="http://www.jxgpc.com/tag/1691" class="jxgpc-article-tag-item">PHP</a> </div> </div> </aside> </div> </div> </main> <!-- 页脚 --> <footer class="jxgpc-footer"> <div class="jxgpc-container"> <div class="jxgpc-footer-links" style="justify-content: center;"> <a href="http://www.jxgpc.com/22.html" class="jxgpc-footer-link">免责声明</a> <a href="http://www.jxgpc.com/23.html" class="jxgpc-footer-link">关于我们</a> <a href="http://www.jxgpc.com/24.html" class="jxgpc-footer-link">联系我们</a> <a href="http://www.jxgpc.com/25.html" class="jxgpc-footer-link">投诉删除</a> <a href="http://www.jxgpc.com/26.html" class="jxgpc-footer-link">投稿须知</a> </div> <div class="jxgpc-footer-copyright" style="text-align: center; margin-top: 20px;"> Copyright 2026 极星编程网 版权所有 <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank" style="color: inherit;">黔ICP备2025063994号</a> </div> <div style="text-align: center; margin-top: 10px; font-size: 12px; color: #999;">免责声明:本站信息来源于网络,仅供学习使用,版权归原作者所有,如有不愿意被转载的情况,请联系QQ:182111886,通知我们删除已转载的信息。</div> </div> </footer> <!-- 返回顶部按钮 --> <button class="jxgpc-back-to-top" id="backToTop" aria-label="返回顶部">⇧</button> <!-- 公共脚本 --> <script src="http://www.jxgpc.com/static/v3/js/common.js"></script> <div class="hide"> </div> </body> </html>