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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-20 10:14:52
Original
1441 people have browsed it

修改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

 

  

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template