PHP & XML
XML is a markup language that is quite similar to HTML. An XML document is plain text and contains tags limited by. There are two major differences between XML and HTML.
XML is a HTML-like markup language. An XML document is plain text and contains tags limited by . There are two major differences between XML and HTML.
- 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.
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
Maybe you are interested
The Hubble Telescope discovered a pair of small dwarf galaxies containing many interesting features Hubble telescope discovers more than 1,000 new asteroids The Hubble Telescope finds a galaxy glowing strangely from behind a dark nebula Hubble Telescope extremely rare photo of the moment of the Supernova explosion The world's most expensive telescope captures stunning views of the famous supernova remnant Lonely star glitters in the background of an amorphous galaxy in the eyes of the Hubble telescope