search
Homephp教程php手册修改XML指定标签的内容 - KeerDi

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

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

test.xml

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

update.php

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

    </span><span style="color: #008000;">//</span><span style="color: #008000;">删除空格</span>
    <span style="color: #0000ff;">function</span> delTrim(<span style="color: #800080;">$object</span><span style="color: #000000;">) {
        </span><span style="color: #800080;">$search</span> = <span style="color: #0000ff;">array</span>(" ", " ", "\t", "\n", "\r"<span style="color: #000000;">);
        </span><span style="color: #800080;">$replace</span> = <span style="color: #0000ff;">array</span>("", "", "", "", ""<span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">str_replace</span>(<span style="color: #800080;">$search</span>, <span style="color: #800080;">$replace</span>, <span style="color: #800080;">$object</span><span style="color: #000000;">);
    }
</span>?>

 

  

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.