XML 11/29/05 CS360 Windows Programming 1 What is XML? XML: Extensible Markup Language HTML expresses appearance XML is designed to express raw attention No predefined tags All text Provides a common data format for exchanging information 11/29/05 CS360 Windows Programming 2 XML Nothing remarkable about XML However, there are numerous tools available for reading and writing XML What would you do if you were handed a large free-formatted text file? 11/29/05 CS360 Windows Programming 3 HTML What is HTML? <HTML> <H1 ALIGN=CENTER>Recipe</H1> <FONT FACE size=2>Chocolate Chip Bars</FONT> How does XML differ? 11/29/05 CS360 Windows Programming 4 XML vs. HTML Example: <author>Shereen Khoja</author> <recipe_name>Chocolate Chip Bars</recipe_name> Tags are invented to describe the content Tags need to make sense to be useful 11/29/05 CS360 Windows Programming 5 XML Language for describing data and the structure of data Start with an XML declaration <?xml version=“1.0” encoding=“UTF-16”?> This is a processing instruction <? ?> 11/29/05 CS360 Windows Programming 6 XML Following the XML declaration is the document’s root element o Also called the document element <Guitars> …. </Guitars> Every document must have a document element 11/29/05 CS360 Windows Programming 7 Element Names Must conform to a set of rules o http://www.w3.org/TR/REC-xml Consist of letters or underscore Followed by letters, digits, periods, hyphens, and underscores No spaces! 11/29/05 CS360 Windows Programming 8 Elements Building blocks of XML May contain o Data o Other elements o Or both Always delimited by start and end tags 11/29/05 CS360 Windows Programming 9 Example <?xml version=“1.0”?> <Guitars> <Guitar> <Make>Gibson</Make> <Model>SG</Model> <Year>1977</Year> <Color>Tobacco Sunburst</Color> <Neck>Rosewood</Neck> </Guitar> <Guitar> <Make>Fender</Make> <Model>Stratocaster</Model> <Year></Year> <Color>Black</Color> <Neck>Rosewood</Neck> </Guitar> </Guitars> 11/29/05 CS360 Windows Programming 10 Another Example <?xml version="1.0"?> <list> <recipe> <author>Shereen Khoja</author> <recipe_name>Chocolate Chip Bars</recipe_name> <meal>Dinner <course>Dessert</course> </meal> <ingredients> <item>2/3 C butter</item> <item>2 C brown sugar</item> <item>1 tsp vanilla</item> <item>1 3/4 C unsifted all-purpose flour</item> <item>1 1/2 tsp baking powder</item> <item>1/2 tsp salt</item> <item>3 eggs</item> <item>1/2 C chopped nuts</item> <item>2 cups (12-oz pkg.) semi-sweet choc. chips</item> </ingredients> <directions> Preheat oven to 350 degrees. Melt butter; combine with brown sugar and vanilla in large mixing bowl. Set aside to cool. Combine flour, baking powder, and salt; set aside. Add eggs to cooled sugar mixture; beat well. ingredients, nuts, and chips. Stir in reserved dry Spread in greased 13-by-9-inch pan. Bake for 25 to 30 minutes until golden brown; cool. Cut into squares. </directions> </recipe> </list> 11/29/05 CS360 Windows Programming 11 XML Contains no information on layout Layout instructions will come from elsewheree 11/29/05 CS360 Windows Programming 12 Well Formed XML documents must be well formed o What does this mean? It must follow three rules o Document starts with an XML declaration o There is a root element in which all others are contained o All elements must be properly nested <ingredients><item></ingredients>chocolate chips</item> 11/29/05 CS360 Windows Programming 13 Well Formed XML Breaking any of the rules for XML well formed documents will result in a fatal error o What does this mean? Application will refuse to process XML XML documents can be very long How do we find errors? 11/29/05 CS360 Windows Programming 14 XML Parsers Solution: Use an XML parser Many free ones are available online o Lark: http://www.textuality.com/Lark/ 11/29/05 CS360 Windows Programming 15 Valid XML <recipe> <author>Shereen Khoja</author> <recipe_name>Chocolate Chip Bars</recipe_name> <meal>Dinner <course>Dessert</course> </meal> <ingredients> </ingredients> <directions>Melt </directions> butter; combine with, etc. ... </recipe> 11/29/05 CS360 Windows Programming 16 Valid XML Valid XML must match a Document Type Definition (DTD) Who creates the DTD? o We do! DTD defines the tags that can be used in an XML documents It also defines the type of data within the tags 11/29/05 CS360 Windows Programming 17 Valid XML <!DOCTYPE list [ <!ELEMENT recipe (recipe_name, author, meal, ingredients, directions)> <!ELEMENT ingredients (item+)> <!ELEMENT meal (#PCDATA|course)*> <!ELEMENT item (#PCDATA|sub_item)*> <!ELEMENT recipe_name (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT course (#PCDATA)> <!ELEMENT sub_item (#PCDATA)> <!ELEMENT directions (#PCDATA)> ]> 11/29/05 CS360 Windows Programming 18 Validating Parsers How can we test that an XML document is valid? Use a validating parser IE5 is an example of a validating parser 11/29/05 CS360 Windows Programming 19 XML in Action XML is used in many applications o Banks use proprietary systems to track transactions internally, but if they use a common XML format over the Web, then they'd be able to describe transaction information to another institution or an application (like Quicken or MS Money). Of course, they'd also be able to present the data in a pretty Web page. 11/29/05 CS360 Windows Programming 20 XML in Action Give an example of where you might use XML in an application…. 11/29/05 CS360 Windows Programming 21 Summary Completed Chapter 12 Next Time o XML in .NET 11/29/05 CS360 Windows Programming 22