XML Presentation

advertisement

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

• 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

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

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

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.

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>

Alan Turing

</person> start tag element content end tag

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

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>

<name> root element (aka document element) child of “person”; parent of “firstname” and “lastname”

<firstname> Alan </firstname>

<lastname> Turing </lastname> child of “name” child of “name”

</name>

<profession> computer scientist </profession> child of “person”

<profession> mathematician </profession>

<profession> cryptographer </profession> child of “person” child of “person”

</person>

XML – File Format

(cont’d)

• Tree diagram of previous example person name profession profession profession firstname lastname

Alan Turing computer scientist mathematician cryptographer

Element

Element Content

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:

• Horizontal Rule:

<br/>

<hr/>

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

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:

• Ampersand:

&lt;

&amp;

• Greater-Than sign:

Double Quotation:

&gt;

&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

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”

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>

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> 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 (); if (saveFileDialog1.ShowDialog() == DialogResult .OK)

// Create an empty XML document

// Show the dialog. If OK is clicked,

// save the features to the selected file.

{

XmlElement elt = xmlDoc.CreateElement( "tiObjectList" ); foreach ( tiGeometry g in _tiGISObjectList._tiObjList)

// First, create a top-level element

// 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); xmlDoc.Save(saveFileDialog1.FileName);

} saveFileDialog1.Dispose();

// Append the top-level element to the document

// Write out the XML to a file.

// Dispose of the save dialog object

}

Sample C# Code to Open File

private void openToolStripMenuItem_Click( object sender, EventArgs e)

{

OpenFileDialog openFileDialog1 = new OpenFileDialog ();

XmlDocument xmlDoc = new XmlDocument ();

// Create a open file dialog

// 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);

XmlElement elt = xmlDoc.DocumentElement;

_tiGISObjectList._tiObjList.Clear(); foreach ( XmlElement childElt in elt.ChildNodes)

// Load the Xml document from the given file.

// get top-level element

// clear out current object list

// 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(); tiPictureBox.Refresh();

// Dispose of the open dialog object

//redraw scene

}

Download