Knowledge Point records for PHP operations XML (DOMDocument)

Source: Internet
Author: User
Tags readable

1, XML file content

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Utf-8"?>
<phoneBook>
<person>
<name> Wu Ben </name>
<tel>13222053785</tel>
</person>
<person>
<name> Wu Ben 2</name>
<tel>15151981159</tel>
</person>
<person>
<name>test</name>
<tel>15077887624</tel>
</person>
<person>
<name> Satan Hair satan hair </name>
<tel>132423142134</tel>
</person>
<person>
<name> Aston Flight </name>
<tel>1234123421342134</tel>
</person>
</phoneBook>


2, generate a new XML file

The code is as follows Copy Code

$doc =new DOMDocument ("1.0", "Utf-8");  #声明文档类型
$doc->formatoutput=true;               #设置可以输出操作
#声明根节点, it is best to have an XML file with a node
$root = $doc-> CreateElement ("Phonebook"); #创建节点对象实体
$doc->appendchild ($root);             #把节点添加进来
  
$person = $doc->createelement ("person");  #创建节点对象实体   
$root->appendchild ($person);    #把节点添加到root节点的子节点
  
$ Contactname= $doc->createelement ("name");
$person->appendchild ($contactName);
  
$contactNum = $doc->createelement ("tel");
$person->appendchild ($contactNum);
  
$contactName->appendchild ($doc->createtextnode ($name));
$contactNum->appendchild ($doc->createtextnode ($tel));
  
$doc->save ($filename); #保存路径


3, XML File add node

The code is as follows Copy Code


$doc = new DOMDocument (); #声明文档类型
$doc-> Load ($filename);
$root = $doc->getelementsbytagname ("Phonebook")->item (0);

$person = $doc->createelement ("person");

$contactName = $doc->createelement ("name");
$contactName->nodevalue = $name;

$contactNum = $doc->createelement ("tel");
$contactNum->nodevalue = $tel;

$person-> appendchild ($contactName);
$person-> appendchild ($contactNum);
$root-> appendchild ($person);
$doc->save ($filename); #保存路径

4, traverse the XML file

The code is as follows Copy Code

$xmldoc = new DOMDocument ();
$xmldoc-> Load ("Contacts.xml");
$root = $xmldoc->getelementsbytagname ("person");

foreach ($root as $v) {
echo $v->getelementsbytagname ("name")->item (0)->nodevalue. " <br/> ";
}

Attach DOMDocument related content.

Property:

Attributes Storage node's list of properties (read-only)
ChildNodes the list of child nodes of the storage node (read-only)
DataType returns the data type of this node
definition is defined as a node given in a DTD or XML schema ( Read-only)
Doctype Specify document type node (read only)
DocumentElement return the root element of the document (readable and writable)
FirstChild returns the first child node of the current node (read-only)
implementation returns X Mldomimplementation 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 return node type (read only)
nodeTypedValue storage node value (readable and writable)
NodeValue return node text (readable)
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 resolved (read-only)
Prefix return namespace prefix (read-only)
PreserveWhitespace Specify whether to leave blank (readable and writable) br> PreviousSibling Returns the previous sibling node (read-only)
text returns the text content (readable)
URL for this node and its descendants returns the URL of the most recently loaded XML document (read-only)
XML return node and its descendants XML representation (read-only)

Method:
AppendChild adds a new child node for the current node, after the last child node
CloneNode returns the copy of the current node
CreateAttribute creates a new property
Createcdatasec tion Create a CDATA segment that includes a given data
Createcomment create a comment node
createdocumentfragment create DocumentFragment object
createelement Create an element node
Createentityreference Create EntityReference object
CreateNode Create a node of the given type, name, and namespace
Createporcessinginstruction Create an action instruction node
createTextNode Create a text node that includes a given data
getElementsByTagName the collection of elements that returns the specified name
HasChildNodes returns whether the current node has child nodes
InsertBefore Insert child nodes before the specified node
Load Import XML document at the specified location
Loadxml Import XML document for the specified string
Removechil D deletes the specified child node from the list of child nodes
ReplaceChild replaces the specified child node from the child node list
Save saves the XML file to the specified node
SelectNodes matches the node, and returns a list of matching nodes
Sele Ctsinglenode the specified match to the node and returns the first matching node
Transformnode to convert the node and its descendants using the specified style sheet
transformNodeToObject Converts a node and its descendants to the
instance to get the label properties using the specified style sheet. Value:

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.