How use xml with php explain with example?
XML is the acronym for Extensible Markup Language. XML is used to structure, store and transport data from one system to another. XML is similar to HTML. It uses opening and closing tags. Unlike HTML, XML allows users to define their own tags. In this tutorial, you will learn- DOM is the acronym for Document Object Model. It’s a cross platform and language neutral standard that defines how to access and manipulate data in;
DOM XML is used to access and manipulate XML documents. It views the XML document as a tree-structure. XML ParsersAn XML parser is a program that translates the XML document into an XML Document Object Model (DOM) Object. The XML DOM Object can then be manipulated using JavaScript, Python, and PHP etc. The keyword CDATA which is the acronym for (Unparsed) Character Data is used to ignore special characters such as “<,>” when parsing an XML document. Why use XML?
XML Document exampleLet’s suppose that you are developing an application that gets data from a web service in XML format. Below is the sample of how the XML document looks like.
HERE,
How to Read XML using PHPLet’s now write the code that will read the employees XML document and display the results in a web browser. Index.php Employees Listing'; $list = $xml->record; for ($i = 0; $i < count($list); $i++) { echo 'Man no: ' . $list[$i]->attributes()->man_no . ' HERE,
Testing our applicationAssuming you saved the file index.php in phptus/xml folder, browse to the URL http://localhost/phptuts/xml/index.php How to Create an XML document using PHPWe will now look at how to create an XML document using PHP. We will use the example above in the DOM tree diagram. The following code uses the PHP built in class DOMDocument to create an XML document. encoding = 'utf-8'; $dom->xmlVersion = '1.0'; $dom->formatOutput = true; $xml_file_name = 'movies_list.xml'; $root = $dom->createElement('Movies'); $movie_node = $dom->createElement('movie'); $attr_movie_id = new DOMAttr('movie_id', '5467'); $movie_node->setAttributeNode($attr_movie_id); $child_node_title = $dom->createElement('Title', 'The Campaign'); $movie_node->appendChild($child_node_title); $child_node_year = $dom->createElement('Year', 2012); $movie_node->appendChild($child_node_year); $child_node_genre = $dom->createElement('Genre', 'The Campaign'); $movie_node->appendChild($child_node_genre); $child_node_ratings = $dom->createElement('Ratings', 6.2); $movie_node->appendChild($child_node_ratings); $root->appendChild($movie_node); $dom->appendChild($root); $dom->save($xml_file_name); echo "$xml_file_name has been successfully created"; ?> HERE,
Testing our applicationAssuming you saved the file create_movies_list in phptuts/xml folder, browse to the URL http://localhost/phptuts/xml/create_movies_list.php Click on movies_list_xml link Summary
What is use of XML in PHP?The XML functions lets you parse, but not validate, XML documents. XML is a data format for standardized structured document exchange. More information on XML can be found in our XML Tutorial. This extension uses the Expat XML parser. Expat is an event-based parser, it views an XML document as a series of events.
What is XML and use of XML with example?What is XML? The Extensible Markup Language (XML) is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more. It was derived from an older standard format called SGML (ISO 8879), in order to be more suitable for Web use.
HOW include XML file in PHP?$dom2 = new DOMDocument; $dom2->load('existingFile. xml'); $dom2->documentElement->appendChild($dom2->importNode($fragment, true));
What is XML explain with example?XML stands for Extensible Markup Language. It is a text-based markup language derived from Standard Generalized Markup Language (SGML). XML tags identify the data and are used to store and organize the data, rather than specifying how to display it like HTML tags, which are used to display the data.
|