1 / 16

GTECH 731 Lab Session 13 11/2/10

GTECH 731 Lab Session 13 11/2/10. Quiz 2 Review XML. 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

miron
Download Presentation

GTECH 731 Lab Session 13 11/2/10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GTECH 731Lab 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.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 “<” followed by the element name and end with a “greater-than” sign “>” <ElementName> • End tags begin with a “less-than” sign and a forward slash “</” followed by the element name and end with a “greater-than” sign “>” </ElementName> • 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” <person> start tag Alan Turing element content </person> 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” <person born = “1912/06/23” died=“1954/06/07” > Alan Turing </person> • 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<person>root element (aka document element)<name>child of “person”; parent of “firstname” and “lastname”<firstname>Alan</firstname>child of “name”<lastname>Turing</lastname>child of “name”</name><profession>computer scientist</profession>child of “person”<profession>mathematician</profession>child of “person”<profession>cryptographer</profession>child of “person”</person>

  9. XML – File Format (cont’d) • Tree diagram of previous example person name profession profession profession firstname lastname computer scientist mathematician cryptographer Alan Turing Element Element Content

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

  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 &lt; to represent character within element content • Five predefined Entity References in XML include ... • Less-Than sign:&lt; • Ampersand: &amp; • Greater-Than sign: &gt; • Double Quotation: &quot; • Single Quotation or Apostrophe: &apos; • Only the &lt; (<) and &amp; (&) must be used instead of literal characters • &quot; 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 <?xml version=“1.0” encoding=“US-ASCII” standalone=“yes”> • 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 <tiObjectList> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (95 23)" ObjLineColor="Red" ObjLineThickness="1" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (97 56)" ObjLineColor="Red" ObjLineThickness="2" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (96 97)" ObjLineColor="Yellow" ObjLineThickness="3" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (92 140)" ObjLineColor="Yellow" ObjLineThickness="4" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (89 186)" ObjLineColor="Cyan" ObjLineThickness="5" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (89 227)" ObjLineColor="Magenta" ObjLineThickness="6" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (90 274)" ObjLineColor="Green" ObjLineThickness="7" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (90 322)" ObjLineColor="Green" ObjLineThickness="8" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (93 365)" ObjLineColor="Blue" ObjLineThickness="9" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (97 405)" ObjLineColor="Black" ObjLineThickness="10" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiLineString" Geometry="LINESTRING (148 33, 196 132, 255 29, 319 133, 357 30)" ObjLineColor="Green" ObjLineThickness="8" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPolygon" Geometry="POLYGON ((180 287, 183 390, 399 401, 387 277, 276 220, 180 287))" ObjLineColor="Cyan" ObjLineThickness="4" ObjFillColor="Black" ObjSelected="False" /> </tiObjectList>

  15. XML File and Sample C# Code to Save File <tiObjectList> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (95 23)" ObjLineColor="Red" ObjLineThickness="1" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (97 56)" ObjLineColor="Red" ObjLineThickness="2" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (96 97)" ObjLineColor="Yellow" ObjLineThickness="3" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (92 140)" ObjLineColor="Yellow" ObjLineThickness="4" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (89 186)" ObjLineColor="Cyan" ObjLineThickness="5" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (89 227)" ObjLineColor="Magenta" ObjLineThickness="6" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (90 274)" ObjLineColor="Green" ObjLineThickness="7" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (90 322)" ObjLineColor="Green" ObjLineThickness="8" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (93 365)" ObjLineColor="Blue" ObjLineThickness="9" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPoint" Geometry="POINT (97 405)" ObjLineColor="Black" ObjLineThickness="10" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiLineString" Geometry="LINESTRING (148 33, 196 132, 255 29, 319 133, 357 30)" ObjLineColor="Green" ObjLineThickness="8" ObjFillColor="" ObjSelected="False" /> <Shape GeoType="FinalProject.tiPolygon" Geometry="POLYGON ((180 287, 183 390, 399 401, 387 277, 276 220, 180 287))" ObjLineColor="Cyan" ObjLineThickness="4" ObjFillColor="Black" ObjSelected="False" /> </tiObjectList> usingSystem.Xml; // Required for Xml documents privatevoidsaveToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog1 = newSaveFileDialog(); // Create a save file dialog XmlDocumentxmlDoc = newXmlDocument(); // Create an empty XML document if (saveFileDialog1.ShowDialog() == DialogResult.OK) // Show the dialog. If OK is clicked, // save the features to the selected file. { XmlElementelt = 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 { XmlElementchildElt = 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 privatevoidopenToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = newOpenFileDialog(); // Create a open file dialog XmlDocumentxmlDoc = newXmlDocument(); // 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. XmlElementelt = xmlDoc.DocumentElement; // get top-level element _tiGISObjectList._tiObjList.Clear(); // clear out current object list foreach (XmlElementchildEltinelt.ChildNodes) // go through XML child elements & add // a corresponding geometry { StringtempGeoType = childElt.GetAttribute("GeoType"); StringtempGeo = childElt.GetAttribute("Geometry"); StringtempObjCol = childElt.GetAttribute("ObjLineColor"); StringtempObjLineThickness = childElt.GetAttribute("ObjLineThickness"); StringtempObjFillCol = childElt.GetAttribute("ObjFillColor"); StringtempSelected = childElt.GetAttribute("ObjSelected"); switch (tempGeoType) { case"FinalProject.tiPoint": { _tiGISObjectList._tiObjList.Add(newtiPoint(tempObjCol,Convert.ToInt16(tempObjLineThickness),tempGeo)); _tiGISObjectList._tiObjList[_tiGISObjectList._tiObjList.Count - 1].tiObjSelected = Convert.ToBoolean(tempSelected); break; } case"FinalProject.tiLineString": { _tiGISObjectList._tiObjList.Add(newtiLineString(tempObjCol,Convert.ToInt16(tempObjLineThickness), tempGeo)); _tiGISObjectList._tiObjList[_tiGISObjectList._tiObjList.Count - 1].tiObjSelected = Convert.ToBoolean(tempSelected); break; } case"FinalProject.tiPolygon": { _tiGISObjectList._tiObjList.Add(newtiPolygon (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 }

More Related