DOMDocument and SimpleXML in PHP Create and parse XML programs

Source: Internet
Author: User
Tags php book xml parser xpath

Example:

DOM XML parser functions are part of the core of PHP. You can use these functions without installation.
XML file
The following XML file will be used in our example:

The code is as follows Copy Code
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<body>don ' t forget the meeting!</body>
</note>

Loading and exporting XML
We need to initialize the XML parser, load the XML, and output it:

Example

The code is as follows Copy Code

<?php
$xmlDoc = new DOMDocument ();
$xmlDoc->load ("Note.xml");

Print $xmlDoc->savexml ();
?>

The output of the above code:

George John Reminder Don t forget the meeting! if you view the source code in a browser window, you will see the following HTML:

The code is as follows Copy Code
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<body>don ' t forget the meeting!</body>
</note> on

Example creates a domdocument-object and loads the XML from "Note.xml" into this Document object.

The SaveXML () function puts the internal XML document into a string so that we can output it.

Example

The code is as follows Copy Code

<?php
Create a new document
$dom = new DOMDocument (' 1.0 ');

Create the root element book and add it to the document
$book = $dom->appendchild ($dom->createelement (' book '));

Create a TITLE element and add it to the $book
$title = $book->appendchild ($dom->createelement (' title '));
$title->appendchild ($dom->createtextnode (' Php book '));
Cover the Add property for the title element, the property value soft
$title->setattribute (' cover ', ' soft ');

Create the author element and add it to the $book
$kaifabu = $book->appendchild ($dom->createelement (' author '));
Add text to the author element
$kaifabu->appendchild ($dom->createtextnode (' Kaifabu '));

Create the author element and add it to the $book
$kaifabu = $book->appendchild ($dom->createelement (' author '));
Add text to the author element
$kaifabu->appendchild ($dom->createtextnode (' Younker '));

Output formatted DOM document as XML
$dom->formatoutput = true;

Generating books.xml files
$dom->save (' books.xml ');
This step displays the generated XML as a string
Echo htmlentities ($dom->savexml ());
?>

The output results are as follows:

The code is as follows Copy Code


<?xml version= "1.0"?>
<book>
<title cover= "soft" >php book</title>
<author>kaifabu</author>
<author>Younker</author>
</book>


Note: If the above generation XML is changed to $dom->save (' books.xml '), a books.xml file is generated directly

Here we use SimpleXML to parse XML files

The SimpleXML function allows you to convert XML to an object.

You can work with this object, just as you would any other object, with a normal property selector or an array iterator.

Some of these functions require the latest version of PHP.
Installation
The SimpleXML function is part of the core of PHP. These functions can be used without installation.

The code is as follows Copy Code

<?php
Parsing an XML document using the SimpleXML extension resolves an object that can get its element content through an object
$SX = simplexml_load_file (' books.xml ');
Get the contents of the title tag
$title = $sx->title;
Because there are two author elements, the content of each author element is retrieved by looping
$authors = ';
foreach ($sx->author as $author)
{
$authors. = ' & '. $author;
}
$authors = substr ($authors, 1);
Echo $title. ':' . $authors;
?>

The output results are as follows:
PHP Book:kaifabu&younker

PHP: Indicates the earliest version of PHP that supports this function.

SimpleXML function

__construct () to create a new SimpleXMLElement object. 5
AddAttribute () adds an attribute to the SimpleXML element. 5
AddChild () adds a child element to the SimpleXML element. 5
Asxml () gets the XML string from the SimpleXML element. 5
Attributes () Gets the properties of the SimpleXML element. 5
Children () Gets the child of the specified node. 5
Getdocnamespaces () Gets the namespace of the XML document. 5
GetName () Gets the name of the SimpleXML element. 5
GetNamespaces () Gets the namespace from the XML data. 5
Registerxpathnamespace () creates a namespace context for the next XPath query. 5
Simplexml_import_dom () Gets the SimpleXMLElement object from the DOM node. 5
Simplexml_load_file () Gets the SimpleXMLElement object from the XML document. 5
Simplexml_load_string () Gets the SimpleXMLElement object from an XML string. 5
XPath () runs an XPath query on the XML data. 5

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.