XML Primer           for Android Developers                      Vladimir Kulyukinhttp://www.youtube.com/vkedco      www.v...
Outline   ●       Why XML for Android?   ●       What is XML   ●       XML & HTML   ●       Tags & Elements   ●       Attr...
XML Primerhttp://www.youtube.com/vkedco   www.vkedco.blogspot.com
What is XML   ●       XML stands for eXtensible Markup Language   ●       eXtensible, because you can define your own XML ...
XML & HTML   ●       Both XML and HTML give documents tree-like       structures   ●       XML and HTML are similar but XM...
Sample XML     <address>       <name>         <first-name>Daniel</first-name>         <last-name>Watson</last-name>       ...
XML Tags & Elements   ●       Tags          –   Text b/w matching < and >          –   Start tags without slash: <street> ...
XML Attributes   ●       Attributes          –   Name/value pair inside a start tag < and >          –   Attribute values ...
XML Attributes vs. Elements   ●       Attributes          –   Cannot have multiple values          –   Cannot have tree su...
XML Attributes vs. Elements   ●       Element age             <person>                <name>Daniel</name>                <...
XML Syntax   ●       Tags are case-sensitive: <Person></Person> is different       from <person></person>   ●       End ta...
XML Comments   ●       Comments start with <!-- and end with -->   ●       Example:       <!-- This is a binary tree node ...
XML Entities   ●       Some special characters are called entities in       XML   ●       Examples:          – &lt;       ...
Entity Example   <!-- 10 < 20 is True -->   <bool_expression>      <lhs_operand>10</lhs_operand>      <comp_operator>&lt;<...
XML Document   <!-- declaration -->   <?xml version=”1.0” encoding=”utf-8” ?>   <!-- employees is the root element -->   <...
XML Namespaces   ●       Namespaces are a tag collision resolution       mechanism   ●       Different XML documents may r...
XML Namespaces   ●       Namespaces are defined as attributes that begin       with xmlns:       <bookstore        xmlns:b...
XML Tag Definitions   ●       Tag definitions can be explicitly controlled via           – Document Type Definition (DTD) ...
Parsing XML Documents   ●       Document Object Model (DOM) Parser          – Read everything into memory          – Manip...
Feedback             Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco       www.vkedco.blogspot...
Upcoming SlideShare
Loading in...5
×

XML Primer for Android Developers

598

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
598
On Slideshare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "XML Primer for Android Developers"

  1. 1. XML Primer for Android Developers Vladimir Kulyukinhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Why XML for Android? ● What is XML ● XML & HTML ● Tags & Elements ● Attributes, Comments, Entities ● Namespaces ● XML Parsing: SAX & DOMhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  3. 3. XML Primerhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. What is XML ● XML stands for eXtensible Markup Language ● eXtensible, because you can define your own XML tags ● Markup because it uses tags to define document structure: <LinearLayout></LinearLayout> ● Language (in the formal sense), because it has a rigorous syntax and can be used for knowledge engineeringhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  5. 5. XML & HTML ● Both XML and HTML give documents tree-like structures ● XML and HTML are similar but XML is considered more semantically flexible because of user-defined tags ● Unlike HTML that is typically rendered to be displayed in a browser, XML is just text and can be viewed in any text editorhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  6. 6. Sample XML <address> <name> <first-name>Daniel</first-name> <last-name>Watson</last-name> </name> <street>4205 Old Main Hill</street> <city>Logan</city> <state>UT</state> <postal-code></postal-code> </address>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  7. 7. XML Tags & Elements ● Tags – Text b/w matching < and > – Start tags without slash: <street> – End tags with slash: </street> ● Elements – Text b/w a pair of matching start and end tags <name> <first-name>Daniel</first-name> <last-name>Watson</last-name> </name>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  8. 8. XML Attributes ● Attributes – Name/value pair inside a start tag < and > – Attribute values are quoted ● Example: age is an attribute whose value is 28 <person age=”28”> <name>Daniel</name> </person>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  9. 9. XML Attributes vs. Elements ● Attributes – Cannot have multiple values – Cannot have tree sub-structures – Not easily expandable ● Elements – Can have multiple values – Can have tree sub-structures – More easily expandablehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  10. 10. XML Attributes vs. Elements ● Element age <person> <name>Daniel</name> <age>28</age> </person> ● Attribute age <person age=”28”> <name>Daniel</name> </person>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  11. 11. XML Syntax ● Tags are case-sensitive: <Person></Person> is different from <person></person> ● End tags can be shortened to /> <uses-sdk android:minSdkVersion=”8” android:targetSdkVersion=”15” /> ● Elements must be nested properly: – This nesting is OK: <employee><name>Daniel</name></employee> – This nesting is not OK: <employee><name>Daniel</employee></name>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  12. 12. XML Comments ● Comments start with <!-- and end with --> ● Example: <!-- This is a binary tree node --> <bin_tree_node> <!-- this is nodes key --> <key>125</key> <!-- this is nodes key --> <value>string_125</value> </bin_tree_node>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  13. 13. XML Entities ● Some special characters are called entities in XML ● Examples: – &lt; less than (<) – &gt; greater than (>) – &amp; ampersand (&) – &apos; apostrophe () – &quot; quotation mark (“)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  14. 14. Entity Example <!-- 10 < 20 is True --> <bool_expression> <lhs_operand>10</lhs_operand> <comp_operator>&lt;</comp_operator> <rhs_operand>20</rhs_operand> <bool_result>True</bool_result> </bool_expression>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  15. 15. XML Document <!-- declaration --> <?xml version=”1.0” encoding=”utf-8” ?> <!-- employees is the root element --> <employees> <person> <name>Bill</name><age>25</age> </person> <person> <name>Sue</name><age>30</age> </person> </employees>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  16. 16. XML Namespaces ● Namespaces are a tag collision resolution mechanism ● Different XML documents may reuse the same tags so long as these tags are used in different namespaces: – <title></title> in xmlns:books (books namespace) – <title></title> in xmlns:houses (houses namespace)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  17. 17. XML Namespaces ● Namespaces are defined as attributes that begin with xmlns: <bookstore xmlns:books=”http://www.mystore.com/books” xmlns:author=”http://www.mystore.com/books”> </bookstore> ● The name after xmlns: is the namespace ● The value is a string (often a unique URI)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  18. 18. XML Tag Definitions ● Tag definitions can be explicitly controlled via – Document Type Definition (DTD) which defines ● Elements that can appear ● Order in which elements can appear ● Nesting rules – XML Schemas ● Same definitions as DTD ● Plus: Data types and complex data rules ● These mechanisms are beyond the scope of this classhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  19. 19. Parsing XML Documents ● Document Object Model (DOM) Parser – Read everything into memory – Manipulate XML documents as in-memory trees ● Simple API for XML (SAX) Parser – XML documents are input streams – Events and data are generated dynamically as XML streams are read ● XML Parsing on Android – Both DOM and XML are supported – Additional support for GUI XML specificationshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  20. 20. Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco www.vkedco.blogspot.com

×