PHP Operation XML

Source: Internet
Author: User
Tags addchild

First, PHP DOM

1. Main categories:

DOMDocument: Document Class--$dom->new DOMDocument (' 1.0 ', ' gb2312 ')

Domnodelist: Node List class--$dom->getelementsbytagname (")

DomElement: Element Class--$dom->getelementsbytagname (")->item (0); $dom->documentelement (Root node)

Domnode: Node class

node additions and deletions change (To find the parent node first)

$dom->load (' Aa.xml ')

$dom->createelement (' name ' [, ' value ']);

$son = $dom->createcdatasection (");

$parent->appendchild ($son);

$parent->removechild ($son);--delete node

$parent->replacechild ($new, $old);--replace node

$dom->savexml (' Aa.xml ')

$dom->save (' Aa.xml ')

Property manipulation

$element->setattribute (' name ', ' value '); Add or modify property values

$element->getattribute (' name ');--Get the value of the property

$element->hasattribute (' name ');--to determine if there is a property

$element->removeattribute (' name ')--delete attribute

Second, SimpleXML

The returned XML variable is an instance of the SimpleXMLElement class//This object represents the root element in the XML $xml=simplexml_load_file (' Demo05.xml '); Echo $xml->person[1] ->name. ' <br> '; Echo $xml->person[1]->age. ' <br> ';

  

$xml =simplexml_load_file (' demo05.xml ');
$xml->children ();--represents all child nodes of the root node

To add a node:

$person = $xml->addchild (' person ');

$person->addchild (' name ', ' Tom ');

$person->addchild (' Age ', ' 29 ');

$xml->asxml (' demo05.xml ');

Add Property

$xml =simplexml_load_file (' demo05.xml ');//Add the id attribute to the 3rd person with a value of P103$xml->person[2]->addattribute (' id ', ' p103 '); $ Xml->asxml (' Demo05.xml ');

  

Third, XPATH

Ads

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.