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 ...
随机推荐
- eclipse 启动程序时错误弹窗:multiple problems have occurred
.log内容如下: !ENTRY org.eclipse.ui 4 4 2017-04-14 09:31:05.341!MESSAGE An internal error has occurred.! ...
- zabbix、agent端服务器图形化展示
[root@agent ~]# cat /etc/hostname agent.zabbix.com [root@agent ~]# cat /etc/hosts 127.0.0.1 localh ...
- Git的初始化设置
Git安装成功之后,新建一个初始化的仓库以及配置GitHub仓库 Git配置GitHub账户 安装完成之后要进行git的配置,这里配置的是GitHub账户 MisSa@DESKTOP-PIQ06QO ...
- freeswitch 中文语音
1.下载中文语音包 链接:https://pan.baidu.com/s/1UODvqj8NAQw7_CRatfl0kg 提取码:qwdn 创建目录 /usr/local/freeswitch/sou ...
- java判断输入的数字的位数_数字问题
import java.util.Scanner;public class Numbers { public void Judgy(int n){ for(int i=0;i<100;i++){ ...
- echarts图表属性说明
参考博客: https://blog.csdn.net/luanpeng825485697/article/details/76691965
- Python学习笔记(7)字典
2019-03-07 字典(dict): (1)字典用大括号({})定义,字典由多个键及其对应的值组合而成,每一对键值组合称为项. (2)字典的键唯一,但是值可以是任何(不可变的)数据类型(整型,字符 ...
- Django -聚合分组,FQ操作, cookie, session
一. 聚合查询和分组 1. 聚合 aggregate(*args, **kwargs) 对一组数据进行统计分析, 通过对QuerySet进行计算, 返回一个聚合值得字典. arrgregate()中每 ...
- poj 3311 Hie with the Pie (状压dp) (Tsp问题)
这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...
- RabbitMQ学习总结(6)——消息的路由分发机制详解
一.Routing(路由) (using the Java client) 在前面的学习中,构建了一个简单的日志记录系统,能够广播所有的日志给多个接收者,在该部分学习中,将添加一个新的特点,就是可以只 ...