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>来获取配置文件中的配置信息.这里给出一个简单的例子. 首 ...
随机推荐
- SimpleAdapter类使用方法
SimpleAdapter的构造函数是: public SimpleAdapter (Context context, List<? extends Map<String, ?>&g ...
- java 学习之(基本语法)
强类型:1所有变量必须先定义,再使用.2变量所赋值必须为与其类型匹配的类型. 标识符与关键字 分隔符:;{},[],() space . 用法与.net一样 标识符规则:以_ 字母 $符开头 ...
- ref传递
下面通过一个排序的小栗子来分析ref传递: static void Main(string[] args) { ,,,,}; int num; Console.WriteLine("请输入您 ...
- PAT1020. Tree Traversals
//典型后中省树,这种方法必须有 中序序列来确定根的位置,然后二分建树: //因为用的vc,之前用序列位置建树通不过,用坐标建树通过了,怀疑vc的功能限制,有时间再来测试,眼下感觉还是坐标好啊,用地址 ...
- myecplise 添加svn插件
myecplise svn插件下载地址 http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 1.SVN下载地址 ...
- Oracle笔记 目录索引
Oracle笔记 一.oracle的安装.sqlplus的使用 Oracle笔记 二.常用dba命令行 Oracle笔记 三.function .select Oracle笔记 四.增删改.事务 Or ...
- C++著名程序库的比较和学习经验 (转)
转自:http://www.open-open.com/lib/view/open1328670468108.html 内容目录: 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介 ...
- Eclipse 安装Groovy插件
摘自: http://blog.csdn.net/boonya/article/details/45399901 步骤一: 下载eclipse4.3.0,地址:http://www.eclipse.o ...
- c++ 创建 socket server
下面一段代码是创建socket server的代码片段: 需要引用的库包括: #include <sys/types.h> #include <sys/socket.h> #i ...
- [原]Hrbust1328 相等的最小公倍数 (筛素数,素因子分解)
本文出自:http://blog.csdn.net/svitter/ 题意: 求解An 与 An-1是否相等. n分为两个情况-- 1.n为素数, 2.n为合数. = =好像说了个废话..素数的时候 ...