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>来获取配置文件中的配置信息.这里给出一个简单的例子. 首 ...
随机推荐
- swat主流域文件(file.cio)参数详解——引自http://blog.sciencenet.cn/blog-922140-710636.html
% file.clo,即主流域文件用于文件管理,包括与模型选项.气候输入.数据库和输出控制相关的信息. Master Watershed File: file.cio Project Descript ...
- Unable to add App ID because the '10' App ID limit in '7' days has been exceeded.
Unable to add App ID because the '10' App ID limit in '7' days has been exceeded. 官方的原因是对bundle iden ...
- js实现文字截断
先前用jq做了一个文字截断功能,但是不用jq的项目要实现此功能还要引如jq显得过于麻烦.这里写了一个js的文字截断功能.直接上代码. HTML(测试用的): <div>我是pox我是pox ...
- c语言学习的第四天2
上图显示了float和double的精度,精度:就是指数值的精确程度,浮点类型可表示的数值范围很大,但只有几位是精确的,可以通过小 数所占的位数来获取,float小数部分最多能有7位有效数字,但绝对能 ...
- 微软必应·英雄会第三届在线编程大赛:几个bing?
发布公司:微软亚太研发集团 有 效 期:2013-12-31至2014-02-01 难 度 等 级: 答 题 时 长:120分钟 编程语言要求:C C++ Java C# 悬赏详情 一等奖 : 价值2 ...
- sprintf函数减少字符串拼接错误
$return_string=""; foreach($cat_list as $value){ $return_string .= sprintf('<dd>< ...
- WordPress 主题开发 - (十二) Search模板与Page模板 待翻译
The Search Template and The Page Template are vital to any complete WordPress Theme. And they're bot ...
- 重拾C,一天一点点_9-指针与数组
这一章节很重要,一定要多思考.理解! 指针是一种保存变量地址的变量. 通常的机器 都有一系列连续编号或编址的存储单元.一个字节可存char类型,两相邻字节存储单元可存一个short,依此类推. p = ...
- php循环创建目录
代码取自thinkphp中: function mk_dir($dir, $mod = 0777) { if(!is_dir($dir) || mkdir($dir, $mod)) { if(!mk_ ...
- Eclipse编码问题
通常在Eclipse下,mac和windows编码是不一样的.如果含有中文java sources通常会出现乱码. 解决---小程序! import java.io.File; import java ...