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 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.