<?php
Run:utf-8
$html= <<<EOF
<XML>
<info p="RMB:87" />
<data>
<d count="1" p="40" what="" who="" why="" type="1" sort="DIY日曆" id="56" s="../up_file/KSWUzf_1.png" />
<d count="1" p="47" what="" who="" why="" type="1" sort="DIY相册" id="57" s="../up_file/KSWUzf_2.jpg" />
</data>
</XML>
EOF;
$doc = new DOMDocument();
$doc->loadXML($html);
$tags = $doc->getElementsByTagName('d');foreach ($tags as $tag)
{
echo $tag->getAttribute('count').'<br>';
echo $tag->getAttribute('p').'<br>';
echo $tag->getAttribute('sort').'<br>';
#……
}
?>
解决方案 »
- 请教一下高手,PHP里怎么做每日领取2个游戏物品的功能???
- 用PHP读取文件输出,有乱码问题~~~请教下哪里有问题噢
- 关于脚本php,与javascript的优劣?
- 这么个奇怪的需求你们能帮我想到怎么做么?
- 如何用php给vsftp增加帐户?
- Call to a member function fetch_array() on a non-object错误,求助!
- PHP实现交通银行支付接口
- 想找一套象elong,携程类似的预订系统源码,最好是繁体的。。提供源码者免费提供2G高速全支持服务器空间!
- PHP可以做出关联菜单吗?谢谢!
- php如何get获取id,然后通过id查询后将数据返给html,html的datagrid接收值。
- php zend架构的那种网址真的可以防止注入吗?比如http://beyondsky.spaces.xxx.com/articles/article/item/12852
- 头晕,这样配置zendframework为什么不行
<?php
$html= <<<EOF
<XML>
<info p="RMB:87" />
<data>
<d count="1" p="40" what="" who="" why="" type="1" sort="DIY日曆" id="56" s="../up_file/KSWUzf_1.png" />
<d count="1" p="47" what="" who="" why="" type="1" sort="DIY相册" id="57" s="../up_file/KSWUzf_2.jpg" />
<d>haha</d>
</data>
</XML>
EOF;
$doc = new DOMDocument();
$doc->loadXML($html);
$tags = $doc->getElementsByTagName('d');foreach ($tags as $tag)
{
echo $tag->getAttribute('count').'';
echo $tag->getAttribute('p').'';
echo $tag->getAttribute('sort');
echo $tag->nodeValue.'<br>';
}
?>
再给你补全点.
怎么按照你的写法没数据出来呀?
我的那个问题因为忙,也一直没去管,现在测试出现以下问题:$html= <<<EOF
<XML>
<info p="RMB:87" />
<data>
<d count="1" p="40" what="" who="" why="" type="1" sort="DIY日曆" id="56" s="../up_file/KSWUzf_1.png" />
</data>
</XML>
EOF;
$doc = new DOMDocument();
echo var_dump($doc);//浏览器输出object(domdocument)(0) { }
$doc->loadXML($html);
$tags = $doc->getElementsByTagName('d');
echo $tags;echo $html;//没有输出
foreach ($tags as $tag)
{
echo $tag->getAttribute('count').'';
echo $tag->getAttribute('p').'';
echo $tag->getAttribute('what').'';
echo $tag->getAttribute('who').'';
echo $tag->getAttribute('why').'';
echo $tag->getAttribute('type').'';
echo $tag->getAttribute('sort').'';
echo $tag->getAttribute('s').'';
echo $tag->nodeValue.'<br>';
}
$doc->loadXML($html);这个有问题的吗?
貌似后面的程序都没执行。
echo var_dump($doc);//浏览器输出object(domdocument)(0) { }
$html=<<<EOF
<?xml version="1.0" encoding="gb2312" ?>
<data>
<d count="1" p="40" sort="DIY日历"></d>
<d count="1" p="40" sort="DIY日历"></d>
</data>
EOF;
$xml = simplexml_load_string($html);
foreach ($xml->d as $da) {
print_r($da);
}
?>
运行结果----------------------SimpleXMLElement Object
(
[@attributes] => Array
(
[count] => 1
[p] => 40
[sort] => DIY日历
))
SimpleXMLElement Object
(
[@attributes] => Array
(
[count] => 1
[p] => 40
[sort] => DIY日历
))
我用的是PHP4,没有simplexml的
请问怎么解决?