本文介绍了PHP解析XML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含XML数据的字符串.如何在PHP中解析?

I have a string with XML data in it. How do I parse it in PHP?

谢谢

推荐答案

尝试使用简单XML ,这是一个示例:

Try with simple XML, here's an example:

do.php:

<?php
$xml_str = file_get_contents('xmlfile.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('*/item');

foreach($items as $item) {
    echo $item['title'], ': ', $item['description'], "\n";
}

xmlfile.xml:

xmlfile.xml:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <items>
        <item title="Hello World" description="Hellowing the world.." />
        <item title="Hello People" description="greeting people.." />
    </items>
</xml>

这篇关于PHP解析XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 23:14
查看更多