首頁 > 後端開發 > php教程 > 修改XML指定標籤的內容 c#取得xml節點內標籤 c# xml 空值標籤 騰訊qq xml訊息標

修改XML指定標籤的內容 c#取得xml節點內標籤 c# xml 空值標籤 騰訊qq xml訊息標

WBOY
發布: 2016-07-29 08:50:59
原創
1895 人瀏覽過

修改Xml指定標籤內容(我這是去掉指定標籤內容的空格)

其實就是個很簡單的方法,需要的盆友直接拿走。

test.xml

<span><?</span><span>xml version="1.0" encoding="UTF-8"</span><span>?></span><span><</span><span>DOCUMENT</span><span>></span><span><</span><span>item</span><span>></span><span><</span><span>key</span><span>></span>记录博客是一个很好的习惯<span></</span><span>key</span><span>></span><span><</span><span>url</span><span>></span>http://www.cnblogs.com/keerdi/p/5465549.html<span></</span><span>url</span><span>></span><span><</span><span>title</span><span>></span>去除XML的空格<span></</span><span>title</span><span>></span><span><</span><span>content</span><span>></span>养成一个好习惯很重要,  这是一段有空   格的文件内容,<span></</span><span>content</span><span>></span><span><</span><span>date</span><span>></span>2016-1-12<span></</span><span>date</span><span>></span><span></</span><span>item</span><span>></span><span><</span><span>item</span><span>></span><span><</span><span>key</span><span>></span>大家一起来写博客<span></</span><span>key</span><span>></span><span><</span><span>url</span><span>></span>http://www.cnblogs.com/keerdi/p/5465549.html<span></</span><span>url</span><span>></span><span><</span><span>title</span><span>></span>写博客可以增强记忆能力<span></</span><span>title</span><span>></span><span><</span><span>content</span><span>></span>一般安 装后的linux系统都是英文的界面,网上查  了一下各种说法  都有,  我只做了如下的配  置就好了,下载个中文包,改一下i18    n就完事了,并没有那么复杂<span></</span><span>content</span><span>></span><span><</span><span>date</span><span>></span>2016-1-12<span></</span><span>date</span><span>></span><span></</span><span>item</span><span>></span><span></</span><span>DOCUMENT</span><span>></span>
登入後複製

update.php

<?<span>php
    </span><span>function</span><span> actionGetXml(){
        </span><span>$xmlpath</span> = 'test.xml'<span>;
        </span><span>$xml</span> = <span>new</span> DOMDocument("1.0","UTF-8"<span>);
        </span><span>$xml</span>->load(<span>$xmlpath</span><span>);
        </span><span>$root</span> = <span>$xml</span>-><span>documentElement;
        </span><span>//</span><span> 获取标签总数</span><span>$nodes</span> = <span>$root</span>->getElementsByTagName("item"<span>);
        </span><span>for</span>(<span>$i</span>=0;<span>$i</span><=<span>$nodes</span>->length;<span>$i</span>++<span>){
            </span><span>//</span><span> 指定content标签,并获取content标签内容通过delTrim()方法去除所有空格</span><span>$str</span> = <span>$xml</span>->getElementsByTagName("content")->item(<span>$i</span><span>);
            </span><span>//</span><span> $str->nodeValue = "这里是要修改的内容";</span><span>$str</span>->nodeValue = <span>$this</span>->delTrim(<span>$str</span>-><span>nodeValue);
            </span><span>$xml</span>->save("test.xml"<span>);
        }
    }

    </span><span>//</span><span>删除空格</span><span>function</span> delTrim(<span>$object</span><span>) {
        </span><span>$search</span> = <span>array</span>(" ", " ", "\t", "\n", "\r"<span>);
        </span><span>$replace</span> = <span>array</span>("", "", "", "", ""<span>);
        </span><span>return</span><span>str_replace</span>(<span>$search</span>, <span>$replace</span>, <span>$object</span><span>);
    }
</span>?>
登入後複製

以上就介紹了修改XML指定標籤的內容,包含了xml,標籤方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板