Presentation is loading. Please wait.

Presentation is loading. Please wait.

GTECH 731 Lab Session 13 11/2/10 Quiz 2 Review XML.

Similar presentations


Presentation on theme: "GTECH 731 Lab Session 13 11/2/10 Quiz 2 Review XML."— Presentation transcript:

1 GTECH 731 Lab Session 13 11/2/10 Quiz 2 Review XML

2 XML – Introduction XML = Extensible Markup Language Language for describing data in a simple, straight forward format Saved as text file; anything that reads ASCII text file can read XML Allows documents and data to be read on many different platforms Can save information from many applications including MS Word, Excel, Powerpoint, or CorelDraw as well as custom programs XML is not a database Is Markup language, not a programming language Does not do anything Only stores information in defined format Can provide data to instruct or guide the logic of a program XML specification http://www.xml.com/axml/testaxml.htmhttp://www.xml.com/axml/testaxml.htm Other links on class website

3 XML – File Content Basic unit of XML Data “element” Stored as “text string” and “marked-up” with surrounding tags in human readable format Never binary data in XML file Markup tags describe document’s structure Tag names describe data element purpose (i.e., date, name, person etc.) Elements associated with other elements in hierarchical structure Meta-Markup language that can have any number of tags unlike HTML that has fixed number of tags “Extensible” because users can define tags for elements as they need them Customizable and usable in many different domains (i.e., web sites, data interchange, vector graphics, real estate etc.) “Grammar” regulates naming and placement of tags in documents “Well-formed” XML documents follows proper grammar

4 XML – Application XML “Application” Not executable programs in usual sense of “application” Set of agreed-upon “tags” used by organization or group of individuals to enhance interoperability between users XML being “Applied” in particular domain Real estate agency uses program that saves data to XML files, and posts information to website XML Application restricts data elements to those relevant to domain Might include “house”, “address”, “bedrooms”, “eat-in-kitchen” etc. Program will open and save XML files with only those agreed-upon tags Markups for particular XML Application defined in “document type definition” or DTD Lists all legal Markups for that domain Specifies where and how Markups can be used in a document Document might contain DTD itself or pointer to centrally located DTD

5 XML – Accessing Files Software packages use an XML “parser” to access XML documents “system.xml” namespace in Visual Studio C# environment provides tools for saving and extracting information in XML documents Information retrieved from XML document divided into “elements” and “attributes” and passed to calling program piece by piece Parser reports errors when violation of XML rules detected or when document “instance” of XML Application does not match with DTD Documents that match DTD are valid; those that don’t are invalid Validity errors are not necessarily fatal and an application can continue to parse the XML file if desired XML documents that are not “well-formed” produce fatal errors Applications that might use XML parsers include... Web browsers, word processors, database servers, drawing programs, spreadsheets etc.

6 XML – File Format Documents “Marked-up” with “start” and “end” tags Start tags begin with a “less-than” sign “ ” End tags begin with a “less-than” sign and a forward slash “ ” Everything between the start tag and end tag mark an element’s content The following simple XML document provides for a single element of type “person” with content “Alan Turing” start tag Alan Turing element content end tag

7 XML – File Format (cont’d) Elements can have “attributes” “name-value” pairs defined within the start tag of an element Name is separated from value with equal sign In the following example, “born” and “died” are attributes of the element “person” Alan Turing When do you use “attributes” vs “elements” to hold data? Depends on the use of the information and personal preference Some believe attributes should be metadata about element information Others say its too difficult to what is data or metadata No steadfast rules

8 XML – File Format (cont’d) XML documents are hierarchical trees with parent and child elements All elements have exactly one parent except the root element that has no parent Child element must be completely enclosed within a parent element start and end tags root element (aka document element ) child of “person”; parent of “firstname” and “lastname” Alan child of “name” Turing child of “name” computer scientist child of “person” mathematician child of “person” cryptographer child of “person”

9 XML – File Format (cont’d) Tree diagram of previous example person nameprofession firstnamelastname computer scientist mathematiciancryptographer AlanTuring Element Content Element

10 XML – File Format (cont’d) Elements can have both character data and child elements in “Mixed Content” elements Character Only: Alan Turing Child Elements Only: Alan Turing Mixed Content: My name is Tony and I live in Ossining. XML is case sensitive != != Must close element with same name as it was opened with OK not OK Empty elements Line Break: Horizontal Rule:

11 XML – Name Rules Rules for element and attribute names are the same May contain... Standard English Alpha-numeric: A-Z, a-z, 0-9 Non-English numbers and ideograms: ö, ç, ψ etc. Punctuation characters: Underscore, Hyphen and Period May not contain... Punctuation characters: Quotation marks, apostrophes, dollar signs, carets, percent symbols and semicolons Colon is allowed but reserved for use in namespaces White space of any kind such as space character, carriage return, line feed or non-breaking space Can start with... Letters, ideograms or underscore Not a number, hyphen or a period There is no limit to name length

12 XML – Entity References “Entity References” are escape sequences to represent reserved characters within element content Less-than sign “<” is always interpreted as the beginning of a tag Use escape sequence < to represent character within element content Five predefined Entity References in XML include... Less-Than sign:< Ampersand:& Greater-Than sign:> Double Quotation:" Single Quotation or Apostrophe:&apos; Only the < (<) and & (&) must be used instead of literal characters " and &apos; can be used when literal apostrophe or double quote might be mistaken for the ending of an attribute value

13 XML – Declaration XML documents should begin with an XML Declaration, but is optional If document contains declaration, it must be first line in file Includes name, version, encoding and standalone attributes Version attribute Always “1.0” as this is the most recent version Encoding attribute Provide name of encoding such as UTF-8 (8-bit Unicode Transformation Format), ASCII, ISO-8859_1 etc Attribute is optional with UTF-8 as default Standalone attribute Refers to DTD location If value is “yes”: Internal or no DTD If value is “no”: external DTD is used Optional attribute with default value of “no”

14 XML File and Corresponding Image

15 XML File and Sample C# Code to Save File using System.Xml; // Required for Xml documents private void saveToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog();// Create a save file dialog XmlDocument xmlDoc = new XmlDocument();// Create an empty XML document if (saveFileDialog1.ShowDialog() == DialogResult.OK)// Show the dialog. If OK is clicked, // save the features to the selected file. { XmlElement elt = xmlDoc.CreateElement("tiObjectList");// First, create a top-level element foreach (tiGeometry g in _tiGISObjectList._tiObjList)// Then go through each feature and create // a corresponding child element { XmlElement childElt = xmlDoc.CreateElement("Shape");// Shape will be the name of each element; childElt.SetAttribute("GeoType", g.GetType().ToString()); childElt.SetAttribute("Geometry", g.tiObject.ToText() ); childElt.SetAttribute("ObjLineColor", g.tiObjLineColor);// class, type, and geometry will be the attributes. childElt.SetAttribute("ObjLineThickness", g.tiObjLineThickness.ToString()); childElt.SetAttribute("ObjFillColor", g.tiObjFillColor); childElt.SetAttribute("ObjSelected", g.tiObjSelected.ToString()); elt.AppendChild(childElt);// Append the new element as a child element of the top-level element } xmlDoc.AppendChild(elt);// Append the top-level element to the document xmlDoc.Save(saveFileDialog1.FileName);// Write out the XML to a file. } saveFileDialog1.Dispose();// Dispose of the save dialog object }

16 Sample C# Code to Open File private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog();// Create a open file dialog XmlDocument xmlDoc = new XmlDocument();// Create an empty XML document if (openFileDialog1.ShowDialog() == DialogResult.OK)// Show the dialog. If OK is clicked, // save the features to the selected file. { xmlDoc.Load(openFileDialog1.FileName);// Load the Xml document from the given file. XmlElement elt = xmlDoc.DocumentElement;// get top-level element _tiGISObjectList._tiObjList.Clear();// clear out current object list foreach (XmlElement childElt in elt.ChildNodes)// go through XML child elements & add // a corresponding geometry { String tempGeoType = childElt.GetAttribute("GeoType"); String tempGeo = childElt.GetAttribute("Geometry"); String tempObjCol = childElt.GetAttribute("ObjLineColor"); String tempObjLineThickness = childElt.GetAttribute("ObjLineThickness"); String tempObjFillCol = childElt.GetAttribute("ObjFillColor"); String tempSelected = childElt.GetAttribute("ObjSelected"); switch (tempGeoType) { case "FinalProject.tiPoint": { _tiGISObjectList._tiObjList.Add(new tiPoint(tempObjCol,Convert.ToInt16(tempObjLineThickness),tempGeo)); _tiGISObjectList._tiObjList[_tiGISObjectList._tiObjList.Count - 1].tiObjSelected = Convert.ToBoolean(tempSelected); break; } case "FinalProject.tiLineString": { _tiGISObjectList._tiObjList.Add(new tiLineString(tempObjCol,Convert.ToInt16(tempObjLineThickness), tempGeo)); _tiGISObjectList._tiObjList[_tiGISObjectList._tiObjList.Count - 1].tiObjSelected = Convert.ToBoolean(tempSelected); break; } case "FinalProject.tiPolygon": { _tiGISObjectList._tiObjList.Add(new tiPolygon (tempObjCol, tempObjFillCol,Convert.ToInt16(tempObjLineThickness), tempGeo)); _tiGISObjectList._tiObjList[_tiGISObjectList._tiObjList.Count - 1].tiObjSelected = Convert.ToBoolean(tempSelected); break; } openFileDialog1.Dispose();// Dispose of the open dialog object tiPictureBox.Refresh();//redraw scene }


Download ppt "GTECH 731 Lab Session 13 11/2/10 Quiz 2 Review XML."

Similar presentations


Ads by Google