当前位置:网站首页>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"); ?>边栏推荐
- [exercise] basic practice letter graph of Blue Bridge Cup
- 雲服務器部署 Web 項目
- 【学习强化学习】总目录
- Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
- Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
- Here comes the nearest chance to Ali
- What do you think of the deleted chat records? How to restore the deleted chat records on wechat?
- VLAN access mode
- 1380. lucky numbers in matrices
- 重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
猜你喜欢

STM32F103系列控制的OLED IIC 4针

CompletionService使用及原理(源码分析)

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

MySQL存储系统

MySQL高级SQL语句

UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)

接口中方法详解

Using lazy < t > in C # to realize singleton mode in WPF

UE4_ Editor UMG close window cannot destroy UMG immediately

Here comes the nearest chance to Ali
随机推荐
SparseArray
Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
InputStream to inputstreamsource
Ultra simple STM32 RTC alarm clock configuration
STM32F103系列控制的OLED IIC 4针
MySQL advanced (Advanced SQL statement)
STM32F103 series controlled OLED IIC 4-pin
Codeforces Round #390 (Div. 2) D. Fedor and coupons
旋转标注工具roLabelImg
Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"
如何制作CSR(Certificate Signing Request)文件?
What kind of answer has Inspur given in the big AI model landing test?
General contents of learning reinforcement learning
Shenzhou ares tx6 boot logo modification tutorial
Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (II)
拼多多店铺搜索相关问题,为什么新品上架搜索不到
数据读写:Unity中基于C#脚本实现数据读写功能
Promise knowledge points
[road of system analyst] collection of wrong topics in Project Management Chapter
MySQL事物