修改XML指定标签的内容 - KeerDi
修改Xml指定标签内容(我这是去掉指定标签内容的空格)
其实就是个很简单的方法,需要的盆友直接拿走。
test.xml
<span style="color: #0000ff;"></span><span style="color: #ff00ff;">xml version="1.0" encoding="UTF-8"</span><span style="color: #0000ff;">?></span> <span style="color: #0000ff;"><span style="color: #800000;">DOCUMENT</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">item</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">key</span><span style="color: #0000ff;">></span>记录博客是一个很好的习惯<span style="color: #0000ff;"></span><span style="color: #800000;">key</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">url</span><span style="color: #0000ff;">></span>http://www.cnblogs.com/keerdi/p/5465549.html<span style="color: #0000ff;"></span><span style="color: #800000;">url</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>去除XML的空格<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">content</span><span style="color: #0000ff;">></span>养成一个好习惯很重要, 这是一段有空 格的文件内容,<span style="color: #0000ff;"></span><span style="color: #800000;">content</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">date</span><span style="color: #0000ff;">></span>2016-1-12<span style="color: #0000ff;"></span><span style="color: #800000;">date</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">item</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">item</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">key</span><span style="color: #0000ff;">></span>大家一起来写博客<span style="color: #0000ff;"></span><span style="color: #800000;">key</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">url</span><span style="color: #0000ff;">></span>http://www.cnblogs.com/keerdi/p/5465549.html<span style="color: #0000ff;"></span><span style="color: #800000;">url</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>写博客可以增强记忆能力<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">content</span><span style="color: #0000ff;">></span>一般安 装后的linux系统都是英文的界面,网上查 了一下各种说法 都有, 我只做了如下的配 置就好了,下载个中文包,改一下i18 n就完事了,并没有那么复杂<span style="color: #0000ff;"></span><span style="color: #800000;">content</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">date</span><span style="color: #0000ff;">></span>2016-1-12<span style="color: #0000ff;"></span><span style="color: #800000;">date</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">item</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">DOCUMENT</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Copy after login
update.php
<span style="color: #000000;">php </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> actionGetXml(){ </span><span style="color: #800080;">$xmlpath</span> = 'test.xml'<span style="color: #000000;">; </span><span style="color: #800080;">$xml</span> = <span style="color: #0000ff;">new</span> DOMDocument("1.0","UTF-8"<span style="color: #000000;">); </span><span style="color: #800080;">$xml</span>->load(<span style="color: #800080;">$xmlpath</span><span style="color: #000000;">); </span><span style="color: #800080;">$root</span> = <span style="color: #800080;">$xml</span>-><span style="color: #000000;">documentElement; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取标签总数</span> <span style="color: #800080;">$nodes</span> = <span style="color: #800080;">$root</span>->getElementsByTagName("item"<span style="color: #000000;">); </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$nodes->length;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #008000;">//</span><span style="color: #008000;"> 指定content标签,并获取content标签内容通过delTrim()方法去除所有空格</span> <span style="color: #800080;">$str</span> = <span style="color: #800080;">$xml</span>->getElementsByTagName("content")->item(<span style="color: #800080;">$i</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> $str->nodeValue = "这里是要修改的内容";</span> <span style="color: #800080;">$str</span>->nodeValue = <span style="color: #800080;">$this</span>->delTrim(<span style="color: #800080;">$str</span>-><span style="color: #000000;">nodeValue); </span><span style="color: #800080;">$xml</span>->save("test.xml"<span style="color: #000000;">); } } </span><span style="color: #008000;">//</span><span style="color: #008000;">删除空格</span> <span style="color: #0000ff;">function</span> delTrim(<span style="color: #800080;">$object</span><span style="color: #000000;">) { </span><span style="color: #800080;">$search</span> = <span style="color: #0000ff;">array</span>(" ", " ", "\t", "\n", "\r"<span style="color: #000000;">); </span><span style="color: #800080;">$replace</span> = <span style="color: #0000ff;">array</span>("", "", "", "", ""<span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">str_replace</span>(<span style="color: #800080;">$search</span>, <span style="color: #800080;">$replace</span>, <span style="color: #800080;">$object</span><span style="color: #000000;">); } </span>?>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
4 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
4 weeks ago
By DDD
Two Point Museum: All Exhibits And Where To Find Them
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
