simplexml_load_string获取xml节点里的属性值
http://stackoverflow.com/questions/14359658/get-xml-attribute-using-simplexml-load-string
问:
I am using a few third party APIs which returns errors via xml in the following form:
<xml>
<status>0</status>
<error code="111">Error message text goes here.</error>
</xml>
Using simplexml_load_string
in PHP I can easily get the status 0 and the error message text but I cannot find a means of retrieving the code="111"
value from the <error code="111">
. It seems to get dropped by SimpleXML.
<?php
$bytesRead = file_get_contents('http://api.....');
$xml = simplexml_load_string($bytesRead);
echo '<pre>'; print_r($xml); echo '</pre>';
?>
Outputs
SimpleXMLElement Object
(
[status] => 0
[error] => Error message text goes here.
)
Am I missing something? Is there a way to obtain this value or can someone suggest another method to get this?
回答:
So far, we have only covered the work of reading element names and their values. SimpleXML can also access element attributes. Access attributes of an element just as you would elements of an array.
Example:
$x = '<xml>
<status>0</status>
<error code="111">Error message text goes here.</error>
</xml>';
$attributeObject = simplexml_load_string($x)->error['code'];
print_r($attributeObject);
print_r((string) $attributeObject);
Program Output (Demo)
SimpleXMLElement Object
(
[0] => 111
)
111
simplexml_load_string获取xml节点里的属性值的更多相关文章
- 获取xml字符串中的属性值
pagexml = @"<?xml version='1.0' encoding='utf-8'?> <DATAPACKET Version='2.0'> <M ...
- Day14_84_通过反射机制修改和获取class里的属性值
通过反射机制修改和获取class里的属性值 * 属性对象.set(Object,属性值) 给Object对象中的某个属性赋值(属性对象) * 属性对象.get(Object); 获取Object对象中 ...
- Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从而去做一些运行时的逻辑处理
1.Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从而去做一些运行时的逻辑处理 2. 自定义注解: 使用@interfac ...
- 在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be closed first”
在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be ...
- 用LINQ获取XML节点数据
Insus.NET想对<从字符串中获取XML节点数据> http://www.cnblogs.com/insus/p/3299052.html 这篇改写为使用LINQ的方法实现.LINQ中 ...
- 从字符串中获取XML节点数据
从字符串中获取XML节点数据,前一篇<字符串创建XML文档> http://www.cnblogs.com/insus/p/3298579.html 是储存为一个XML文档.现在,Insu ...
- android中如何获取xml界面里的非自定义属性
获取自定义属性大家都很熟悉了,就不多说了(定义declare-styleable,context.obtainStyledAttributes巴拉巴拉...) 下面我们说一下怎么获取非自定义的属性,比 ...
- [Web 前端 ] Jquery attr()方法 获取或修改 对象的属性值
cp from : https://blog.csdn.net/gf771115/article/details/18086707 jquery中用attr()方法来获取和设置元素属性,attr是at ...
- Spring中获取外部配置文件中的属性值
很多时候需要将配置信息从程序中剥离粗来,Spring现在提供的方法是通过@Value注解和<context:placeholder>来获取配置文件中的配置信息.这里给出一个简单的例子. 首 ...
随机推荐
- Activity使用Dialog样式导致点击空白处自动关闭的问题
将Activity设置成窗口的样式实现Dialog或者Popupwindow效果在开发中是很常用的一种方式,在AndroidMenifest.xml中将需要设置的Activity增加android:t ...
- extern 相关
假如a.h中有 int a=10; t1.cpp和t2.cpp同时include "a.h"则编译不成功,因为a重复定义:如果 a.h中是 static int a=10;则可以, ...
- 【ITOO 1】将List数据导出Excel表
需求描述:在课表导入的时候,首先给用户提供模板(excel),然后将用户填写好的数据读取到list集合中.再进行判空处赋值处理,以及去重处理.这篇博客,主要介绍读取excel表和导出excel表的方法 ...
- SAS 5/iR Adapter 驱动下载
http://www.dell.com/support/home/cn/zh/cnbsd1/Drivers/DriversDetails?driverId=FF6F6
- CentOS 6.4安装Kangle面板
kangle web server一键安装包是一个用Linux Shell编写的可以为CentOS 6 VPS(VDS)或独立主机安装kangle web server(kangle,easypane ...
- SDUST 软件工程2016-作业4-A 百钱买鸡问题
解决百钱买鸡问题原本并不困难,关键的是这道题对其进行了升级,测试数据太大,传统的解法,像三重循环,二重循环都会导致超时. 这道题正确的解法应该是结合数学方程进行化简,将其转化为1层循环: x+y+z= ...
- C# 随机颜色的方法
public string GetRandomColor() { Random RandomNum_First = new Random((int)DateTime.Now.Ticks); // 对于 ...
- PHP四大安全策略
PHP中的文件系统安全.数据库安全.用户数据安全等安全相关的问题. 一.文件系统安全 php如果具有root权限,且在脚本中允许用户删除文件,那么用户提交数据,不进行过滤,就非常有可能删除系统文件 & ...
- wampserver php多版本5.2--5.6和apche2.2/2.4
一.准备 wampserver2.5 php5各版本 php5.2到php5.6 apache2.2和apache2.4 二.安装 先成功安装wampserver2.5,如果安装不成功,多是vc11没 ...
- hadoop2—namenode—HA原理详解
在hadoop1中NameNode存在一个单点故障问题,也就是说如果NameNode所在的机器发生故障,那么整个集群就将不可用(hadoop1中有个SecorndaryNameNode,但是它并不是N ...