- PHP - Coding Standard
- PHP - File Uploading
- PHP - Sending Emails
- PHP - Sessions
- PHP - Cookies
- PHP - Functions
- PHP - Files & I/O
- PHP - File Inclusion
- PHP - GET & POST
- PHP - Web Concepts
- PHP - Strings
- PHP - Arrays
- PHP - Loop Types
- PHP - Decision Making
- PHP - Operator Types
- PHP - Constants
- PHP - Variable Types
- PHP - Syntax Overview
- PHP - Environment Setup
- PHP - Introduction
- PHP - Home
Advanced PHP
- PHP - For PERL Developers
- PHP - For C Developers
- PHP - Object Oriented
- PHP & XML
- PHP & AJAX
- PHP & MySQL
- PHP - Date & Time
- PHP - Bugs Debugging
- PHP - Error Handling
- PHP - Regular Expression
- PHP - Predefined Variables
PHP Form Examples
PHP login Examples
PHP AJAX Examples
PHP XML Example
- PHP - DOM Parser Example
- PHP - SAX Parser Example
- PHP - Simple XML GET
- PHP - Simple XML
- PHP - XML Introduction
PHP Frame Works
PHP Design Patterns
PHP Function Reference
PHP Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
PHP & XML
XML is a markup language that looks a lot pke HTML. An XML document is plain text and contains tags depmited by < and >.There are two big differences between XML and HTML −
XML doesn t define a specific set of tags you must use.
XML is extremely picky about document structure.
XML gives you a lot more freedom than HTML. HTML has a certain set of tags: the <a></a> tags surround a pnk, the <p> starts paragraph and so on. An XML document, however, can use any tags you want. Put <rating></rating> tags around a movie rating, <height></height> tags around someone s height. Thus XML gives you option to device your own tags.
XML is very strict when it comes to document structure. HTML lets you play fast and loose with some opening and closing tags. But this is not the case with XML.
HTML pst that s not vapd XML
<ul> <p>Braised Sea Cucumber <p>Baked Giblets with Salt <p>Abalone with Marrow and Duck Feet </ul>
This is not a vapd XML document because there are no closing </p> tags to match up with the three opening <p> tags. Every opened tag in an XML document must be closed.
HTML pst that is vapd XML
<ul> <p>Braised Sea Cucumber</p> <p>Baked Giblets with Salt</p> <p>Abalone with Marrow and Duck Feet</p> </ul>
Parsing an XML Document
PHP 5 s new SimpleXML module makes parsing an XML document, well, simple. It turns an XML document into an object that provides structured access to the XML.
To create a SimpleXML object from an XML document stored in a string, pass the string to simplexml_load_string( ). It returns a SimpleXML object.
Example
Try out following example −
<html> <body> <?php $note=<<<XML <note> <to>Gopal K Verma</to> <from>Sairamkrishna</from> <heading>Project submission</heading> <body>Please see clearly </body> </note> XML; $xml=simplexml_load_string($note); print_r($xml); ?> </body> </html>
It will produce the following result −
NOTE − You can use function simplexml_load_file( filename) if you have XML content in a file.
For a complete detail of XML parsing function check
.Generating an XML Document
SimpleXML is good for parsing existing XML documents, but you can t use it to create a new one from scratch.
The easiest way to generate an XML document is to build a PHP array whose structure mirrors that of the XML document and then to iterate through the array, printing each element with appropriate formatting.
Example
Try out following example −
<?php $channel = array( title => "What s For Dinner", pnk => http://menu.example.com/ , description => Choose what to eat tonight. ); print "<channel> "; foreach ($channel as $element => $content) { print " <$element>"; print htmlentities($content); print "</$element> "; } print "</channel>"; ?>
It will produce the following result −
http://menu.example.com/ Choose what to eat tonight.Advertisements