搜索
首页php教程php手册在PHP5中使用DOM控制XML

  PHP5中增强了XML的支持,使用DOM扩展了XML操作的能耐。这些函数作为 PHP5 核心的一部分,无需被安装即可使用。

  下面的例子简单的演示了DOM对XML的操作,详细解释请看代码中的注释


/************************************************
**                    use XML in PHP5
** reference site:
** http://cn.php.net/manual/zh/ref.dom.php
** the follow codes need PHP5 support
*************************************************/


//首先要创建一个DOMDocument对象
$dom = new DomDocument();
//然后载入XML文件
$dom -> load("test.xml");

//输出XML文件
//header("Content-type: text/xml;charset=gb2312");
//echo $dom -> saveXML();

//保存XML文件,返回值为int(文件大小,以字节为单位)
//$dom -> save("newfile.xml");

echo "


取得所有的title元素:
";
$titles = $dom -> getElementsByTagName("title");
foreach ($titles as $node)
{
  echo $node -> textContent . "
";
  //这样也可以
  //echo $node->firstChild->data . "
";
}

/*
echo "


从根结点遍历所有结点:
";
foreach ($dom->documentElement->childNodes as $items) {
  //如果节点是一个元素(nodeType == 1)并且名字是item就继续循环
  if ($items->nodeType == 1 && $items->nodeName == "item") {
    foreach ($items->childNodes as $titles) {
      //如果节点是一个元素,并且名字是title就打印它.
      if ($titles->nodeType == 1 && $titles->nodeName == "title") {
        print $titles->textContent . "\n";
      }
    }
  }
}
*/

//使用XPath查询数据
echo "


使用XPath查询的title节点结果:
";
$xpath = new domxpath($dom);
$titles = $xpath->query("/rss/channel/item/title");
foreach ($titles as $node)
{
  echo $node->textContent."
";
}
/*
这样和使用getElementsByTagName()方法差不多,但是Xpath要强大的多
深入一点可能是这样:
/rss/channel/item[position() = 1]/title 返回第一个item元素的所有
/rss/channel/item/title[@id = '23'] 返回所有含有id属性并且值为23的title
/rss/channel/&folder&/title 返回所有articles元素下面的title(译者注:&folder&代表目录深度)
*/


//向DOM中写入新数据
$item = $dom->createElement("item");
$title = $dom->createElement("title");
$titleText = $dom->createTextNode("title text");
$title->appendChild($titleText);
$item->appendChild($title);
$dom->documentElement->getElementsByTagName('channel')->item(0)->appendChild($item);

//从DOM中删除节点
//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0));
//或者使用xpath查询出节点再删除
//$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0));
//$dom->save("newfile.xml");

//从DOM中修改节点数据
//修改第一个title的文件
//这个地方比较笨,新创建一个节点,然后替换旧的节点。如果哪位朋友有其他好的方法请一定要告诉我
$firstTitle = $xpath->query("/rss/channel/item/title")->item(0);
$newTitle = $dom->createElement("title");
$newTitle->appendChild(new DOMText("This's the new title text!!!"));
$firstTitle->parentNode->replaceChild($newTitle, $firstTitle);
//修改属性
//$firstTitle = $xpath->query("/rss/channel/item/title")->item(0);
//$firstTitle->setAttribute("orderby", "4");
$dom->save("newfile.xml");

echo "


查看newfile.xml";

//下面的代码获得并解析php.net的首页,将返第一个title元素的内容。
/*
$dom->loadHTMLFile("http://www.php.net/");
$title = $dom->getElementsByTagName("title");
print $title->item(0)->textContent;
*/
?>

下面是test.xml文件代码:




javascript
http://blog.csdn.net/zhongmao/category/29515.aspx
javascript
zh-chs
.text version 0.958.2004.2001

zhongmao
out put excel used javascript
http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx
wed, 15 sep 2004 13:32:00 gmt
http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx
http://blog.csdn.net/zhongmao/comments/105385.aspx
http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx#feedback
2
http://blog.csdn.net/zhongmao/comments/commentrss/105385.aspx
http://blog.csdn.net/zhongmao/services/trackbacks/105385.aspx
test description


zhongmao
out put word used javascript
http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx
fri, 06 aug 2004 16:33:00 gmt
http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx
http://blog.csdn.net/zhongmao/comments/67161.aspx
http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback
0
http://blog.csdn.net/zhongmao/comments/commentrss/67161.aspx
http://blog.csdn.net/zhongmao/services/trackbacks/67161.aspx
test word description


zhongmao
xmlhttp
http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx
mon, 02 aug 2004 10:11:00 gmt
http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx
http://blog.csdn.net/zhongmao/comments/58417.aspx
http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback
0
http://blog.csdn.net/zhongmao/comments/commentrss/58417.aspx
http://blog.csdn.net/zhongmao/services/trackbacks/58417.aspx
xmlhttpaaa asd bb cc dd




声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版