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节点里的属性值的更多相关文章

  1. 获取xml字符串中的属性值

    pagexml = @"<?xml version='1.0' encoding='utf-8'?> <DATAPACKET Version='2.0'> <M ...

  2. Day14_84_通过反射机制修改和获取class里的属性值

    通过反射机制修改和获取class里的属性值 * 属性对象.set(Object,属性值) 给Object对象中的某个属性赋值(属性对象) * 属性对象.get(Object); 获取Object对象中 ...

  3. Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从而去做一些运行时的逻辑处理

    1.Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从而去做一些运行时的逻辑处理 2. 自定义注解: 使用@interfac ...

  4. 在实体对象中访问导航属性里的属性值出现异常“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 ...

  5. 用LINQ获取XML节点数据

    Insus.NET想对<从字符串中获取XML节点数据> http://www.cnblogs.com/insus/p/3299052.html 这篇改写为使用LINQ的方法实现.LINQ中 ...

  6. 从字符串中获取XML节点数据

    从字符串中获取XML节点数据,前一篇<字符串创建XML文档> http://www.cnblogs.com/insus/p/3298579.html 是储存为一个XML文档.现在,Insu ...

  7. android中如何获取xml界面里的非自定义属性

    获取自定义属性大家都很熟悉了,就不多说了(定义declare-styleable,context.obtainStyledAttributes巴拉巴拉...) 下面我们说一下怎么获取非自定义的属性,比 ...

  8. [Web 前端 ] Jquery attr()方法 获取或修改 对象的属性值

    cp from : https://blog.csdn.net/gf771115/article/details/18086707 jquery中用attr()方法来获取和设置元素属性,attr是at ...

  9. Spring中获取外部配置文件中的属性值

    很多时候需要将配置信息从程序中剥离粗来,Spring现在提供的方法是通过@Value注解和<context:placeholder>来获取配置文件中的配置信息.这里给出一个简单的例子. 首 ...

随机推荐

  1. 【Hibernate 9】悲观锁和乐观锁

    一.锁的基本简介 1.1,为什么需要锁 首先,锁的概念产生,主要是为了解决并发性的问题.什么是并发性问题呢,比如: Angel现在银行有个账号,里面有存款1000块.现在,Angel的账户,在两个地方 ...

  2. AX 最顶部工作区间窗口文本修改

    修改Class\Info\method\workspaceWindowCreated: void workspaceWindowCreated(int _hWnd) { // Put workspac ...

  3. 有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎

    有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎 presto,hive,drill,calcite,sparksq

  4. 数学对象Math ceil()、floor()、round()方法

      Math.ceil()   功能:对一个数进行上取整. 语法:Math.ceil(x) 参数: x:一个数值. 返回值:返回大于或等于x,并且与之最接近的整数. 注:如果x是正数,则把小数“入”: ...

  5. Something wrong with FTK's index search results

    My friend she told me last week that FTK could not "see" keywords in a plain text files wh ...

  6. poj2027

    #include <stdio.h> int main(){ int n; int a,b; while(~scanf("%d",&n)){ while(n-- ...

  7. 一个简单的SpringMVC3 程序

    初学者对于Spring框架的难度:引用Jar包不全,或者不正确: 1.运行界面 2.客户端页面 index.jsp 的代码 <%@ page language="java" ...

  8. Windows服务编程Demo

    实现一个开机自动启动的关机程序,具体代码如下: #include <Windows.h> void ServiceMain(); void ControlHandler(DWORD req ...

  9. 乐够GO应用源码完整版

    乐够GO应用源码完整版 V1.0,系统2.3以上使用,需要联网,每天定时更新数据,实现了对文章赞的功能,以及常用的评论功能,还有生活的职业的相关功能,如查找功能,分类的分类等功能,具体大家可以看看应用 ...

  10. c#桌面小软件

    这是以前练习时用c#做的桌面小软件,今天回顾下. 这是设计界面 可以看出该程序能够播放网络歌曲及浏览新闻. 实现:歌曲来源百度API,播放WindowsMediaPlayer api地址:string ...