Xmlphp

856 views

Published on

  • Be the first to comment

  • Be the first to like this

Xmlphp

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

×