Download presentation
Presentation is loading. Please wait.
Published byGeorge Reed Modified over 9 years ago
1
Introduction XML http://www.w3schools.com/xml/ Dr. Qusai Abuein June, 2012 XML Dr. Qusai Abuein 1 Part (I) XML Basics
2
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 XML Dr. Qusai Abuein 2
3
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 HTML is about displaying information, while XML is about carrying information. The tags used in HTML are predefined. HTML documents can only use tags defined in the HTML standard (like,, etc.). The tags used in XML are not predefined, XML allows the author to define his/her own tags and his/her own document structure. XML Dr. Qusai Abuein 3
4
XML Does Not DO Anything Maybe it is a little hard to understand, but XML does not DO anything. XML was created to structure, store, and transport information. The following example is a note to XX, from YY, stored as XML: XX YY Reminder Don't forget our trip this weekend! The note above is quite self descriptive. It has sender and receiver information, it also has a heading and a message body. But still, this XML document does not DO anything. It is just information wrapped in tags. Someone must write a piece of software to send, receive or display it. XML Dr. Qusai Abuein 4
5
XML Tags The tags in XML (like and ) are not defined in any XML standard. These tags are "invented" by the author of the XML document. That is because the XML language has no predefined tags. The tags used in HTML are predefined. HTML documents can only use tags defined in the HTML standard (like,, etc.). XML allows the author to define his/her own tags and his/her own document structure. XML Dr. Qusai Abuein 5
6
Starting XML XML Tree: XML documents form a tree structure that starts at "the root" and branches to "the leaves". An Example XML Document: XX YY Reminder Don't forget our trip this weekend! The first line is the XML declaration. It defines the XML version (1.0), which is mandatory. Although the number might change for future versions of XML, 1.0 is the current version It defines the XML version (1.0), which is mandatory. Although the number might change for future versions of XML, 1.0 is the current version XML Dr. Qusai Abuein 6
7
Starting XML XX YY Reminder Don't forget our trip this weekend! The first line also defines the encoding used (ISO-8859-1 = Latin-1/West European character set). This is optional. If used, the encoding declaration must appear immediately after the version information in the XML declaration, and must contain a value representing an existing character encoding. The encoding declaration identifies which encoding is used to represent the characters in the document. Although XML parsers can determine automatically if a document uses the UTF-8 or UTF-16 Unicode encoding. defines the encoding declaration for a document that uses the Japanese encoding method Shift- JIS. XML Dr. Qusai Abuein 7
8
Starting XML The next line describes the root element of the document (like saying: "this document is a note"): The next 4 lines describe 4 children elements of the root (to, from, heading, and body): XX YY Reminder Don't forget our trip this weekend! And finally the last line defines the end of the root element: XML Dr. Qusai Abuein 8
9
XML Rules XML documents must contain a root element. This element is "the parent" of all other elements. All elements can have sub elements (child elements):..... The terms parent, child, and sibling are used to describe the relationships between elements. Parent elements have children. Children on the same level are called siblings (brothers or sisters). All elements can have text content and attributes (just like in HTML). XML Dr. Qusai Abuein 9
10
XML Rules: Example XML Dr. Qusai Abuein 10 Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95
11
XML Rules: Example The root element in the example is. All elements in the document are contained within. The element has 4 children:,,,. Everyday Italian Giada De Laurentiis 2005 30.00 XML Dr. Qusai Abuein 11
12
XML Rules All XML Elements Must Have a Closing Tag In HTML, some elements do not have to have a closing tag: This is a paragraph. In XML, it is illegal to omit the closing tag. All elements must have a closing tag: This is a paragraph. Note: You might have noticed from the previous example that the XML declaration did not have a closing tag. This is not an error. The declaration is not a part of the XML document itself, and it has no closing tag. XML Dr. Qusai Abuein 12
13
XML Rules XML Tags are Case Sensitive XML tags are case sensitive. The tag is different from the tag. Opening and closing tags must be written with the same case: This is incorrect This is correct XML Dr. Qusai Abuein 13
14
XML Rules XML Elements Must be Properly Nested within each other: This text is bold and italic XML Dr. Qusai Abuein 14
15
XML Rules XML Attribute Values Must be Quoted: XML elements can have attributes in name/value pairs just like in HTML. In XML, the attribute values must always be quoted. Tove Jani XML Dr. Qusai Abuein 15
16
XML Rules Entity References: Some characters have a special meaning in XML. If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element. This will generate an XML error: if salary To avoid this error, replace the "<" character with an entity reference: if salary < 1000 then XML Dr. Qusai Abuein 16
17
XML Rules There are 5 predefined entity references in XML: Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it. XML Dr. Qusai Abuein 17 <<less than >>greater than &&ersand ''apostrophe ""quotation mark
18
XML Rules Comments in XML: The syntax for writing comments in XML is similar to that of HTML. XML Dr. Qusai Abuein 18
19
XML Rules White-space is Preserved in XML: HTML truncates multiple white-space characters to one single white-space: With XML, the white-space in a document is not truncated. XML Dr. Qusai Abuein 19 HTML:Hello Qusai Output:Hello Qusai
20
XML Rules XML Stores New Line as LF In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line feed (LF). In Unix applications, a new line is normally stored as an LF character. Macintosh applications also use an LF to store a new line. XML stores a new line as LF. XML Dr. Qusai Abuein 20
21
XML Elements What is an XML Element? An XML element is everything from (including) the element's start tag to (including) the element's end tag (, ). An element can contain: other elements ( contains,, ) Text ( contains “Harry Potter” as text) Attributes ( contains category=“CHILDREN” as attribute ) or a mix of all of the above... XML Dr. Qusai Abuein 21 Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95 In the example above, and have element contents, because they contain other elements. also has an attribute (category="CHILDREN").,,, and have text content because they contain text.
22
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. XML Dr. Qusai Abuein 22
23
Best Naming Practices Make names descriptive. Names with an underscore separator are nice:,. Names should be short and simple, like this: not like this:. Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first. Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first." Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later). XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents. Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them. XML Dr. Qusai Abuein 23
24
XML Elements are Extensible XML elements can be extended to carry more information. Look at the following XML example: XML Dr. Qusai Abuein 24 Tove Jani Don't forget our trip this weekend! Let's imagine that we created an application that extracted the,, and elements from the XML document to produce this output: MESSAGETo: Tove From: Jani Don't forget our trip this weekend! 2008-01-10 Tove Jani Reminder Don't forget our trip this weekend! Should the application break or crash? No. The application should still be able to find the,, and elements in the XML document and produce the same output. One of the beauties of XML, is that it can be extended without breaking applications.
25
XML Attributes XML elements can have attributes, just like HTML. Attributes provide additional information about an element. In HTML, attributes provide additional information about elements: Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but can be important to the software that wants to manipulate the element: XML Dr. Qusai Abuein 25 computer.gif
26
XML Attributes XML Attributes Must be Quoted Attribute values must always be quoted. Either single or double quotes can be used. For a person's gender, the person element can be written like this: or like this: If the attribute value itself contains double quotes you can use single quotes, like in this example: or you can use character entities: XML Dr. Qusai Abuein 26
27
XML Elements vs. Attributes Take a look at these examples: XML Dr. Qusai Abuein 27 Anna Smith female Anna Smith In the first example gender is an attribute. In the last, gender is an element. Both examples provide the same information. There are no rules about when to use attributes or when to use elements. Attributes are handy in HTML. In XML the advice is to avoid them. Use elements instead.
28
The Favorite Way The following three XML documents contain exactly the same information: A date attribute is used in the first example: XML Dr. Qusai Abuein 28 Tove Jani Reminder Don't forget our trip this weekend! A date element is used in the second example: 10/01/2008 Tove Jani Reminder Don't forget our trip this weekend! An expanded date element is used in the third: (THIS IS MY FAVORITE): 10 01 2008 Tove Jani Reminder Don't forget our trip this weekend!
29
Avoid XML Attributes? Some of the problems with using attributes are: attributes cannot contain multiple values (elements can) attributes cannot contain tree structures (elements can) attributes are not easily expandable (for future changes) Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data. Don't end up like this: XML Dr. Qusai Abuein 29 Tove Jani Reminder Don't forget our trip this weekend!
30
The XML DOM The XML DOM defines a standard way for accessing and manipulating XML documents. The XML DOM views an XML document 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. The HTML DOM The HTML DOM defines a standard way for accessing and manipulating HTML documents. All HTML elements can be accessed through the HTML DOM.
31
The XMLHttpRequest Object The XMLHttpRequest object is used to exchange data with a server behind the scenes. The XMLHttpRequest object is the developers dream, because you can: 1.Update a web page without reloading the page 2.Request data from a server after the page has loaded 3.Receive data from a server after the page has loaded 4.Send data to a server in the background
32
Example 1 XML FILE Abeer Hijaa Eyad Alshareef Call Me ASAP when you receive this e-mail, please call me
33
Example 1 HTML FILE Eyad script src ="file.js" type = "text/javascript"> First Program To: From: message:
34
Example 1 JS FILE function start() { xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","P1.xml",false); xmlhttp.send(); xmlDoc = xmlhttp.responseXML; document.getElementById("D1").innerHTML =xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue; document.getElementById("from").innerHTML =xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue; document.getElementById("message").innerHTML =xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue; } window.addEventListener("load",start,false);
36
THE RUN
37
Create an XMLHttpRequest Object All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) have a built-in XMLHttpRequest object. Syntax for creating an XMLHttpRequest object: xmlhttp=new XMLHttpRequest(); xmlhttp=new XMLHttpRequest(); Old versions of Internet Explorer (IE5 and IE6) uses an ActiveX Object: Old versions of Internet Explorer (IE5 and IE6) uses an ActiveX Object: xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); To handle all modern browsers, including IE5 and IE6, check if the browser supports the XMLHttpRequest object. If it does, create an XMLHttpRequest object, if not, create an ActiveXObject: To handle all modern browsers, including IE5 and IE6, check if the browser supports the XMLHttpRequest object. If it does, create an XMLHttpRequest object, if not, create an ActiveXObject: Example if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
38
Send a Request To a Server To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object: xmlhttp.open("GET","xmlhttp_info.txt ",true); xmlhttp.send();
39
Cont.… get the response data as XML data responseXML: get the response data as XML data xmlDoc - the XML DOM object created by the parser. getElementsByTagName("title")[0] - the first element childNodes[0] - the first child of the element (the text node) nodeValue - the value of the node (the text itself)
40
XmlDocument.GetElementsByTagName Method (String) Returns an XmlNodeList containing a list of all descendant elements that match the specified Name. xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeVa lue; Equivalent to : x=xmlDoc.getElementsByTagName(“to")[0] y=x.childNodes[0]; document.write(y.nodeValue);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.