<?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方法)的更多相关文章

  1. PLSQL_PLSQL读和写XML文件方式(案例)

    2012-05-01 Created By BaoXinjian

  2. Dom方法,解析XML文件

    Dom方法,解析XML文件的基本操作 package com.demo.xml.jaxp; import java.io.IOException; import javax.xml.parsers.D ...

  3. [Android L]SEAndroid开放设备文件结点权限(读或写)方法(涵盖常用操作:sys/xxx、proc/xxx、SystemProperties)

    温馨提示      建议你先了解一下上一篇博文([Android L]SEAndroid增强Androd安全性背景概要及带来的影响)所讲的内容,先对SEAndroid窥个全貌,然后再继续本节内容.   ...

  4. C#操作Xml:linq to xml操作XML

    LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...

  5. linq to xml操作XML(转)

    转自:http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html LINQ to XML提供了更方便的读写xml方式.前几 ...

  6. DOM解析xml实现读、写、增、删、改

    qt提供了三种方式解析xml,不过如果想实现对xml文件进行增.删.改等操作,还是DOM方式最方便. 项目配置 pro文件里面添加QT+=xml include <QtXml>,也可以in ...

  7. 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 ...

  8. Java解析XMl文件之SAX和DOm方法

    如题,这两种方法的jar包都在jdk中,不需要下载. 先来说下目录结构: 首先建一个Peron类封装person.xml的属性:DomParseService和SaxParseService分别为两种 ...

  9. Android解析xml文件-采用DOM,PULL,SAX三种方法解析

    解析如下xml文件 <?xml version="1.0" encoding="UTF-8"?> <persons> <perso ...

随机推荐

  1. struct和class两个关键字的区别

    1. <C++ Primer> 用class和struct关键字定义类的唯一差别在于默认访问级别:默认情况下,struct的成员为public,而class的成员为private. 2. ...

  2. iOS的流畅性

    1优先级别不同:iOS最先响应屏幕 优先级别不同:iOS最先响应屏幕 当我们使用iOS或者是Android手机时,第一步就是滑屏解锁找到相应程序点击进入.而这个时候往往是所有操控开始的第一步骤,iOS ...

  3. Unity局部坐标系与世界坐标系的区别

    局部坐标旋转是指以父物体为参考,进行旋转. 而世界坐标系以"坐标陀螺"来进行旋转. 类似的情况例如: 东.南.西.北.是世界坐标系. 前.后.左.右是局部坐标系

  4. java的插入排序

    import java.util.Scanner;public class test22 { public static void main(String[] args) {  Scanner in= ...

  5. Python笔记26----正则表达式匹配

    1.语法: 2.题目1:数据类型: 如果要选择 time 为2014-12-18那天的数据:采用正则化来处理 代码:   import re regex = re.compile(r'^2014-12 ...

  6. HDU1061 - Rightmost Digit

    Given a positive integer N, you should output the most right digit of N^N. Input The input contains ...

  7. NOIP2018提高组金牌训练营——数论专题

    地址 https://www.51nod.com/live/liveDescription.html#!liveId=23 1187 寻找分数 给出 a,b,c,d, 找一个分数p/q,使得a/b & ...

  8. 【codeforces 794C】Naming Company

    [题目链接]:http://codeforces.com/contest/794/problem/C [题意] 有n个位置; 两个人; 每个人都有n个字符组成的集合s1,s2(可以有重复元素); 然后 ...

  9. codevs 1803 志愿者招募

    1803 志愿者招募 2008年NOI全国竞赛  时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master   题目描述 Description 申奥成功后,布布经过不懈努 ...

  10. C++11时间具体解释

    转载请注明出处:http://blog.csdn.net/luotuo44/article/details/46854229 C++ 11添加了三个与时间相关的类型:时间段.时钟.时间点. 以史为鉴 ...