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>来获取配置文件中的配置信息.这里给出一个简单的例子. 首 ...
随机推荐
- 中断是CPU的机制
中断是CPU的机制,不管运行的是什么操作系统,只有是运行于x86架构,IDT结构式必然存在的.IDT表中的ISRs应该有操作系统提供
- SAS 5/iR Adapter 驱动下载
http://www.dell.com/support/home/cn/zh/cnbsd1/Drivers/DriversDetails?driverId=FF6F6
- 1.5如何学习Linux驱动开发
1.准备一个自己熟悉的Linux操作系统,用于开发和测试Linux驱动,建议使用Ubuntu Linux 10.04及以上版本: 2.准备一块开发板,建议采用基于ARM11的开发板: 3.学习GUN ...
- centos6.5下yum安装lnmp(适合刚入职的新手的方法)
新入职的员工,开始的时候都是让配环境,本地写代码用的wamp,在lnmp或lamp测试,除非有些土豪公司 用的是(果机). 另外安装时,把整个流程在脑子里先过一篇(记不全也没关系,一回生二回熟),重在 ...
- MinGW编译wxWidgets中的问题及解决方法
其实网上wxWidgets编译相关的博文,都没写到关键点上,泛泛而谈——就写了执行几个命令,就万事大吉了! 维基百科上的这个页面讲解了编译中可能遇到的各种问题及解决办法.比较懒,不想翻译.wxWidg ...
- HTML5 学习笔记 1
1.音频.视频 <!DOCTYPE HTML> <html> <body> <audio controls="controls"> ...
- DevExpress GridControl 使用方法技巧 总结 收录整理
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...
- Toast提示信息
用Toast来作为操作成功以及用户误操作等等的提示,非常的简单.直接上代码: 创建方式一: ps: 此处没有设置toast的其他属性,均使用默认的风格(个人觉得默认的风格除了字体比较小之外 还是挺好看 ...
- Hadoop伪分布式搭建CentOS
所需软件及版本: jdk-7u80-linux-x64.tar.gz hadoop-2.6.0.tar.gz 1.安装JDK Hadoop 在需在JDK下运行,注意JDK最好使用Oracle的否则可能 ...
- IOS下载资源zip到本地然后读取
思路是 1.ios下载服务器上的zip资源包(图片,声音等经过zip压缩的资源包)到本地 2.解压zip到程序目录 3.从程序目录加载资源文件 一.下载zip资源 [cpp]-(NSString*)D ...