How to parse XML files with PHP _php tutorial

Source: Internet
Author: User
DomElement

DomElement domdocument::createelement (String $name [, String $value])

Creating a Node Element

String $name: node name

String $value: The value of the node

8. Adding nodes

domnode domnode::appendchild (Domnode $newnode)

Add child nodes

Domnode $newnode: New node

In DOM operations, adding and deleting operations must depend on the parent node

9. Save

string Domdocument::savexml

Save to a string

int Domdocument::save (string $filename)

Save to a file

String $filename: File name

10. Deleting nodes

domnode domnode::removechild (Domnode $oldnode)

Delete a node

Domnode $oldnode: The node to be deleted

11. Update nodes

domnode domnode::replacechild (Domnode $newnode, Domnode $oldnode)

Domnode $newnode: New node

Domnode $oldnode: Original node

12. Adding attributes

domattr Domelement::setattribute (String $name, String $value)

String $name: Property name

String $value: Property value

13. Modifying properties

Domattr Domelement::setattribute (String $name, String $value)

String $name: Property name

String $value: Property value

14. Delete Attributes

bool Domelement::removeattribute (string $name)

String $name: Name of the property to be removed

15. Get Properties

string Domelement::getattribute (String $name)

String $name: Property name of the property value to get

DOMDocument is also part of the DOM extension introduced after PHP5, which can be used to establish or parse html/xml, which currently only supports UTF-8 encoding.

code as follows

$xmlstring = << !--? XML version= ' 1.0 '?-->

login
imdonkey

XML;

$dom = new DOMDocument ();
$dom->loadxml ($xmlstring);
Print_r (GetArray ($dom->documentelement));

function GetArray ($node) {
$array = false;

if ($node->hasattributes ()) {
foreach ($node->attributes as $attr) {
$array [$attr->nodename] = $attr->nodevalue;
}
}

if ($node->haschildnodes ()) {
if ($node->childnodes->length = = 1) {
$array [$node->firstchild->nodename] = GetArray ($node->firstchild);
} else {
foreach ($node->childnodes as $childNode) {
if ($childNode->nodetype! = Xml_text_node) {
$array [$childNode->nodename][] = GetArray ($childNode);
}
}
}
} else {
return $node->nodevalue;
}
return $array;
}

SimpleXML

SimpleXML is an easy-to-use XML toolset provided after PHP5 that transforms XML into an easy-to-handle object or organizes XML data to be generated. However, it does not apply to XML that contains namespace, and is guaranteed to be complete in XML format (well-formed). It provides three methods: Simplexml_import_dom, Simplexml_load_file, simplexml_load_string, and the function name is very intuitive to illustrate the function. All three functions return the SimpleXMLElement object, and the read/add of the data is done through simplexmlelement

The code is as follows


$string = << <>


Login
Imdonkey

XML;

$xml = Simplexml_load_string ($string);
Print_r ($xml);
$login = $xml->login;//returned here is still a SimpleXMLElement object
Print_r ($login);
$login = (string) $xml->login;//when making data comparisons, be careful to cast
Print_r ($login);

The advantage of SimpleXML is that it is easy to develop, and the downside is that it will load the entire XML into memory before processing it, so it may be too weak to parse an XML document that is hyper-content. If you are reading small files, and the XML does not contain namespace, then SimpleXML is a good choice.

XMLReader

XmlReader is also an extension after PHP5 (after 5.1 default installation), it moves in the document flow like a cursor, and stops at each node and is flexible to operate. It provides fast and non-cached streaming access to inputs that can read streams or documents, allow users to extract data from them, and skip records that have no meaning to the application.

Using the Google Weather API to get information examples of the use of the next XmlReader, here is only a small number of functions, please refer to the official documentation.

The code is as follows

$xml _uri = ' HTTP://WWW.GOOGLE.COM/IG/API?WEATHER=BEIJING&HL=ZH-CN ';
$current = Array ();
$forecast = Array ();

$reader = new XMLReader ();
$reader->open ($xml _uri, ' GBK ');
while ($reader->read ()) {
Get Current data
if ($reader->name = = "Current_conditions" && $reader->nodetype = = xmlreader::element) {
while ($reader->read () && $reader->name! = "Current_conditions") {
$name = $reader->name;
$value = $reader->getattribute (' data ');
$current [$name] = $value;
}
}

Get Forecast data
if ($reader->name = = "Forecast_conditions" && $reader->nodetype = = xmlreader::element) {
$sub _forecast = Array ();
while ($reader->read () && $reader->name! = "Forecast_conditions") {
$name = $reader->name;
$value = $reader->getattribute (' data ');
$sub _forecast[$name] = $value;
}
$forecast [] = $sub _forecast;
}
}
$reader->close ();

XmlReader and XML parser are similar, all are side-read edge operations, the big difference is that the SAX model is a "push" model, where the parser pushes events to the application, notifies the application each time a new node is read, Applications that use XmlReader can extract nodes from the reader at will, and are more controllable.

Since XmlReader is based on libxml, some functions should refer to the documentation to see if they apply to your libxml version.

http://www.bkjia.com/PHPjc/372026.html www.bkjia.com true http://www.bkjia.com/PHPjc/372026.html techarticle domelement domelement domdocument::createelement (String $name [, String $value]) creates a node element string $name: section name string $v Alue: Node value 8, add node Domn ...

  • 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.