当前位置:网站首页>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"); ?>边栏推荐
- Acwing winter vacation daily question 2022 punch in day 11
- The average salary of software testing in 2022 has been released. Have you been averaged?
- [deep learning] data segmentation
- 拼多多店铺搜索相关问题,为什么新品上架搜索不到
- Summation of basic exercise sequence of test questions
- Cisco VXLAN配置
- Idea of capturing mobile terminal variant combination
- 雲服務器部署 Web 項目
- hashlips_ art_ Engine-1.0.6 usage
- English语法_形容词/副词3级-最高级
猜你喜欢

About modifying dual system default startup item settings
![[ansible series] fundamentals 02 module debug](/img/99/c53be8e2a42c7cb5b4a9a7ef4ad98c.jpg)
[ansible series] fundamentals 02 module debug

MySQL index

86. separate linked list

电脑查看WiFi使用密码
![[road of system analyst] collection of wrong topics in Project Management Chapter](/img/8b/2908cd282f5e505efe5223b4c5ddbf.jpg)
[road of system analyst] collection of wrong topics in Project Management Chapter

At the age of 32, I fell into a middle-aged crisis and finally quit naked...

Transfer the token on the matic-erc20 network to the matic polygon

We strongly recommend more than a dozen necessary plug-ins for idea development

Intelligent question - horse racing question
随机推荐
Dynamic programming -- gliding wing of the strange thief Kidd
CompletableFuture从了解到精通,你想知道的这里都有
Using lazy < t > in C # to realize singleton mode in WPF
ES6解构赋值
2022年,谁在推动音视频产业的新拐点?
What do you think of the deleted chat records? How to restore the deleted chat records on wechat?
Voting vault: a new primitive for defi and Governance
MySQL数据库用户管理
电脑查看WiFi使用密码
Simple use of qlistview of QT (including source code + comments)
Cisco VXLAN配置
ECS deployment web project
网络基础知识
1380. lucky numbers in matrices
[deep learning] data segmentation
[ansible series] fundamentals 02 module debug
超简单 STM32 RTC闹钟 时钟配置
Luogup2756 pilot pairing scheme problem (maximum flow)
Official win 10 image download
86. separate linked list