SimpleXML获取节点值 [英] SimpleXML get node value

查看:128
本文介绍了SimpleXML获取节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我具有以下XML结构:

Say I have this following XML structure:

<?xml version="1.0" encoding="UTF-8"?>
<main>
    <parent>
        <child1>some value</child1>
        <child2>another value</child2>
    </parent>
</main>

我做了一个XML变量,现在我想获取child1的值,所以我使用SimpleXML:

I made a variable of the XML and now I want to get the values of child1, so I use SimpleXML:

$xml = new SimpleXMLElement($xml);
$this->xmlcode = (string) $xml->main->parent->child1;

但我收到此消息:注意:试图在x的/x.php中获取非对象的属性

But I get this message: Notice: Trying to get property of non-object in /x.php on line x

我也用$ xml-> parent-> child1尝试过,但是没有成功.

I also tried it with $xml->parent->child1, but no success.

有人吗?

推荐答案

$xml = new SimpleXMLElement($xml);
$this->xmlcode = (string) $xml->parent[0]->child1;

这篇关于SimpleXML获取节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆