Modify the content of the specified XML tag c# Get the tag in the xml node c# xml null value tag Tencent qq xml message tag

WBOY
Release: 2016-07-29 08:50:59
Original
1851 people have browsed it

Modify the content of the specified tag in Xml (I am removing the spaces in the content of the specified tag)

In fact, it is a very simple method, friends who need it can take it directly.

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>
Copy after login

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>?>
Copy after login

The above introduces the content of modifying XML specified tags, including xml and tag content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template