Xml tutorial (II)-how to use php to parse xml

Source: Internet
Author: User
Xml can store and transmit data. to obtain the data, we need to parse the xml, which can be js parsing, html parsing, or php parsing.

Xml parsing

PHP DOM

SIMPLEXML

I. Why should I parse xml?

Xml can store and transmit data. to obtain the data, we need to parse the xml, which can be JavaScript parsing, html parsing, or php parsing.

II. Use of PHP dom

Dom: document object mode document object Model

  • DOMDocument: Document object
  • DOMNodeList: node list
  • DOMNode: Node
  • DOMElement: element

1. create a dom document object

  • DOMDocument ::__ construct ([string $ version [, string $ encoding])

String $ version: version

String $ encoding: encoding

2. load xml

  • Mixed DOMDocument: load (string $ filename)

String $ filename: xml document name

3. get nodes

  • DOMNodeList DOMDocument: getElementsByTagName (string name)

Get nodes by tag name

4. Domnodelist

  • Length: the Length of the node list (several nodes exist in this node list)
  • DOMNode DOMNodelist: item (int $ index) select a node; int $ index: index of the node

5. DOMNode

  • NodeValue node value

6. read attributes

  • Bool DOMElement: hasAttribute (string $ name)

Determines whether the current node has a certain attribute.

  • Bool DOMNode: hasAttributes (void)

Determines whether the current node has attributes.

  • String DOMElement: getAttribute (string $ name)

Get the attribute value of the current node

7. create a node

DOMElement DOMDocument: createElement (string $ name [, string $ value])

Create node element

String $ name: node name

String $ value: node value

8. add nodes

  • DOMNode: appendChild (DOMNode $ newnode)

Add a subnode

DOMNode $ newnode: new node

In dom operations, the addition, deletion, modification, and deletion operations must depend on the parent node.

9. Save

  • String DOMDocument: saveXML

Save to a string

  • Int DOMDocument: save (string $ filename)

Save to an object

String $ filename: file name

10. delete a node

  • DOMNode: removeChild (DOMNode $ oldnode)

Delete a node

DOMNode $ oldnode: the node to be deleted

11. update nodes

  • DOMNode: replaceChild (DOMNode $ newnode, DOMNode $ oldnode)

DOMNode $ newnode: new node

DOMNode $ oldnode: original node

12. add attributes

  • DOMAttr DOMElement: setAttribute (string $ name, string $ value)

String $ name: attribute name

String $ value: attribute value

13. modify attributes

DOMAttr DOMElement: setAttribute (string $ name, string $ value)

String $ name: attribute name

String $ value: attribute value

14. delete attributes

  • Bool DOMElement: removeAttribute (string $ name)

String $ name: name of the attribute to be deleted

15. get attributes

  • String DOMElement: getAttribute (string $ name)

String $ name: attribute name of the property value to be obtained

1. read xml

  • SimpleXMLElement simplexml_load_file (string $ filename)

Returns the corresponding dom object by reading the specified file.

  • SimpleXMLElement ::__ construct (string data)

Returns the corresponding dom object using an xml string.

II. obtain subnodes

  • Public SimpleXMLElement: children ()

Obtain the subnode list

3. add nodes

Public SimpleXMLElement: addChild (string $ name [, string $ value])

String $ name: node name

[, String $ value]: node value

4. add attributes

Public void SimpleXMLElement: addAttribute (string $ name [, string $ value])

String $ name: attribute name

[, String $ value]: attribute value

5. save xml

  • Public mixed SimpleXMLElement: asXML ([string $ filename])

String $ filename: specifies the xml file name.

XPATH

XPath is a language used to search for information in XML documents.

XPath can be used to traverse elements and attributes in XML documents.

In xpath, we can reference some or some node elements in this way.

/Root element/child element /......

The following is an example of xpath:

$ Xml = simplexml_load_file ('demo01. XML '); /*************************************** // the following statement returns all person $ person = $ xml-> xpath ("/persons/person "); // returns the name of 1st People, echo $ person [0]-> name; **************************************** ***///********************************** * ***** // the following statement returns the name of the owner/$ name = $ xml-> xpath ("/persons/person/name "); // cyclically traverse the name array and put the name of each person in the value/* foreach ($ name as $ value) {echo $ value.'
';} ***************************************/ // ******************************* // The following statement indicates that the returned age information of all students larger than 35 // $ person = $ xml-> xpath ("/persons/person [age> 35]"); // obtain the total number // $ count = count ($ person); // looping/* for ($ I = 0; $ I <$ count; $ I ++) {echo $ person [$ I]-> name; echo $ person [$ I]-> age.'
';} * // Query the information of a student named zhangsan $ person = $ xml-> xpath ("person [name = 'zhangsan']"); echo $ person [0]-> name; echo $ person [0]-> age.'
';

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.