Home  >  Article  >  Backend Development  >  PHP XML operation class DOMDocument_PHP tutorial

PHP XML operation class DOMDocument_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:42:42715browse

DOMDocument related content.

Attributes:

Attributes stores the node’s attribute list (read-only)
childNodes stores the node’s child node list (read-only)
dataType returns this node The data type of
Definition is the definition of the node given in DTD or XML mode (read-only)
Doctype specifies the document type node (read-only)
documentElement returns the root element of the document (readable and writable)
firstChild returns the first child node of the current node (read-only)
Implementation returns the XMLDOMImplementation object
lastChild returns the last child node of the current node (read-only)
nextSibling returns the next sibling node of the current node ( Read only)
nodeName returns the name of the node (read only)
nodeType returns the type of the node (read only)
nodeTypedValue stores the node value (readable and writable)
nodeValue returns the text of the node (readable) Write)
ownerDocument returns the root document containing this node (read-only)
parentNode returns the parent node (read-only)
Parsed returns whether this node and its child nodes have been parsed (read-only)
Prefix Returns the namespace prefix (read-only)
preserveWhiteSpace Specifies whether to preserve whitespace (readable and writable)
previousSibling Returns the previous sibling node of this node (read-only)
Text Returns the text of this node and its descendants Content (readable and writable)
url Returns the URL of the recently loaded XML document (read-only)
Xml Returns the XML representation of the node and its descendants (read-only)



Method:

appendChild adds a new child node to the current node and places it after the last child node
cloneNode returns a copy of the current node
createAttribute creates a new attribute
createCDATASection creates a include Given a CDATA segment of data
createComment creates a comment node
createDocumentFragment creates a DocumentFragment object
createElement creates an element node
createEntityReference creates an EntityReference object
createNode creates a given type, name and namespace Node
createPorcessingInstruction creates an operation instruction node
createTextNode creates a text node including the given data
getElementsByTagName returns a collection of elements with the specified name
hasChildNodes returns whether the current node has child nodes
insertBefore before the specified node Insert child node
Load Import the XML document at the specified location
loadXML Import the XML document with the specified string
removeChild Remove the specified child node from the child node list
replaceChild Replace the specified child node from the child node list The child node
Save saves the XML file to the specified node
selectNodes performs the specified matching on the node and returns the matching node list
selectSingleNode performs the specified matching on the node and returns the first matching node
transformNode uses the specified style sheet to transform the node and its descendants
transformNodeToObject uses the specified style sheet to transform the node and its descendants

Instance gets the label attribute. Value:

Me. xml

Copy code The code is as follows:




Introduction to PHP XML processing one
Details one< ;/details>


Introduction to PHP XML processing 2
Details Content 2



Introduction to PHP XML processing 3
< details>Details three


Copy the code
The code is as follows:


// First create a DOMDocument object
$xml = new DOMDocument(); // Load the Xml file $xml->load("me.xml");
// Get all post tags
$postDom = $xml->getElementsByTagName("post");

// Loop through the post tags
foreach($postDom as $post){
// Get the Title tag Node
$title = $post->getElementsByTagName("title");

/**
* To get the Id attribute of the Title tag, there are two steps
* 1. Get the list of all attributes in the title, which is $title->item(0)->attributes
* 2. Get the id attribute in the title. Because it is in the first place, use item(0)
*
* Tips:
* If you want to get the value of the attribute, you can use item(*)->nodeValue
* If you want to get the label of the attribute, you can use item(*)->nodeName
* If you want to get the type of the attribute, you can use item(*)->nodeType
*/
echo "Id: " . $title->item(0)->attributes->item(0)->nodeValue . "< br />";
echo "Title: " . $title->item(0)->nodeValue . "
";
echo "Details: " . $post- >getElementsByTagName("details")->item(0)->nodeValue . "

";
}



The following is the code for js to read the xml file ajax+xml message
Copy the code The code is as follows:

var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async = false;
var old statement = "";
function getxml(){
try{
xmldoc .load("word.xml");
if(xmldoc.documentElement != null){
var new statement = xmldoc.xml;
if(new statement!= old statement){
Old message = New message;
var allwords = xmldoc.selectNodes("list/message");
New message = "";
for(var i = 0;i < allwords.length;i++ ){
var array = allwords[i].text.split("@");
var author = array[0];
if(author== username.value){
Author= ""+Author+"";
}
else{
Author= " "+author+"";
}
var statement = ""+array[1]+"";
New Post+= "
"+Author+" "+Post+"
"
+"
"
+allwords[i].getAttribute("author").substring(0,allwords[i].getAttribute("author").lastIndexOf(".")+1)
+"* "
+allwords[i].getAttribute("time")
+"
";
}
words.innerHTML = new statement;
words.scrollTop = words .scrollHeight;
}
}
}
catch(e){
}
}
var reading = false;
function getdata(){
if(!Reading){
Reading = true;
getxml();
Reading = false;
}
setTimeout("getdata()", 3000);
}
getdata();
submit.onclick = function(){
submit.disabled = true;
if(word.value != ""){
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET","save.php?name=" +encodeURIComponent(username.value)+ "&word="+encodeURIComponent(word .value), true);
xmlHttp.setRequestHeader("Content-Type","text/html; charset=utf-8");
xmlHttp.send(null);
word.value = "";
if(!Reading){
Reading = true;
getxml();
Reading = false;
}
}
setTimeout("submit.disabled = false;",3000);
word.focus();
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320909.htmlTechArticleDOMDocument related content. Attributes: Attributes The attribute list of the storage node (read-only) childNodes The list of child nodes of the storage node (Read-only) dataType Returns the data type of this node Definiti...
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