search
HomeBackend DevelopmentPHP TutorialPHP XML操作类DOMDocument

不得不自已写一个.XML 的操作一直没有用过.下面是自己搜集的XML操作类

DOMDocument相关的内容.

属性:
Attributes--->存储节点的属性列表(只读)
childNodes--->存储节点的子节点列表(只读)
dataType--->返回此节点的数据类型
Definition--->以DTD或XML模式给出的节点的定义(只读)
Doctype--->指定文档类型节点(只读)
documentElement--->返回文档的根元素(可读写)
firstChild--->返回当前节点的第一个子节点(只读)
Implementation--->返回XMLDOMImplementation对象
lastChild--->返回当前节点最后一个子节点(只读)
nextSibling--->返回当前节点的下一个兄弟节点(只读)
nodeName--->返回节点的名字(只读)
nodeType--->返回节点的类型(只读)
nodeTypedValue--->存储节点值(可读写)
nodeValue--->返回节点的文本(可读写)
ownerDocument--->返回包含此节点的根文档(只读)
parentNode--->返回父节点(只读)
Parsed--->返回此节点及其子节点是否已经被解析(只读)
Prefix--->返回名称空间前缀(只读)
preserveWhiteSpace--->指定是否保留空白(可读写)
previousSibling--->返回此节点的前一个兄弟节点(只读)
Text--->返回此节点及其后代的文本内容(可读写)
url--->返回最近载入的XML文档的URL(只读)
Xml--->返回节点及其后代的XML表示(只读)

方法:
appendChild--->为当前节点添加一个新的子节点,放在最后的子节点后
cloneNode--->返回当前节点的拷贝
createAttribute--->创建新的属性
createCDATASection--->创建包括给定数据的CDATA段
createComment--->创建一个注释节点
createDocumentFragment--->创建DocumentFragment对象
createElement--->创建一个元素节点
createEntityReference--->创建EntityReference对象
createNode--->创建给定类型,名字和命名空间的节点
createPorcessingInstruction--->创建操作指令节点
createTextNode--->创建包括给定数据的文本节点
getElementsByTagName--->返回指定名字的元素集合
hasChildNodes--->返回当前节点是否有子节点
insertBefore--->在指定节点前插入子节点
Load--->导入指定位置的XML文档
loadXML--->导入指定字符串的XML文档
removeChild--->从子结点列表中删除指定的子节点
replaceChild--->从子节点列表中替换指定的子节点
Save--->把XML文件存到指定节点
selectNodes--->对节点进行指定的匹配,并返回匹配节点列表
selectSingleNode--->对节点进行指定的匹配,并返回第一个匹配节点
transformNode--->使用指定的样式表对节点及其后代进行转换
transformNodeToObject--->使用指定的样式表将节点及其后代转换
实例获取标签属性.值:

Me.xml

View Code

 1 <?xml version="1.0" encoding="utf-8"?>  2 <phplamp>  3 <post>  4 <title id="1">PHP XML处理介绍一</title>  5 <details>详细内容一</details>  6 </post>  7 <post>  8 <title id="2">PHP XML处理介绍二</title>  9 <details>详细内容二</details> 10 </post> 11 <post> 12 <title id="3">PHP XML处理介绍三</title> 13 <details>详细内容三</details> 14 </post> 15 </phplamp>

 

 
 

View Code

 1 // 首先要建一个DOMDocument对象  2 $xml = new DOMDocument();  3 // 加载Xml文件  4 $xml->load("me.xml");  5 // 获取所有的post标签  6 $postDom = $xml->getElementsByTagName("post");  7 // 循环遍历post标签  8 foreach($postDom as $post){  9 // 获取Title标签Node 10 $title = $post->getElementsByTagName("title"); 11 /** 12 * 要获取Title标签的Id属性要分两部走 13 * 1. 获取title中所有属性的列表也就是$title->item(0)->attributes 14 * 2. 获取title中id的属性,因为其在第一位所以用item(0) 15 * 16 * 小提示: 17 * 若取属性的值可以用item(*)->nodeValue 18 * 若取属性的标签可以用item(*)->nodeName 19 * 若取属性的类型可以用item(*)->nodeType 20 */ 21 echo "Id: " . $title->item(0)->attributes->item(0)->nodeValue . "<br />"; 22 echo "Title: " . $title->item(0)->nodeValue . "<br />"; 23 echo "Details: " . $post->getElementsByTagName("details")->item(0)->nodeValue . "<br /><br />"; 24 } 

 

 



下面是js读取xml文件的代码 ajax+xml留言

Javascript代码

 1 var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); 2 xmldoc.async = false; 3 var 旧发言 = ""; 4 function getxml(){ 5     try { 6         xmldoc.load("word.xml"); 7         if (xmldoc.documentElement != null) { 8             var 新发言 = xmldoc.xml; 9             if (新发言 != 旧发言) {10                 旧发言 = 新发言;11                 var allwords = xmldoc.selectNodes("list/message");12                 新发言 = "";13                 for (var i = 0; i < allwords.length; i++) {14                     var 数组 = allwords[i].text.split("@");15                     var 作者 = 数组[0];16                     if (作者 == 用户名.value) {17                         作者 = "<font color='Turquoise'>" + 作者 + "</font>";18                     }19                     else {20                         作者 = "<font color='Silver'>" + 作者 + "</font>";21                     }22                     var 发言 = "<font color='Gold'>" + 数组[1] + "</font>";23                     新发言 += "<div>" + 作者 + " " + 发言 + "</div>" +24                     "<div style='text-align:right;font-size:9px;'>" +25                     allwords[i].getAttribute("author").substring(0, allwords[i].getAttribute("author").lastIndexOf(".") + 1) +26                     "* " +27                     allwords[i].getAttribute("time") +28                     "</div>";29                 }30                 words.innerHTML = 新发言;31                 words.scrollTop = words.scrollHeight;32             }33         }34     } 35     catch (e) {36     }37 }38 39 var 读取中 = false;40 function getdata(){41     if (!读取中) {42         读取中 = true;43         getxml();44         读取中 = false;45     }46     setTimeout("getdata()", 3000);47 }48 49 getdata();50 submit.onclick = function(){51     submit.disabled = true;52     if (word.value != "") {53         var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");54         xmlHttp.open("GET", "save.php?name=" + encodeURIComponent(用户名.value) + "&word=" + encodeURIComponent(word.value), true);55         xmlHttp.setRequestHeader("Content-Type", "text/html; charset=utf-8");56         xmlHttp.send(null);57         word.value = "";58         if (!读取中) {59             读取中 = true;60             getxml();61             读取中 = false;62         }63     }64     setTimeout("submit.disabled = false;", 3000);65     word.focus();66 }

 

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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Framework Security Features: Protecting against vulnerabilities.Framework Security Features: Protecting against vulnerabilities.Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles

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
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool