PHPXML operation class DOMDocument

Source: Internet
Author: User
I have to write a. XML file myself, but I have never used it. below is the content related to the XML operation class DOMDocument.

Attribute:

Attribute list of the Attributes storage node (read-only)
List of child nodes of the childNodes storage node (read-only)
DataType returns the data type of this node
Definition defines nodes in DTD or XML mode (read-only)
Doctype specifies the file type node (read-only)
DocumentElement returns the root element of the document (read/write)
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: return the node name (read-only)
NodeType return node type (read-only)
NodeTypedValue stores node values (read/write)
NodeValue returns the node text (read/write)
OwnerDocument returns the root document containing this node (read-only)
ParentNode returns the parent node (read-only)
Parsed: returns whether the node and its subnodes have been Parsed (read-only)
Prefix returns the 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 returns the Text content of the node and its descendants (read/write)
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 subnode to the current node, which is placed after the last subnode
CloneNode returns the copy of the current node
CreateAttribute
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 creates nodes of the given type, name, and namespace.
CreatePorcessingInstruction
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 inserts 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 deletes a specified subnode from the subnode list.
ReplaceChild replaces the specified child node from the child node list
Save the XML file to the specified node
SelectNodes matches the specified node and returns the list of matched nodes.
SelectSingleNode matches the specified node and returns the first matching node.
TransformNode converts nodes and their descendants using the specified style table.
TransformNodeToObject converts nodes and their descendants using the specified style table

The instance obtains the tag attributes. value:

Me. xml

  
  
   
   Php xml processing 1 
   
    
Details 1
    
   
   
   Php xml processing 2 
   
    
Details 2
    
   
   
   Php xml processing 3 
   
    
Details 3
    
   
 
// Create a DOMDocument object $ xml = new DOMDocument (); // load the Xml file $ xml-> load ("me. xml "); // get all the post tags $ postDom = $ xml-> getElementsByTagName (" post "); // cyclically traverse the post tag foreach ($ postDom as $ post) {// obtain the Title tag Node $ title = $ post-> getElementsByTagName ("title");/*** the Id attribute of the Title tag must be divided into two parts: * 1. obtain the list of all attributes in the title, that is, $ title-> item (0)-> attributes * 2. obtain the attribute of the id in the title, because it is in the first place, so use item (0) ** Tips: * If you get the attribute value, you can use item (*) -> nodeValue * you can use item (*)-> nodeName * to obtain the attribute type. you can use item (*)-> nodeType */echo "Id: ". $ title-> item (0)-> attributes-> item (0)-> nodeValue."
"; Echo" Title: ". $ title-> item (0)-> nodeValue ."
"; Echo" Details: ". $ post-> getElementsByTagName (" details ")-> item (0)-> nodeValue ."

";}

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

Var xmldoc = new ActiveXObject ("Microsoft. XMLDOM "); xmldoc. async = false; var old statement = ""; function getxml () {try {xmldoc. load ("word. xml "); if(xmldoc.doc umentElement! = Null) {var new statement = xmldoc. xml; if (new statement! = Old statement) {old statement = new statement; var allwords = xmldoc. selectNodes ("list/message"); new statement = ""; 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 statement + ="

"+ Author +" "+ Speaker +"

"+"

"+ Allwords [I]. getAttribute ("author "). substring (0, allwords [I]. getAttribute ("author "). lastIndexOf (". ") + 1) +" * "+ allwords [I]. getAttribute ("time") +"

";} Words. innerHTML = new speech; 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 (user name. 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 ();}

For more articles about php xml operations-related DOMDocument, refer to the PHP Chinese website!

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.