PHPXML operation class DOMDocument

Source: Internet
Author: User
PHPXML operation class DOMDocument has to write a. XML operation, which has never been used. below is the XML operation Class collected by myself.

DOMDocument-related content.

Attribute:
Attributes ---> attribute list of storage nodes (read-only)
ChildNodes ---> List of child nodes of the storage node (read-only)
DataType ---> return the data type of this node
Definition ---> Definition of nodes in DTD or XML mode (read-only)
Doctype ---> specifies the file type node (read-only)
DocumentElement ---> return the root element (read/write) of the document)
FirstChild ---> returns the first child node of the current node (read-only)
Implementation ---> return the XMLDOMImplementation object
LastChild ---> return the last child node of the current node (read-only)
NextSibling ---> returns the next sibling node of the current node (read-only)
NodeName ---> return the node name (read-only)
NodeType ---> return node type (read-only)
NodeTypedValue ---> storage node value (read/write)
NodeValue ---> return the node text (read/write)
OwnerDocument ---> return the root document containing this node (read-only)
ParentNode ---> return parent node (read-only)
Parsed ---> return whether the node and its subnodes have been Parsed (read-only)
Prefix ---> return namespace Prefix (read-only)
PreserveWhiteSpace ---> specifies whether to retain white space (read/write)
Previussibling ---> returns the previous sibling node of this node (read-only)
Text ---> return the Text content of this node and its descendants (read/write)
Url ---> return the URL of the recently loaded XML document (read-only)
Xml ---> XML representation of returned nodes and their descendants (read-only)

Method:
AppendChild ---> add a new subnode to the current node and put it after the last subnode
CloneNode ---> returns the copy of the current node.
CreateAttribute ---> create a new attribute
CreateCDATASection ---> create CDATA segments that contain the given data
CreateComment ---> Create a comment node
CreateDocumentFragment ---> Create a DocumentFragment object
CreateElement ---> create an element node
CreateEntityReference ---> Create an EntityReference object
CreateNode ---> Create a node of the given type, name, and namespace
CreatePorcessingInstruction ---> Create an operation command node
CreateTextNode ---> create a text node that includes the given data
GetElementsByTagName ---> returns the set of elements with the specified name.
HasChildNodes ---> returns whether the current node has subnodes.
InsertBefore ---> Insert a subnode before a specified node
Load ---> import the XML document at the specified position
LoadXML ---> import the XML document of the specified string
RemoveChild ---> delete a specified subnode from the subnode list
ReplaceChild ---> replace the specified subnode from the subnode list
Save ---> Save the XML file to the specified node
SelectNodes ---> specify a match for a node and return a list of matched nodes.
SelectSingleNode ---> specify a match for the node and return the first matching node.
TransformNode ---> use the specified style sheet to convert the node and its descendants
TransformNodeToObject ---> use the specified style sheet to convert nodes and their descendants
The instance obtains the tag attributes. value:

Me. xml

View Code

1
 2
 
  
3
  
   
4
   Php xml processing 15
   
    
Details 1
   6
  7
  
   
8
   Php xml processing 29
   
    
Details 2
   10
  11
  
   
12
   Php xml processing 313
   
    
Details 3
   14
  15
 


View Code

1 // Create a DOMDocument object 2 $ xml = new DOMDocument (); 3 // load the Xml file 4 $ xml-> load ("me. xml "); 5 // get all the post labels 6 $ postDom = $ xml-> getElementsByTagName (" post "); 7 // cyclically traverse the post label 8 foreach ($ postDom as $ post) {9 // obtain the Title label Node 10 $ title = $ post-> getElementsByTagName ("title "); 11/** 12 * The Id attribute of the Title tag to be obtained must be divided into two parts: 13*1. obtain the list of all attributes in the title, that is, $ title-> item (0)-> attributes 14*2. obtain the attribute of the id in the title, because it is in the first place, so use item (0) 15*16 * Tips: 17 * if you get the attribute value, you can use item (*) -> nodeValue 18 * Use item (*)-> nodeName 19 * If the tag to take the attribute can use item (*) -> nodeType 20 */21 echo "Id :". $ title-> item (0)-> attributes-> item (0)-> nodeValue."
"; 22 echo" Title: ". $ title-> item (0)-> nodeValue ."
"; 23 echo" Details: ". $ post-> getElementsByTagName (" details ")-> item (0)-> nodeValue ."

"; 24}



The following is the code for js to read xml files: ajax + xml message

Javascript code

1 var xmldoc = new ActiveXObject ("Microsoft. XMLDOM "); 2 xmldoc. async = false; 3 var old statement = ""; 4 function getxml () {5 try {6 xmldoc. load ("word. xml "); 7 if (xmldoc.doc umentElement! = Null) {8 var new statement = xmldoc. xml; 9 if (new statement! = Old statement) {10 old statement = new statement; 11 var allwords = xmldoc. selectNodes ("list/message"); 12 new Speech = ""; 13 for (var I = 0; I <allwords. length; I ++) {14 var array = allwords [I]. text. split ("@"); 15 var author = array [0]; 16 if (author = Username. value) {17 author = "" + author + ""; 18} 19 else {20 author = "" + author + ""; 21} 22 var statement = "" + array [1] + ""; 23 new speech + ="

"+ Author +" "+ Speaker +"

"+ 24"

"+ 25 allwords [I]. getAttribute ("author "). substring (0, allwords [I]. getAttribute ("author "). lastIndexOf (". ") + 1) + 26" * "+ 27 allwords [I]. getAttribute ("time") + 28"

"; 29} 30 words. innerHTML = new speech; 31 words. scrollTop = words. scrollHeight; 32} 33} 34} 35 catch (e) {36} 37} 38 39 var reading = false; 40 function getdata () {41 if (! Reading) {42 Reading = true; 43 getxml (); 44 reading = false; 45} 46 setTimeout ("getdata ()", 3000 ); 47} 48 49 getdata (); 50 submit. onclick = function () {51 submit. disabled = true; 52 if (word. value! = "") {53 var xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP"); 54 xmlHttp. open ("GET", "save. php? Name = "+ encodeURIComponent (user name. value) + "& word =" + encodeURIComponent (word. value), true); 55 xmlHttp. setRequestHeader ("Content-Type", "text/html; charset = utf-8"); 56 xmlHttp. send (null); 57 word. value = ""; 58 if (! Reading) {59 reading = true; 60 getxml (); 61 reading = false; 62} 63} 64 setTimeout ("submit. disabled = false; ", 3000); 65 word. focus (); 66}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.