PHP读xml、写xml(DOM方法)
<?php
/**
* 读取的xml的格式
* <urlset>
* <url>
* <loc>http://www.51buy.com/0.html</loc>
* <priority>1.0</priority>
* <lastmod>Wed, 12 Jun 2013 21:37:52 +0800</lastmod>
* <changefreq>Always</changefreq>
* </url>
* <url>
* <loc>http://www.baidu.com/1.html</loc>
* <priority>1.0</priority>
* <lastmod>Tue, 11 Jun 2013 15:39:17 +0800</lastmod>
* <changefreq>Always</changefreq>
* </url>
* <url>
* <loc>http://www.jd.com/2.html</loc>
* <priority>1.0</priority>
* <lastmod>Tue, 11 Jun 2013 01:21:46 +0800</lastmod>
* <changefreq>Always</changefreq>
* </url>
* </urlset>
*/
//1读取xml
header("Content-type: text/html; charset=utf-8");
// 首先要建一个DOMDocument对象
$xml = new DOMDocument();
// 加载Xml文件
$xml->load("http://www.baidu.com/sitemap.xml");
// 获取所有的post标签
$postDom = $xml->getElementsByTagName("url");
// 循环遍历post标签
$array = array();
foreach($postDom as $post){
// 获取Title标签Node
$title = $post->getElementsByTagName("loc");
$url = $title->item(0)->nodeValue;
//替换数组中某个值为指定字符串(没有需要的此行可以删除)
$url1 = str_replace(array("w.baidu.com",'book.baidu.com','iworld.baidu.com'),"www.baidu.com/nihao",$url);
//priority
$priority= $post->getElementsByTagName("priority")->item(0)->nodeValue;
//lastmod
$lastmod= $post->getElementsByTagName("lastmod")->item(0)->nodeValue;
//changefreq
$changefreq= $post->getElementsByTagName("changefreq")->item(0)->nodeValue;
$article_array = array('loc'=>$url1, 'priority'=>$priority, 'lastmod'=>$lastmod, 'changefreq'=>$changefreq);
$array[] = $article_array; }
//echo "<pre>";
//var_dump($array);
//print_r($array);
//删除XML文件
/*
$file = "2222.html";//此处
if (!unlink($file))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $file");
}
*/ /**
* 写xml的数组的形式
* Array
(
[0] => Array
(
[loc] => http://www.51buy.com/0.html
[priority] => 1.0
[lastmod] => Wed, 12 Jun 2013 21:37:52 +0800
[changefreq] => Always
) [1] => Array
(
[loc] => http://www.51buy.com/0.html
[priority] => 1.0
[lastmod] => Tue, 11 Jun 2013 15:39:17 +0800
[changefreq] => Always
) [2] => Array
(
[loc] => http://www.51buy.com/0.html
[priority] => 1.0
[lastmod] => Tue, 11 Jun 2013 01:21:46 +0800
[changefreq] => Always
)
)
*/
//2写xml
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$rootelement = $dom->createElement("urlset");
foreach ($array as $key=>$value){
$article = $dom->createElement("url");
//$article = $dom->createElement("article", $key);
$loc = $dom->createElement("loc", $value['loc']);
$priority = $dom->createElement("priority", $value['priority']);
$lastmod = $dom->createElement("lastmod", $value['lastmod']);
$changefreq = $dom->createElement("changefreq", $value['changefreq']);
$article->appendChild($loc);
$article->appendChild($priority);
$article->appendChild($lastmod);
$article->appendChild($changefreq);
$rootelement->appendChild($article);
}
$dom->appendChild($rootelement);
$filename = "./test.xml";
echo 'XML文件大小' . $dom->save($filename) . '字节';
PHP读xml、写xml(DOM方法)的更多相关文章
- PLSQL_PLSQL读和写XML文件方式(案例)
2012-05-01 Created By BaoXinjian
- Dom方法,解析XML文件
Dom方法,解析XML文件的基本操作 package com.demo.xml.jaxp; import java.io.IOException; import javax.xml.parsers.D ...
- [Android L]SEAndroid开放设备文件结点权限(读或写)方法(涵盖常用操作:sys/xxx、proc/xxx、SystemProperties)
温馨提示 建议你先了解一下上一篇博文([Android L]SEAndroid增强Androd安全性背景概要及带来的影响)所讲的内容,先对SEAndroid窥个全貌,然后再继续本节内容. ...
- C#操作Xml:linq to xml操作XML
LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...
- linq to xml操作XML(转)
转自:http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html LINQ to XML提供了更方便的读写xml方式.前几 ...
- DOM解析xml实现读、写、增、删、改
qt提供了三种方式解析xml,不过如果想实现对xml文件进行增.删.改等操作,还是DOM方式最方便. 项目配置 pro文件里面添加QT+=xml include <QtXml>,也可以in ...
- C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)【转载】
已知有一个XML文件(bookstore.xml)如下: Corets, Eva 5.95 1.插入节点 往节点中插入一个节点: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- Java解析XMl文件之SAX和DOm方法
如题,这两种方法的jar包都在jdk中,不需要下载. 先来说下目录结构: 首先建一个Peron类封装person.xml的属性:DomParseService和SaxParseService分别为两种 ...
- Android解析xml文件-采用DOM,PULL,SAX三种方法解析
解析如下xml文件 <?xml version="1.0" encoding="UTF-8"?> <persons> <perso ...
随机推荐
- jq——属性和方法
ps:所有元素要加上“” 属性: 1) attr:属性,元素.attr(); 获取属性 $("input").click(function(){ console.log($( ...
- [置顶]
openHAB 体系结构与编程模型 (1) --- 术语
openHAB 术语 Item : 对硬件设备属性的抽象 ( Items are objects that can be read from or written to in order to int ...
- Vue学习之路第七篇:跑马灯项目实现
前面六篇讲解了Vue的一些基础知识,正所谓:学以致用,今天我们将用前六篇的基础知识,来实现类似跑马灯的项目. 学前准备: 需要掌握定时器的两个函数:setInterval和clearInterval以 ...
- Java web课程学习之Request和Response
request和response l HttpServletRequest l 请求转发 l HttpServletResponse l 请求重定向 请求流程 每次请求service(),都会由容 ...
- 安装idea
1.下载idea https://www.jetbrains.com/idea/download/#section=linux 2.解压 sudo tar -zxvf ideaIC-2018.3.2 ...
- webpack中关于require与import的区别
1.require常见使用场景: var path = require('path') var utils = require('./utils') 此时webpack会将path/utils/con ...
- NOIP2018提高组金牌训练营——搜索专题
NOIP2018提高组金牌训练营——搜索专题 1416 两点 福克斯在玩一款手机解迷游戏,这个游戏叫做”两点”.基础级别的时候是在一个n×m单元上玩的.像这样: 每一个单元有包含一个有色点.我们将用不 ...
- 优化VR体验的7个建议
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/50392607 作者:ca ...
- VUE:内置指令与自定义指令
VUE:内置指令与自定义指令 常用的内置指令 1)v:text 更新元素的 textContent 2)v-html 更新元素的 innerHTML 3)v-if 如果为true,当前标签才会输出到页 ...
- 使用excel进行数据挖掘(3)----类别检測
使用excel进行数据挖掘(3)----类别检測 在配置环境后,能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/artic ...