Table of Contents
This guide covers PHP && XML with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
- XML does not define a specific set of tags.
- XML really "chooses" the document structure.
XML gives you more privileges than HTML. HTML has a specific set of tags: tags surrounding a link, tags
Start a paragraph. However, an XML document can use any tag you want. Place tags around a movie rating, tags around someone's height. Therefore, XML gives you the option to structure your own tags.
XML is quite strict when it comes to document structure. HTML allows you to work quickly and fairly softly with open and closed tags. But XML is not like that.
The following HTML list is considered invalid in XML:
HTML là ngôn ngữ đánh dấu văn bản XML cũng là ngôn ngữ đánh dấu văn bản JavacScript không phải ngôn ngữ đánh dấu văn bản
This is an invalid XML document, because there are no closing tags corresponding to 3 open tags
- . Each opening tag in an XML document must have a closing tag.
Valid HTML List in XML must be as follows:
- HTML is a text markup language
- XML is also a text markup language
- JavacScript is not a text markup language
Parsing an XML Document
The New SimpleXML Module of PHP 5 helps to parse an XML document in a simple and good way. It converts an XML document into an object that provides structured access to XML.
To create a SimpleXML object from an XML document stored in a string, pass this string to the Simplexml_load_string () Function. It returns an XML object.
For example:
You try the example:
php $note =<<< XML QTM to > Bạn from > Hẹn đi ăn heading > Cuối tuần đi nhậu nhé! body > note > XML ; $xml = simplexml_load_string ( $note ); print_r ( $xml ); ?>
Save the program in a file named Test. PHP in Htdocs , then open the browser and type the address HTTP: /// localhost: 8080 / test. PHP To see the result.
Note - You can use Simplexml_load_file (filename) Function if you have XML content in a file.
For a complete list of functions related to parse XML documents, you access: Function synthesis in PHP.
How to Create an XML Document
SimpleXML is good to parse existing XML documents, but you can't use it to create a new document.
The easiest way to create an XML document is to build an array in PHP that has a structure that reflects what the XML document is and then traverses the array, printing each element in the appropriate format.
For example
You try the example:
php $channel = array ( 'title' => "Thủ thuật công nghệ" , 'link' => 'https://quantrimang.com/' , 'description' => 'Những thủ thuật công nghệ mới, hay nhất.' ); print "n" ; foreach ( $channel as $element => $content ) { print " <$element>" ; print htmlentities ( $content ); print "n" ; } print "" ; ?> Save the program in a file named Test. PHP in Htdocs , then open the browser and type the address HTTP: /// localhost: 8080 / test. PHP To see the result.
Follow tutorialspoint
Last lesson: PHP & AJAX
Next lesson: Object-oriented programming in PHP
Frequently Asked Questions
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Was this article helpful?
Your feedback helps us improve.
Reader Comments 0
Sign in with email or Google to join the discussion.