What is XML? XML (eXtensibleMarkupLanguage) is a subset of the standard General Markup Language SGML of the International Organization for Standardization. It consists of the following specifications: eXtensibleSytleLanguage (XSL) XML link language (XMLLinkingLanguage, including Xpath, Xlink, and Xpointer) XML namespace (XMLN
What is XML? XML (eXtensible Markup Language) is a subset of the standard General Markup Language SGML of the International Organization for Standardization. It consists of the following specifications: eXtensible Sytle Language (XSL) XML link Language (XML Linking Language, including Xpath, Xlink, and Xpointer) XML namespace (XML N
What is XML?
XML (eXtensible Markup Language) is a subset of the standard General Markup Language SGML of the International Organization for Standardization. It consists of the following specifications:
- EXtensible Sytle Language (XSL)
- XML Linking Language (including Xpath, Xlink, and Xpointer)
- XML Namespace)
There are many problems with XML processing in versions earlier than PHP5. For example, XML tools only have simple associations, and each tool cannot work together, the new XML extension in PHP5 has the following features:
- Ability to coordinate work as a whole
- Is a standardized XML library: libxml2
- Fully complies with W3C specifications
- More effective data processing
- Is an appropriate XML tool in your work
What is the difference and connection between HTML and XML?
HTML and XML are both subset of SGML, so they have great similarity. Below are the differences between XML and HTML:
- Scalability to define new tags. This makes sense for today's web
- Structure, used to represent data of any complexity. In a sense, it is a small relational database.
- Check to check the structure correctness of the data. This can be achieved through DTD constraints.
- Media Independence. Content is published in multiple formats. Web pages, wml displayed on mobile phones, and display on other media Terminals
- Vendor and platform neutral
- Data Representation and content separation (this is essentially different from html, but the current popular design concept of DIV + CSS is similar to this)
- XML elements are case sensitive
- Any element must have an end mark.
- XML has only one root element.
- Attribute must be enclosed in quotation marks
What does a complete XML look like?
Simpsons
FOX
8:00 PM
30
Law & Order
NBC
8:00 PM
60
A well-formed XML document must have the following characteristics:
- Each element has a start and end mark.
- The document has only one root element, and all other elements are its child elements.
- Correct empty element formatting
- Case-sensitive matching of tags
- Correct nesting
- Attribute values must be enclosed in quotation marks.
- The object must be declared before being referenced.
- Entities cannot point to themselves cyclically
Use DOM to generate XML
// Create a new document
$ Dom = new DOMDocument ('1. 0'); // create a root element
And add it to the document
$ Book = $ dom-> appendChild ($ dom-> createElement ('book'); // create a title sub-element, and add it to $ book $ title = $ book-> appendChild ($ dom-> createElement ('title ')); // set the text and cover attributes of the title element $ title-> appendChild ($ dom-> createTextNode ('php cookbook'); $ title-> setAttribute ('cover ', 'Soft'); // create and add the author element to $ book $ sklar = $ book-> appendChild ($ dom-> createElement ('author ')); // Add text to the author Node
$ Sklar-> appendChild ($ dom-> createTextNode ('sklar '); $ trachtenberg = $ book-> appendChild ($ dom-> createElement ('author ')); $ trachtenberg-> appendChild ($ dom-> createTextNode ('trachtenken'); // output a perfectly formatted XML document
$ Dom-> formatOutput = true; echo $ dom-> saveXML ();
The output content is as follows:
PHP Cookbook
Use PHP to parse existing XML files
There are three common methods to parse XML files.
- Use SimpleXML for simple files
- Use DOM extension for complex XML files
- XMLReader extension for large XML files
The XML sample file is as follows (address-book.xml ):
David
Sklar
New York
NY
sklar@php.net
Adam
Trachtenberg
San Francisco
CA
amt@php.net
SimpleXML:
$sx = simplexml_load_file('address-book.xml');foreach ($sx->person as $person) { $firstname_text_value = $person->firstname; $lastname_text_value = $person->lastname; print "$firstname_text_value $lastname_text_value\n";}
DOM extension:
$dom = new DOMDocument;$dom->load('address-book.xml');foreach ($dom->getElementsByTagname('person') as $person) { $firstname = $person->getElementsByTagname('firstname'); $firstname_text_value = $firstname->item(0)->firstChild->nodeValue; $lastname = $person->getElementsByTagname('lastname'); $lastname_text_value = $lastname->item(0)->firstChild->nodeValue; print "$firstname_text_value $lastname_text_value\n";}
XMLReader extension:
$reader = new XMLReader();$reader->open('card-catalog.xml');while ($reader->read()) { if ($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == 'author') { $reader->read(); print $reader->value . "\n"; }}
Use XPath to query information
XPath is available in SimpleXML and DOM extensions.
// SimpleXml example $ emails = $ s-> xpath ('/address-book/preson/email'); // DOM extension example $ xpath = new DOMXPath ($ dom ); $ email = $ xpath-> query ('/address-book/preson/email ');
Verify that the XML document is legal
In PHP, DOM Extensions support Verification Based on DTD, XML Schema, and RelaxNG, while SimpleXML only provides XML Schema verification.
XML content in UTF-8 format
If the data source is in another format and needs to be encoded in UTF-8 format, the following is an example of conversion through the iconv Library
$utf_8 = iconv('ISO-8859-1', 'UTF-8', $iso_8859_1);
Other references: PHP advanced development technology and examples Tsinghua University Press