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 チュートリアルに興味のある方の参考になれば幸いです。