Heim >Backend-Entwicklung >PHP-Tutorial >修改XML指定标签的内容 c#获取xml节点内标签 c# xml 空值标签 腾讯qq xml消息标

修改XML指定标签的内容 c#获取xml节点内标签 c# xml 空值标签 腾讯qq xml消息标

WBOY
WBOYOriginal
2016-07-29 08:50:591857Durchsuche

修改Xml指定标签内容(我这是去掉指定标签内容的空格)

其实就是个很简单的方法,需要的盆友直接拿走。

test.xml

<span></span><span>xml version="1.0" encoding="UTF-8"</span><span>?></span><span><span>DOCUMENT</span><span>></span><span><span>item</span><span>></span><span><span>key</span><span>></span>记录博客是一个很好的习惯<span></span><span>key</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>title</span><span>></span>去除XML的空格<span></span><span>title</span><span>></span><span><span>content</span><span>></span>养成一个好习惯很重要,  这是一段有空   格的文件内容,<span></span><span>content</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>item</span><span>></span><span><span>key</span><span>></span>大家一起来写博客<span></span><span>key</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>title</span><span>></span>写博客可以增强记忆能力<span></span><span>title</span><span>></span><span><span>content</span><span>></span>一般安 装后的linux系统都是英文的界面,网上查  了一下各种说法  都有,  我只做了如下的配  置就好了,下载个中文包,改一下i18    n就完事了,并没有那么复杂<span></span><span>content</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></span></span></span></span></span></span></span></span></span></span></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>$nodes->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教程有兴趣的朋友有所帮助。

Stellungnahme:
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