刚开始碰php,解析xml时碰到有关问题了

WBOY
Release: 2016-06-13 11:06:23
Original
676 people have browsed it

刚开始碰php,解析xml时碰到问题了

本帖最后由 yxxue 于 2012-11-25 18:57:42 编辑 远程XML文档格式如下:



username="u1" sex="1" nickname="麦芽糖"
score="0" province="" city="" area="" />

Copy after login


我在PHP中这样获取到这个XML:
使用自己写的gethttpdata方法,是为了容错,避免访问远程服务器出错时程序报错。

$login_interface_url = "http://www.1.com/1.xml";
$data = gethttpdata($login_interface_url); //获取远程文件内容
$xml = simplexml_load_string($data);
$xmldata = var_export($xml,true);
//如果使用var_dump()就直接输出了,不是我想要的。

Copy after login


接下来我怎么读取这个xmldata呢?
如何获得中的各个属性的值呢?
------解决方案--------------------
本帖最后由 xuzuning 于 2012-11-25 22:28:58 编辑

默认是 utf-8 的
$string = << 

1
1

XML;

$xml = simplexml_load_string($string);//但也可以接受非 utf-8 声明的xml
foreach($xml->foo as $item) {
foreach($item->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
}
Copy after login

但结果还是 utf-8 的
name="Jack"
game="lonely"
name="千斤顶"
game="寂寞"
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!