Monday 12 March 2012

XML

  • XML stands for Extensible Markup Language.
  • XML is a document-processing standard that is recommended by the W3C (World Wide Web Consortium).
  • The creator of XML is Jon Bosak of Sun Microsystems.
  • XML is a markup language much like HTML.
  • XML is designed to describe the data.
  • XML is used to carry the data and not to display the data.
  • XML does not have predefined tags like HTML.
  • XML allows the users to define their own tags.
  • XML is a Meta (Meta means: - A language used to describe another language) language, which allows the users to create and format their own document markups.

Ø       Rules for XML tags

1)     All XML tags which are opened must have a closing tag.
2)     XML tags are case sensitive. EX: <BOOK> and <book> are different tags.
3)     XML elements must be properly nested.
<p> <i> Hello </p> </i> // Improper
<p> <i> Hello </i> </p>  // Proper
4)     XML documents must have a root element.
5)     XML attributes must be quoted.
<booksinfo date=”01/01/2010”> // date is the attribute of booksinfo

Ø       XML Functions

XML Functions are used to convert XML to an object. This function can fetch XML file as an array object.

Function
Description
Simplexml_load_file()
Load a XML file and returns a SimpleXMLObject
from the XML document.
getName()
Returns the name of the XML document.
Children()
Returns the child of the specified node.
Attributes()
Returns the attributes of the elements.
addChild()
Adds a child element to the SimpleXML element.
addAttribute()
Adds a attribute to the SimpleXML element.
__construct
Creates a new SimpleXMLElement object
asXML()
Gets an XML string from a SimpleXML element
getDocNamespaces()
Gets the namespaces of an XML document
getNamespances()
Gets the namespaces from XML data
registerXPathNamespace()
Creates a namespace context for the next XPath query
Simplexml_import_dom()
Gets a SimpleXMLElement object from a DOM node
Simplexml_load_string()
Gets a SimpleXMLElement object from an XML string
xpath()
Runs an XPath query on XML data
                  

  
Ø       Difference between XML and HTML

XML
HTML
XML stands for Extensible Markup Language.
HTML stands for Hyper Text Markup Language.
XML is case sensitive.
HTML is not case sensitive.
In XML it is compulsory to close The tags which are opened.
HTML tags does not require to be closed.
XML was designed to describe data and
To focus on what data is.
HTML was designed to display data and to focus on how data looks.
XML is about describing information.
HTML is about displaying information.

Example: XMLfile

                   <?xml version="1.0"?>
<booksinfo>
                             <id>1</id>
                             <name>PHP</name>
                             <price>200</price>
</booksinfo>

PHP file

<?php
          $xml = simplexml_load_file("info.xml");
          echo $xml->getName()."<br/>";
          foreach($xml->children() as $child)
          {
                   echo $child->getName() . ": " . $child . "<br />";
    }
?>

            Output:

                   Id 1
                   Name php
                   Price 200


Posted By : Hemangi Zala

No comments:

Post a Comment