分解 XML 文件

发布于 2024-10-08 06:41:22 字数 380 浏览 0 评论 0原文

我有一个 XML 输出,它生成如下代码:

<loadavg>
   <one>0.00</one>
   <five>0.02</five>
   <fifteen>0.02</fifteen>
</loadavg>
<!-- whostmgrd -->

我想知道如何使用 PHP 解析该文件,并获取 ,<; 之间的内容。五><十五>。如果将其存储为 $loadavg[1] 数组,则会很有用。

谢谢

I've got an XML output that produces code such as:

<loadavg>
   <one>0.00</one>
   <five>0.02</five>
   <fifteen>0.02</fifteen>
</loadavg>
<!-- whostmgrd -->

I would like to know how I can use PHP to parse that file, and grab the contents between <one>,<five> and <fifteen>. It would be useful if it were to be stored as $loadavg[1] an array.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2024-10-15 06:41:22

是的, SimpleXML 可以做到:

$xml = <<<XML
    <loadavg>
        <one>0.00</one>
        <five>0.02</five> 
        <fifteen>0.02</fifteen>
    </loadavg>
XML;

$root = new SimpleXMLElement($xml);
$loadavg = array((string) $root->one, 
                 (string) $root->five, 
                 (string) $root->fifteen);

print_r($loadavg);

打印

Array (
   [0] => 0.00
   [1] => 0.02
   [2] => 0.02 
)

Yep, SimpleXML can do it:

$xml = <<<XML
    <loadavg>
        <one>0.00</one>
        <five>0.02</five> 
        <fifteen>0.02</fifteen>
    </loadavg>
XML;

$root = new SimpleXMLElement($xml);
$loadavg = array((string) $root->one, 
                 (string) $root->five, 
                 (string) $root->fifteen);

print_r($loadavg);

prints

Array (
   [0] => 0.00
   [1] => 0.02
   [2] => 0.02 
)
花辞树 2024-10-15 06:41:22

解析 XML 文件的最简单方法是使用 SimpleXML。将 XML 作为 SimpleXML 对象加载并使用它获取数据应该很容易。如果您可以发布完整 XML 文件的示例,我可以提供一些示例代码。

The easiest way to parse an XML file is using SimpleXML. It should be easy to load the XML as a SimpleXML object and get the data using that. If you could post a sample of a complete XML file I could offer some sample code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文