当前位置:网站首页>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"); ?>边栏推荐
- MySQL index
- 【数据库】事务
- 09- [istio] istio service entry
- Configure the user to log in to the device through telnet -- AAA local authentication
- Luogup2756 pilot pairing scheme problem (maximum flow)
- Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (II)
- Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (III) [standard answer]
- token 过期后,如何自动续期?
- Xctf attack and defense world crypto advanced area
- Leader: who can use redis expired monitoring to close orders and get out of here!
猜你喜欢

STM32F103系列控制的OLED IIC 4针

Intelligent question - horse racing question

Feisheng: Based on the Chinese word breaker ik-2 ways to build custom hot word separators Showcase & pit arrangement Showtime

PC viewing WiFi password

MySQL index
![[MD editing required] welcome to the CSDN markdown editor](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[MD editing required] welcome to the CSDN markdown editor

leetcode763. Divide letter interval

Cisco vxlan configuration
![[GPU] basic operation of GPU (I)](/img/ce/0ca8c63525038fea64c40aabd17fc6.jpg)
[GPU] basic operation of GPU (I)

MySQL數據庫用戶管理
随机推荐
MySQL transaction
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
Beauty of Refactoring: when multithreaded batch processing task lifts the beam - Universal scaffold
10-【istio】istio Sidecar
Configure the user to log in to the device through telnet -- AAA local authentication
【数据库】事务
Promise knowledge points
聲網,站在物聯網的“土壤”裏
Use of OpenCL thread algebra library viennacl
Force deduction exercise -- deleting repeated items in ordered sequence 1.0
MySQL advanced SQL statement
Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand
UE4_ Editor UMG close window cannot destroy UMG immediately
Voting vault: a new primitive for defi and Governance
从零开发 stylelint规则(插件)
Prototype and prototype chain in JS
Sound network, standing in the "soil" of the Internet of things
Dao -- a beautiful new world?
网络基础知识
English grammar_ Adjective / adverb Level 3 - superlative