XML
SGML
Standard
Formal
generalized markup language
system designed for building test markup languages. recognized standard for text information processing that provide an organized way to structure the content for broad use.
Internationally
XML
A
tag-based meta language
for structured data representation
Designed
Represents Provides
data hierarchically (in a tree)
context to data (makes it meaningful) presentation (HTML) from data (XML)
Self-describing data
Separates
An A
open W3C standard
vs. HTML, which is an implementation of SGML
subset of SGML
XML and Structured Data
Pre-XML
representation of data:
“PO-1234”,”CUST001”,”X9876”,”5”,”14.98”
XML
representation of the same data:
PO-1234 CUST001 X9876 5 14.98
Components of an XML Document
Elements
Each element has a beginning and ending tag
...
Elements can be empty (
)
Attributes
Describes an element; e.g. data type, data range, etc.
Can only appear on beginning tag
An XML Document
The Autobiography of Benjamin Franklin Benjamin Franklin 8.99 The Confidence Man Herman Melville 11.99
Rules For Well-Formed XML
There
must be one, and only one, root element
Sub-elements
must be properly nested
A tag must end within the tag in which it was started
Attributes
are optional
Defined by an optional schema
Attribute
values must be enclosed in “” or „‟
Processing
instructions are optional
XML
is case-sensitive
and are not the same type of element
Well-Formed XML?
No,
CHILD2 and CHILD3 do not nest properly.
This is element 1 Number 3
No,
there are two root elements
This is element 1 This is another element 1
Well-Formed XML?
Yes.
This is element 1
Namespaces: Overview
Part
of XML’s extensibility
Allow
authors to differentiate between tags of the same name (using a prefix) by a URI (Uniform Resource Identifier)
Identified
Namespace
declaration examples:
xmlns: bk = “http://www.example.com/bookinfo/”
Namespace declaration
Prefix
URI (URL)
Namespaces: Examples
All About XML Joe Developer 19.99
An XML namespace declared without a prefix becomes the default namespace for all sub-elements. All elements without a prefix will belong to the default namespace.
All About XML Joe Developer
CDATA, PCDATA AND Entity Ref.
Actual
data : 0 < x < 100
< [CDATA [ 0 < x < 100 ] ] >
Or
0 < x < 100
XML Parsers
SAX ( Simple API for XML)
API to allow developers to read/write XML data Event based
Uses a “push” model
StartDocument StartElement
Characters ----------------EndElement EndDocument
Sequential access only (data not cached) Requires less memory to process XML data
SAX has less overhead (uses small input, work and output buffers) than the DOM
The XML DOM
XML
Document Object Model (DOM)
Provides
a programming interface for manipulating XML documents in memory
a set of objects and interfaces that represent the content and structure of an XML document
Includes
Enables
Allows
a program to traverse an XML tree
elements, attributes, etc., to be added/deleted in an XML tree new XML documents to be created programmatically
Allows
XPath
XML Path Language
Uses:
General purpose query language for identifying nodes in an XML document
In XSL transformation
Supports standard comparison, Boolean and mathematical operators (=, <, and, or, *, +, etc.)
Contextual – the results depend on current node
XPath Query Examples
./author (finds all author elements within current context)
/bookstore (find the bookstore element at the root) /* //author (find the root element) (find all author elements anywhere in document)
/bookstore[@specialty = “textbooks”] (find all bookstores where the specialty attribute = “textbooks”)
/author/*
(find all elements of author element)
//author/@* (find all attributes of author element)
XSL
Language
Specifies
for expressing document styles
the presentation of XML
More powerful than CSS
Consists
of:
XSLT XPath XSL Formatting Objects (XSL-FO)
Transformations: Overview
XSLT
– a language used to transform XML data into a different form (commonly XML or HTML)
XML, HTML, … XSLT
Transformations: Example
Scootney Publishing Regional Sales Report Sales Report West Coast ...
/> /> /> />
Transformations: Example
... | Region\Quarter | Q | ... | color:red; color:green; | ...
Transformations: Example
.NET & XML
XML
Namespaces in .NET
System.Xml
.Xsl .XPath
.Schema
.Serialization
System.Xml Namespace
Overall namespace for classes that provide XML support
Classes for creating, editing, navigating XML documents Reading, writing and manipulating documents via the DOM
Use the XmlDocument class for XML documents Use the XmlDataDocument class relational or XML data
Classes that correspond to every “type” of XML “element”:
XmlElement, XmlAttribute, XmlComment, etc Used by the XmlDocument and XmlDataDocument classes
XmlDocument
Properties of Interest:
ChildNodes: Returns all the children of the current node DocumentType: Gets the DOCTYPE declaration node DocumentElement: Returns the root XmlElement
XmlResolver: Used to resolve DTD & schema references FirstChild: Returns the first child of the current node ParentNode: Returns the parent of the current node Value: Returns the (string) value of the current node
Methods of Interest
CreateComment: Creates a comment node
CreateElement: Creates an element node Load: Loads XML data using a URL, file, Stream, etc Save: Saves the XML document to a file, Stream, or writer
ReadXML.cs
using System;
using System.Xml;
class MyApp { static void Main() {
XmlDocument doc = new XmlDocument(); doc.Load("E:\\Workshop\\book.xml"); XmlNodeList nodes = doc.GetElementsByTagName("book"); foreach(XmlNode node in nodes)
{ Console.WriteLine("{0}",node["author"].InnerText); }
} }
STD
Views: 327 | Downloads: 31
clock
Views: 300 | Downloads: 28
|
|---|