PHP如何读XML(刚学PHP,请大家帮一下)

hisense 2009-04-10 09:42:17
我现在的配置是 centos5.3 php 5.2.5 在读xml时,总不成功,代码如下:
<?php
echo "start";

// 首先要建一个DOMDocument对象
try {
$xml = new DOMDocument("1.0");

// 加载Xml文件
$xml->load("me.xml");

// 获取所有的post标签
$postDom = $xml->getElementsByTagName("post");



// 循环遍历post标签
foreach($postDom as $post){
// 获取Title标签Node
$title = $post->getElementsByTagName("title");

/**
* 要获取Title标签的Id属性要分两部走
* 1. 获取title中所有属性的列表也就是$title->item(0)->attributes
* 2. 获取title中id的属性,因为其在第一位所以用item(0)
*
* 小提示:
* 若取属性的值可以用item(*)->nodeValue
* 若取属性的标签可以用item(*)->nodeName
* 若取属性的类型可以用item(*)->nodeType
*/


echo "Id: " . $title->item(0)->attributes->item(0)->nodeValue . "<br />";
echo "Title: " . $title->item(0)->nodeValue . "<br />";
echo "Details: " . $post->getElementsByTagName("details")->item(0)->nodeValue . "<br /><br />";
}

}
catch (Exception $e) {
print $e->getMessage();
exit();
}

echo "end";
?>

/////////////////
出错提示为:
X-Powered-By: PHP/5.2.5
Set-Cookie: ZendDebuggerCookie=127.0.0.1%3A10137%3A0||084|77742D65|1013; path=/
Content-type: text/html

start<br />
<b>Warning</b>: DOMDocument::load() [<a href='function.DOMDocument-load'>function.DOMDocument-load</a>]: Start tag expected, '<' not found in /var/www/html/TestXML/me.xml, line: 1 in <b>/var/www/html/TestXML/index.php</b> on line <b>9</b><br />
end


////////////////////////
me.xml:

<?xml version="1.0" eccoding="utf-8"?>]
<phplamp>
<post>
<title id="1">php dfs fd </title>
<details> 详细内容</details>
</post>
<post>
<title id="2">php dfs fd </title>
<details> 详细内容2</details>
</post>
</phplamp>


...全文
195 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hisense 2009-04-13
  • 打赏
  • 举报
回复
找到原因了:文件没有读取的权限。(但debug能读?),谢谢大家。
hisense 2009-04-10
  • 打赏
  • 举报
回复
我的php版本是5.1.6,在zendstudio 6.0.1下,调试Debugger location 选php executable?还是 php web server?
总哈哈 2009-04-10
  • 打赏
  • 举报
回复
我觉得编码有问题!
hisense 2009-04-10
  • 打赏
  • 举报
回复
发现$xml->load("me.xml"); 之前的echo 都能显示,但之后的都不显示?
程序猿之殇 2009-04-10
  • 打赏
  • 举报
回复
调试时能显示出来说明正确了.
hisense 2009-04-10
  • 打赏
  • 举报
回复
me.xml 已改为:

<?xml version="1.0" encoding="utf-8"?>
<phplamp>
<post>
<title id="1">php dfs fd </title>
<details> 详细内容</details>
</post>
<post>
<title id="2">php dfs fd </title>
<details> 详细内容2</details>
</post>
</phplamp>

在调试时能显示:
X-Powered-By: PHP/5.2.5
Set-Cookie: ZendDebuggerCookie=127.0.0.1%3A10137%3A0||084|77742D65|1019; path=/
Content-type: text/html

startId: 1<br />Title: php dfs fd <br />Details: 详细内容<br /><br />Id: 2<br />Title: php dfs fd <br />Details: 详细内容2<br /><br />end

但在页面上知西显示:start
ruanchao 2009-04-10
  • 打赏
  • 举报
回复
程序猿之殇 2009-04-10
  • 打赏
  • 举报
回复

<?xml version="1.0" eccoding="utf-8"?>
<phplamp>
<post>
<title id="1">php dfs fd </title>
<details> 详细内容 </details>
</post>
<post>
<title id="2">php dfs fd </title>
<details> 详细内容2 </details>
</post>
</phplamp>

第一行多个]
另外,请确定你的中文是utf-8
落叶秋2020 2009-04-10
  • 打赏
  • 举报
回复

<?
$doc = new DOMDocument; //xml开始解析
$doc->load("me.xml");
$post = $doc->getElementsByTagName( "post");
echo $post->length;
foreach($post as $a){
// 获取Title标签Node
$title = $a->getElementsByTagName("title")->item(0)->nodeValue;
$details = $a->getElementsByTagName("details")->item(0)->nodeValue;
echo iconv("UTF-8","GB2312",$details);

}

?>


解析出来了啊
lonelyriver 2009-04-10
  • 打赏
  • 举报
回复
……
ks_wang 2009-04-10
  • 打赏
  • 举报
回复
还没实现过⊙﹏⊙

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧