Heim php教程 php手册 修改XML指定标签的内容 - KeerDi

修改XML指定标签的内容 - KeerDi

May 20, 2016 am 10:14 AM

修改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>
Nach dem Login kopieren

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>?>
Nach dem Login kopieren

 

  

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)