Вы находитесь на странице: 1из 6

What is XML?

XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data XML tags are not predefined. You must define your own tags XML is designed to be self-descriptive XML is a W3C Recommendation

The Difference Between XML and HTML XML is not a replacement for HTML. XML and HTML were designed with different goals:

XML was designed to transport and store data, with focus on what data is. HTML was designed to display data, with focus on how data looks.

XML is Just Plain Text XML is everywhere. It is the most common tool for data transmissions between all sorts of applications, and is becoming more and more popular in the area of storing and describing information. XML Separates Data from HTML If you need to display dynamic data in your HTML document, it will take a lot of work to edit the HTML each time the data changes. With XML, data can be stored in separate XML files. This way you can concentrate on using HTML for layout and display, and be sure that changes in the underlying data will not require any changes to the HTML. With a few lines of JavaScript, you can read an external XML file and update the data content of your HTML. XML Simplifies Data Sharing In the real world, computer systems and databases contain data in incompatible formats. XML data is stored in plain text format. This provides a software- and hardwareindependent way of storing data. This makes it much easier to create data that different applications can share.

XML Simplifies Data Transport With XML, data can easily be exchanged between incompatible systems. One of the most time-consuming challenges for developers is to exchange data between incompatible systems over the Internet. Exchanging data as XML greatly reduces this complexity, since the data can be read by different incompatible applications. XML Makes Your Data More Available Since XML is independent of hardware, software and application, XML can make your data more available and useful. Different applications can access your data, not only in HTML pages, but also from XML data sources. XML Documents Form a Tree Structure XML documents must contain a root element. This element is "the parent" of all other elements. The elements in an XML document form a document tree. The tree starts at the root and branches to the lowest level of the tree. All elements can have sub elements (child elements): An Example XML Document XML documents use a self-describing and simple syntax: <?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

XML Naming Rules XML elements must follow these naming rules:

Names can contain letters, numbers, and other characters Names cannot start with a number or punctuation character Names cannot start with the letters xml (or XML, or Xml, etc) Names cannot contain spaces

Any name can be used, no words are reserved. Well Formed XML Documents A "Well Formed" XML document has correct XML syntax. The syntax rules were described in the previous chapters:

XML documents must have a root element XML elements must have a closing tag XML tags are case sensitive XML elements must be properly nested XML attribute values must be quoted

Valid XML Documents A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD): <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE note SYSTEM "Note.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> The DOCTYPE declaration in the example above, is a reference to an external DTD file. The content of the file is shown in the paragraph below.

XML DTD

The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements: <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> If you want to study DTD, you will find our DTD tutorial on our homepage.

XML Schema W3C supports an XML-based alternative to DTD, called XML Schema: <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> Displaying XML with XSLT XSLT is the recommended style sheet language of XML. XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated than CSS. XSLT can be used to transform XML into HTML, before it is displayed by a browser:

What is the XMLHttpRequest Object? The XMLHttpRequest object is the developers dream, because you can:

Update a web page with new data without reloading the page Request and receive new data from a server after the page has loaded Communicate with a server in the background

XML Parser The XML DOM contains methods (functions) to traverse XML trees, access, insert, and delete nodes. However, before an XML document can be accessed and manipulated, it must be loaded into an XML DOM object. An XML parser reads XML, and converts it into an XML DOM object that can be accessed with JavaScript. Most browsers have a built-in XML parser. The XML DOM The XML DOM (XML Document Object Model) defines a standard way for accessing and manipulating XML documents. The DOM views XML documents as a tree-structure. All elements can be accessed through the DOM tree. Their content (text and attributes) can be modified or deleted, and new elements can be created. The elements, their text, and their attributes are all known as nodes. In the examples below we use the following DOM reference to get the text from the <to> element: xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue

xmlDoc - the XML document created by the parser. getElementsByTagName("to")[0] - the first <to> element childNodes[0] - the first child of the <to> element (the text node) nodeValue - the value of the node (the text itself)

The DOM is separated into 3 different parts / levels:


Core DOM - standard model for any structured document XML DOM - standard model for XML documents HTML DOM - standard model for HTML documents

Parsers? DOM vs SAX parser

Parsers are fundamental xml components, a bridge between XML documents and applications that process that XML. The parser is responsible for handling xml syntax, checking the contents of the document against constraints established in a DTD or Schema. DOM 1. Tree of nodes 2. Memory: Occupies more memory, preffered for small XML documents 3. Slower at runtime 4. Stored as objects 5. Programmatically easy 6. Ease of navigation SAX 1. Sequence of events 2. Doesn't use any memory preferred for large documents 3. Faster at runtime 4. Objects are to be created 5. Need to write code for creating objects 6. Backward navigation is not possible as it sequentially processes the document SAX: 1. Parses node by node 2. Doesnt store the XML in memory 3. We cant insert or delete a node 4. Top to bottom traversing DOM 1. Stores the entire XML document into memory before processing 2. Occupies more memory 3. We can insert or delete nodes 4. Traverse in any direction.

Вам также может понравиться