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 ...
随机推荐
- Lambda 表达式-即匿名函数
拉姆达值(Lambda),希腊字母表示为Λ,指与真空的空间有关的能量或暗能量. 代表转换的常量.或者转换本身. Lambda 表达式 Lambda 表达式”是一个匿名函数,可以包含表达式和语句 ...
- PAT 天梯赛练习集 L2-016. 愿天下有情人都是失散多年的兄妹
题目链接:https://www.patest.cn/contests/gplt/L2-016 呵呵.大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人.父母.祖父母.曾祖父母. ...
- BZOJ 2402 陶陶的难题II (01分数规划+树剖+线段树+凸包+二分)
题目大意:略 一定范围内求最大值,考虑二分答案 设现在选择的答案是$mid$,$max \left \{ \frac{yi+qj}{xi+pj} \right \} \geq mid $ 展开可得,$ ...
- [luogu4133 BJOI2012] 最多的方案 (计数dp)
题目描述 第二关和很出名的斐波那契数列有关,地球上的OIer都知道:F1=1, F2=2, Fi = Fi-1 + Fi-2,每一项都可以称为斐波那契数.现在给一个正整数N,它可以写成一些斐波那契数的 ...
- debian系统包管理工具aptitude
注意:aptitude与 apt-get 一样,是 Debian 及其衍生系统中功能极其强大的包管理工具.与 apt-get 不同的是,aptitude在处理依赖问题上更佳一些.举例来说,aptitu ...
- 3.如何构建Cython代码
一.与Python不同的是,Cython代码需要进行编译.发生两个阶段 将一个.pyx文件用Cython编译成一个.c文件中,包括Python扩展模块代码 将.c文件使用C编译器编译成.so文件(在w ...
- strlen()函数对一个未初始化数组的处理
今天使用strlen时 ,发现一个问题,demo代码如下: #include <stdio.h> #include <stdlib.h> #include <string ...
- Spring Cloud Feign 出现ClassNotFoundException: feign.Feign$Builder错误
Spring Cloud Feign 出现ClassNotFoundException: feign.Feign$Builder错误 后来发现是POM文件写错了,修改为正确的pom,就可以了: POM ...
- cocos2d-js 热更新具体解释(一)
本文将会具体解说cocos2d-js下的热更新机制.这篇内容先给大家介绍一下两个manifest文件就当热身了. 首先介绍project.manifest: 举个样例 { "package ...
- java结合jQuery.ajax实现左右菜单联动刷新列表内容
http://域名/一级菜单ID-二级菜单ID/ 用这种URL请求页面,出现如图所看到的内容: 该页面包括四部分,顶部文件夹+左側菜单+右側菜单+右下側数据列表. 左側菜单包括一级菜单和二级菜单,点击 ...