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>来获取配置文件中的配置信息.这里给出一个简单的例子. 首 ...
随机推荐
- bzoj1003 [ZJOI2006]物流运输
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6300 Solved: 2597[Submit][Stat ...
- devexpress中应用于girdviw中HtmlDataCellPrepared事件与CellEditorInitialize事件的区别
HtmlDataCellPrepared 事件为页面展示的时候对页面做的初始化(将id变为name) CellEditorInitialize 事件为页面在编辑时(新增.修改)时做的初始化,如将值填 ...
- Avl树的基本操作(c语言实现)
#include<stdio.h> #include<stdlib.h> typedef struct AvlNode *Position; typedef struct Av ...
- C# 共用的返回数据类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dscf ...
- 使用/proc实现内核与用户空间通信
1. 前言 Linux内核空间与用户空间的通信可通过"/proc"目录的文件读写来实现,如果只是控制内核中的参数而不是传输较多数据的话,用“/proc”是很合适的.另外一种内核 ...
- Jquery设置select控件指定text的值为选中项
<select name="streetid" id="streetid"> <option value="4">北 ...
- android中关闭软键盘
/**隐藏软键盘**/ View view = getWindow().peekDecorView(); if (view != null) { InputMethodManager inputman ...
- nth-of-type和nth-child的区别
看CSS3时发现了一个nth-of-type选择器,发现平时基本没见过用,就研究了一下,w3c是这样说明的: :nth-of-type(n) 选择器匹配属于父元素的特定类型的第 N 个子元素的每个元素 ...
- Filestream读取或写入文件
using System.IO;//引用 System.IO namespace filestream { public partial class Form1 : Form { public For ...
- js对象与this指向
创建对象的方法 1.对象字面量法 var obj={} var obj={ 'first-name':'Tom', 'last-name':'bush', age:24, Family:{ Broth ...