当前位置:网站首页>DOM (document object model) document XML file object model
DOM (document object model) document XML file object model
2022-06-30 06:01:00 【bckBCK】
Dom(Document object model) file - object - Model , The core idea is to put a Xml File as an object model , Then operate through objects Xml file . Let's first summarize DOM Object's property method , The application is illustrated by cases !
DOMDocument attribute :
Attributes A list of properties of the storage node ( read-only )
childNodes List of child nodes of the storage node ( read-only )
dataType Returns the data type of this node
Definition With DTD or XML The definition of the node given by the pattern ( read-only )
Doctype Specify the document type node ( read-only )
documentElement Returns the root element of the document ( read-write )
firstChild Returns the first child of the current node ( read-only )
Implementation return XMLDOMImplementation object
lastChild Returns the last child of the current node ( read-only )
nextSibling Returns the next sibling of the current node ( read-only )
nodeName Returns the name of the node ( read-only )
nodeType Return the type of node ( read-only )
nodeTypedValue Storage node values ( read-write )
nodeValue Returns the text of the node ( read-write )
ownerDocument Returns the root document containing this node ( read-only )
parentNode Return to the parent node ( read-only )
Parsed Returns whether this node and its children have been resolved ( read-only )
Prefix Returns the namespace prefix ( read-only )
preserveWhiteSpace Specifies whether to leave blank ( read-write )
previousSibling Returns the previous sibling of this node ( read-only )
Text Returns the text content of this node and its descendants ( read-write )
url Returns the most recently loaded XML Document URL( read-only )
Xml Returns the number of nodes and their descendants XML Express ( read-only )
DOMDocument Method :
appendChild Add a new child node for the current node , After the last child node
cloneNode Returns a copy of the current node
createAttribute Create new properties
createCDATASection Create... That includes the given data CDATA paragraph
createComment Create an annotation node
createDocumentFragment establish DocumentFragment object
createElement_x Create an element node
createEntityReference establish EntityReference object
createNode Create a given type , Node of name and namespace
createPorcessingInstruction Create operation instruction node
createTextNode Create a text node that contains the given data
getElementsByTagName Returns the set of elements with the specified name
hasChildNodes Returns whether the current node has children
insertBefore Insert a child node before the specified node
Load Import... From the specified location XML file
loadXML Import the specified string XML file
removeChild Removes the specified child node from the list of child nodes
replaceChild Replaces the specified child node from the list of child nodes
Save hold XML Save the file to the specified node
selectNodes Make a specified match to the node , And return the list of matching nodes
selectSingleNode Make a specified match to the node , And return the first matching node
transformNode Use the specified style sheet to transform nodes and their descendants
1**、 Create a Xml file **
<?php // Instantiate a DomDocument object $dom = new DomDocument('1.0', 'UTF-8'); ##### $doc->preserveWhiteSpace=false;// Remove xml Space in document ; // If the file does not exist, create one if(!file_exists("xml_test.xml")){ header("Content-Type:text/plain"); $root= $dom->createElement("class"); $dom->appendChild($root); $dom->save("xml_test.xml"); } else{ $dom->load("xml_test.xml"); } print$dom->saveXML(); ?>2**、 Add elements **
<?php //addElementsxml.php $dom = new DOMDocument("1.0","UTF-8"); $dom->load("xml_test.xml"); $root_class =$dom->getElementsByTagName("class"); $i = 0; // for($i = 0;$i < 4; $i++){ $root_class_node =$root_class->item($i); $stu_node=$dom->createElement("student"); $stu_node->setAttribute("xingbie","man"); $stu_node_name=$dom->createElement("name","name".$i); // Set properties $stu_node_name->setAttribute("hel","23"); $stu_node_age=$dom->createElement("age","21"); $stu_node_introduce=$dom->createElement("introduce","1111"); $stu_node->appendChild($stu_node_name); $stu_node->appendChild($stu_node_age); $stu_node->appendChild($stu_node_introduce); $root_class_node->appendChild($stu_node); // } $dom->save("xml_test.xml"); print $dom->saveXML(); ?>3**、 Traversing elements **
<?php //getNode.php // Steps to parse a file //1 Create an object , Represents a document $dom = new DOMDocument("1.0","UTF-8"); //2 Make loading that Xml, Parse that file $dom->load("xml_test.xml"); //3 Get your new node $stu_nodes =$dom->getElementsByTagName("student"); for ($i=0;$i<$stu_nodes->length;$i++){ // Take out each student $stu_node= $stu_nodes->item($i); for($i=0;$i<$stu_node->childNodes->length;$i++){ echo$stu_node->childNodes->item($i)->nodeValue; echo ""; } } ?>
4**、 Remove elements **
<?php // Steps to parse a file //1 Create an object , Represents a document $dom = new DOMDocument("1.0","UTF-8"); //2 Make loading that Xml, Parse that file $dom->load("xml_test.xml"); //3 Get your new node $stu_nodes =$dom->getElementsByTagName("student"); $stu_node=$stu_nodes->item($stu_nodes->length -1); $stu_node->parentNode->removeChild($stu_node); $dom->save("xml_test.xml"); ?>5**、 Modify element properties **
<?php //1 Create an object , Represents a document $dom = new DOMDocument("1.0","UTF-8"); //2 Make loading that Xml, Parse that file $dom->load("xml_test.xml"); // Find this student $stus=$dom->getElementsByTagName("age")->item(0); $stus->nodeValue=100; $dom->save("xml_test.xml"); ?>边栏推荐
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Shenzhou ares tx6 boot logo modification tutorial
- Solitidy - fallback 回退函数 - 2种触发执行方式
- Set of XXL job principles
- Turn off automatic outlining in Visual Studio - turning off automatic outlining in Visual Studio
- 超简单 STM32 RTC闹钟 时钟配置
- [ansible series] fundamentals 02 module debug
- 声网,站在物联网的“土壤”里
- Solidity - 安全 - 重入攻击(Reentrancy)
- Mysql database user management
猜你喜欢
![[ansible series] fundamentals 02 module debug](/img/99/c53be8e2a42c7cb5b4a9a7ef4ad98c.jpg)
[ansible series] fundamentals 02 module debug

OSPF - authentication and load balancing summary (including configuration commands)

Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries

Create priority queue

Balanced binary tree judgment of Li Kou 110 -- classic problems

STM32F103系列控制的OLED IIC 4针

Solidity - Security - reentrancy attack

Summation of basic exercise sequence of test questions

Did you know that WPS can turn on eye protection mode?

Solidity - 安全 - 重入攻击(Reentrancy)
随机推荐
Solitidy - fallback 回退函数 - 2种触发执行方式
MySQL數據庫用戶管理
Solidity - Security - reentrancy attack
Prototype and prototype chain in JS
8 ways to earn passive income
Intelligent deodorizer embedded development
Do you know how to show the health code in only 2 steps
Who is promoting the new inflection point of audio and video industry in 2022?
About modifying dual system default startup item settings
hashlips_ art_ Engine-1.0.6 usage
Xijiao 21 autumn "motor and drive" online homework answer sheet (I) [standard answer]
电脑查看WiFi使用密码
Official win 10 image download
Create priority queue
General contents of learning reinforcement learning
STM32F103 series controlled OLED IIC 4-pin
MySQL存储系统
动态规划--怪盗基德的滑翔翼
雲服務器部署 Web 項目
How does WPS cancel automatic numbering? Four options