Your SlideShare is downloading. ×
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply



Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. XML
  • 2. What is XML?
    • XML stands for E X tensible M arkup L anguage
    • XML is a markup language much like HTML
    • XML was designed to describe data
    • XML tags are not predefined. You must define your own tags
    • XML uses a Document Type Definition (DTD) or an XML Schema to describe the data
    • XML with a DTD or XML Schema is designed to be self-descriptive
    • XML is a W3C Recommendation
  • 3. XML vs. HTML
    • XML is not a replacement for HTML. XML and HTML were designed with different goals:
    • 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.
    • HTML is about displaying information, while XML is about describing information.
  • 4. XML
    • XML was created to structure, store and to send information.
    • The following example is a note to Kamal from Jani, stored as XML:
    • <note>
    • <to>Paresh</to>
    • <from>Jani</from> <heading>Your CV</heading>
    • <body>You have been short listed. </body>
    • </note>
  • 5. How XML can be used?
    • XML can Separate Data from HTML.
      • With XML, your data is stored outside your HTML.
    • XML is Used to Exchange Data
      • With XML, data can be exchanged between incompatible systems.
    • XML Can be Used to Share Data
      • With XML, plain text files can be used to share data.
    • XML Can be Used to Store Data
      • With XML, plain text files can be used to store data.
  • 6. XML Syntax
    • Simple xml file:
    • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <note>
    • <to>Paresh</to>
    • <from>Jani</from>
    • <heading>Your Resume</heading>
    • <body>You have been short listed</body> </note>
  • 7. XML Syntax
    • The first line in the document - the XML declaration - defines the XML version and the character encoding used in the document.
    • The next line describes the root element of the document (like it was saying: &quot;this document is a note&quot;):<note>
    • Next 4 lines describes 4 child elements of the root (to, from, heading, and body)
    • And finally the last line defines the end of the root element:
  • 8. XML Syntax
    • All XML Elements Must Have a Closing Tag.
    • XML Tags are Case Sensitive.
    • XML Elements Must be Properly Nested.
      • <b><i>This text is bold and italic</i></b>
    • XML Documents Must Have a Root Element.
    • XML Attribute Values Must be Quoted.
    • Comments in XML
      • <!-- This is a comment -->
  • 9. XML Elements
    • XML Elements are Extensible.
    • XML Elements have Relationships
      • Elements are related as parents and children.
    • To understand the relationships between XML elements consider the following example:
      • My First XML
        • Introduction to XML
          • What is HTML
          • What is XML
        • XML Syntax
          • Elements must have a closing tag
          • Elements must be properly nested
  • 10. XML Elements
    • <book>
    • <title>My First XML</title>
    • <prod id=&quot;33-657&quot; media=&quot;paper&quot;></prod> <chapter>Introduction to XML
    • <para>What is HTML</para>
    • <para>What is XML</para>
    • </chapter>
    • <chapter>XML Syntax
    • <para>Elements must have a closing tag</para>
    • <para>Elements must be properly nested</para> </chapter>
    • </book>
  • 11. XML Elements
    • In the above example, book is the root element. title, prod, chapter are child elements of book.
    • Book is the parent element of title, prod, and chapter.
    • Title, prod, and chapter are siblings (or sister elements ) because they have the same parent.
  • 12. XML Elements
    • XML elements have contents. An element can have element content, mixed content, simple content, or empty content. An element can also have attributes .
    • In the example above, book has element content , because it contains other elements. Chapter has mixed content because it contains both text and other elements. Para has simple content (or text content ) because it contains only text. Prod has empty content , because it carries no information.
    • In the example above only the prod element has attributes . The attribute named id has the value &quot;33-657&quot;. The attribute named media has the value &quot;paper&quot;. 
  • 13. Naming an Element
    • XML elements must follow these naming rules:
      • Names can contain letters, numbers, and other characters
      • Names must not start with a number or punctuation character
      • Names must not start with the letters xml (or XML, or Xml, etc)
      • Names cannot contain spaces
  • 14. XML Attributes
    • XML elements can have attributes in the start tag, just like HTML.
    • Attributes are used to provide additional information about elements.
    • In HTML and in XML attributes provide additional information about elements:
    • Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person's sex, the person tag can be written like this:
      • <person sex=&quot;female&quot;> or
      • <person sex='female'>
  • 15. What is SimpleXML?
    • SimpleXML is new in PHP 5. It is an easy way of getting an element's attributes and text, if you know the XML document's layout.
    • SimpleXML converts the XML document into an object, like this:
      • Elements - Are converted to single attributes of the SimpleXMLElement object. When there's more than one element on one level, they're placed inside an array
      • Attributes - Are accessed using associative arrays, where an index corresponds to the attribute name
      • Element Data - Text data from elements are converted to strings. If an element has more than one text node, they will be arranged in the order they are found.
  • 16. Using SimpleXML
    • As of PHP 5.0, the SimpleXML functions are part of the PHP core.
      • Load the XML file.
      • Get the name of the first element.
      • Create a loop that will trigger on each child node, using the children() function.
      • Output the element name and data for each child node.
  • 17. PHP Functions for XML
    • PHP 5.0 onwards various functions are available to handle XML documents, some of them are as under:
      • simplexml_load_file()
      • simplexml_element->children()
      • simplexml_element->attribute()
  • 18. XML file
    • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
    • <note>
    • <to>Paresh</to>
    • <from>Jani</from>
    • <heading>Your Resume</heading>
    • <body>You have been short listed</body>
    • </note>
  • 19. simplexml_load_file()
    • The simplexml_load_file() gets a SimpleXMLElement object from an XML document.
    • Syntax:
      • simplexml_load_file(filename)
  • 20.
    • <?php
    • if (file_exists('test.xml'))
    • {
    • $xml = simplexml_load_file('test.xml'); var_dump($xml);
    • }
    • else
    • {
    • exit('Error.');
    • }
    • ?>
    •  var_dump() function displays structured information about one or more expressions that includes its type and value.
  • 21. simplexml->children()
    • The children() function gets the child nodes of the specified XML node. Syntax:
      • children()
  • 22.
    • <?php
    • $xml = simplexml_load_file(&quot;test.xml&quot;);
    • foreach ($xml->children() as $child)
    • {
    • echo &quot;Child node: &quot; . $child . &quot;<br />&quot;;
    • }
    • ?>
  • 23. simplexml->attribute()
    • The attributes() function gets an XML elements attributes.
    • Syntax:
      • attribute()
  • 24.
    • <?php
    • $xml = simplexml_load_file(&quot;test.xml&quot;); foreach($xml->body[0]->attributes() as $a => $b)
    • {
    • echo $a,'=&quot;',$b,&quot;&quot;</br>&quot;;
    • }
    • ?>