This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA
,
This is a paragraph
This is another paragraph In XML all elements must have a closing tag, like this:
This is a paragraph
This is another 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. It is not an XML element, and it should not have a closing tag.<span datafld="ARTIST"> | <span datafld="TITLE"> |
tags cannot be bound to data, so we are using <span> tags. The <span> tag allows the datafld attribute to refer to the XML element to be displayed. In this case, it is datafld="ARTIST" for the XML in Real Life A real-life example of how XML can be used to carry information. Example: XML News XMLNews is a specification for exchanging news and other information. Using such a standard makes it easier for both news producers and news consumers to produce, receive, and archive any kind of news information across different hardware, software, and programming languages. An example XMLNews document: 23 XML Parser To read and update, create and manipulate an XML document, you will need an XML parser. Examples Parse an XML file - Crossbrowser example This example is a cross-browser example that loads an existing XML document ("note.xml") into the XML parser. Parse an XML string - Crossbrowser example This example is a cross-browser example on how to load and parse an XML string. Parsing XML Documents To manipulate an XML document, you need an XML parser. The parser loads the document into your computer's memory. Once the document is loaded, its data can be manipulated using the DOM. The DOM treats the XML document as a tree. To learn more about the XML DOM, please read our XML DOM tutorial. There are some differences between Microsoft's XML parser and the XML parser used in Mozilla browsers. In this tutorial we will show you how to create cross browser scripts that will work in both Internet Explorer and Mozilla browsers. Microsoft's XML Parser Microsoft's XML parser is a COM component that comes with Internet Explorer 5 and higher. Once you have installed Internet Explorer, the parser is available to scripts. Microsoft's XML parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML. To create an instance of Microsoft's XML parser, use the following code: JavaScript: var xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 24 VBScript: set xmlDoc=CreateObject("Microsoft.XMLDOM") ASP: set xmlDoc=Server.CreateObject("Microsoft.XMLDOM") The following code fragment loads an existing XML document ("note.xml") into Microsoft's XML parser: var xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.load("note.xml"); The first line of the script above creates an instance of the XML parser. The second line turns off asynchronized loading, to make sure that the parser will not continue execution of the script before the document is fully loaded. The third line tells the parser to load an XML document called "note.xml". XML Parser in Mozilla, Firefox, and Opera Mozilla's XML parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML. To create an instance of the XML parser in Mozilla browsers, use the following code: JavaScript: var xmlDoc=document.implementation.createDocument("ns","root",null); The first parameter, ns, defines the namespace used for the XML document. The second parameter, root, is the XML root element in the XML file. The third parameter, null, is always null because it is not implemented yet. The following code fragment loads an existing XML document ("note.xml") into Mozillas' XML parser: var xmlDoc=document.implementation.createDocument("","",null); xmlDoc.load("note.xml"); The first line of the script above creates an instance of the XML parser. The second line tells the parser to load an XML document called "note.xml". Parsing an XML File - A Cross browser Example The following example is a cross browser example that loads an existing XML document ("note.xml") into the XML parser: 25 <script type="text/javascript"> var xmlDoc; function loadXML() { // code for IE if (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.load("note.xml"); getmessage(); } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { xmlDoc=document.implementation.createDocument("","",null); xmlDoc.load("note.xml"); xmlDoc.onload=getmessage; } else { alert('Your browser cannot handle this script'); } } function getmessage() { document.getElementById("to").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; } W3Schools Internal NoteTo: <span id="to"> W3Schools Internal Note To: Tove From: Jani Message: Don't forget me this weekend! Important Note To extract the text (Jani) from an XML element like: 26 getElementsByTagName("from")[0].childNodes[0].nodeValue IMPORTANT: getElementsByTagName returns an array of nodes. The array contains all elements with the specified name within the XML document. In this case there is only one "from" element, but you still have to specify the array index ( [0] ). Parsing an XML String - A Cross browser Example The following code is a cross-browser example on how to load and parse an XML string: <script type="text/javascript"> var text="<note>"; text=text+" "); document.write("Text of second child element: "); document.write(x.childNodes[1].childNodes[0].nodeValue); Output: Text of first child element: Tove Text of second child element: Jani Note: Internet Explorer uses the loadXML() method to parse an XML string, while Mozilla browsers uses the DOMParser object. XML ADVANCED XML Namespaces 27 XML Namespaces provide a method to avoid element name conflicts. Name Conflicts Since element names in XML are not predefined, a name conflict will occur when two different documents use the same element names. This XML document carries information in a table:
Namespaces in Real Use When you start using XSL, you will soon see namespaces in real use. XSL style sheets are used to transform XML documents into other formats, like HTML. If you take a close look at the XSL document below, you will see that most of the tags are HTML tags. The tags that are not HTML tags have the prefix xsl, identified by the namespace "http://www.w3.org/1999/XSL/Transform": <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> My CD Collection
XML CDATA 30 All text in an XML document will be parsed by the parser. Only text inside a CDATA section will be ignored by the parser. Parsed Data XML parsers normally parse all the text in an XML document. When an XML element is parsed, the text between the XML tags is also parsed: <message>This text is also parsed The parser does this because XML elements can contain other elements, as in this example, where the Escape Characters Illegal XML characters have to be replaced by entity references. 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. You cannot write something like this: <message>if salary < 1000 then To avoid this, you have to replace the "<" character with an entity reference, like this: <message>if salary < 1000 then There are 5 predefined entity references in XML: < < less than > > greater than & & ampersand ' ' apostrophe " " quotation mark Note: Only the characters "<" and "&" are strictly illegal in XML. Apostrophes, quotation marks and greater than signs are legal, but it is a good habit to replace them. CDATA 31 Everything inside a CDATA section is ignored by the parser. If your text contains a lot of "<" or "&" characters - as program code often does - the XML element can be defined as a CDATA section. A CDATA section starts with "": <script> In the example above, everything inside the CDATA section is ignored by the parser. Notes on CDATA sections: A CDATA section cannot contain the string "]]>", therefore, nested CDATA sections are not allowed. Also make sure there are no spaces or line breaks inside the "]]>" string. XML Encoding XML documents may contain foreign characters, like Norwegian æ ø å , or French ê è é. To let your XML parser understand these characters, you should save your XML documents as Unicode. Windows 2000 Notepad Windows 2000 Notepad can save files as Unicode. Save the XML file below as Unicode (note that the document does not contain any encoding attribute): <note> 32 The file above, note_encode_none_u.xml will NOT generate an error in IE 5+, Firefox, or Opera, but it WILL generate an error in Netscape 6.2. Windows 2000 Notepad with Encoding Windows 2000 Notepad files saved as Unicode use "UTF-16" encoding. If you add an encoding attribute to XML files saved as Unicode, windows encoding values will generate an error. The following encoding (open it), will NOT give an error message: The following encoding (open it), will NOT give an error message: The following encoding (open it), will NOT give an error message: The following encoding (open it), will NOT generate an error in IE 5+, Firefox, or Opera, but it WILL generate an error in Netscape 6.2. Error Messages If you try to load an XML document into Internet Explorer, you can get two different errors indicating encoding problems: An invalid character was found in text content. You will get this error message if a character in the XML document does not match the encoding attribute. Normally you will get this error message if your XML document contains "foreign" characters, and the file was saved with a single-byte encoding editor like Notepad, and no encoding attribute was specified. Switch from current encoding to specified encoding not supported. You will get this error message if your file was saved as Unicode/UTF-16 but the encoding attribute specified a single-byte encoding like Windows-1252, ISO-8859-1 or UTF-8. You can also get this error message if your document was saved with single-byte encoding, but the encoding attribute specified a double-byte encoding like UTF-16. Conclusion 33 The conclusion is that the encoding attribute has to specify the encoding used when the document was saved. My best advice to avoid errors is: • • • Use an editor that supports encoding Make sure you know what encoding it uses Use the same encoding attribute in your XML documents XML on the Server XML can be generated on a server without installing any XML controls. Storing XML on the Server XML files can be stored on an Internet server exactly the same way as HTML files. Start Windows Notepad and write the following lines: <note> Generating XML with ASP XML can be generated on a server without any installed XML software. To generate an XML response from the server - simply write the following code and save it as an ASP file on the web server: <% response.ContentType="text/xml" response.Write("") response.Write("<note>") response.Write(" 34 Getting XML From a Database XML can be generated from a database without any installed XML software. To generate an XML database response from the server, simply write the following code and save it as an ASP file on the web server: <% response.ContentType = "text/xml" set conn=Server.CreateObject("ADODB.Connection") conn.provider="Microsoft.Jet.OLEDB.4.0;" conn.open server.mappath("/db/database.mdb") sql="select fname,lname from tblGuestBook" set rs=Conn.Execute(sql) rs.MoveFirst() response.write("") response.write(" XML Application This chapter demonstrates a small framework for an XML application. Note: This example uses a Data Island, which only works in Internet Explorer. The XML Example Document Look at the following XML document ("cd_catalog.xml"), that represents a CD catalog: 35 . . ... more ... . View the full "cd_catalog.xml" file in your browser. Load the XML Document Into a Data Island A Data Island can be used to access the XML file. To get your XML document "inside" an HTML page, add an XML Data Island to the HTML page: <xml src="cd_catalog.xml" id="xmldso" async="false"> With the example code above, the XML file "cd_catalog.xml" will be loaded into an "invisible" Data Island called "xmldso". The async="false" attribute is added to make sure that all the XML data is loaded before any other HTML processing takes place. Bind the Data Island to an HTML Table To make the XML data visible on the HTML page, you must "bind" the Data Island to an HTML element. To bind the XML data to an HTML table, add a datasrc attribute to the table element, and add datafld attributes to the span elements inside the table data:
Bind the Data Island to <span> or Elements <span> or elements can be used to display XML data. 36 You don't have to use the HTML table element to display XML data. Data from a Data Island can be displayed anywhere on an HTML page. All you have to do is to add some <span> or elements to your page. Use the datasrc attribute to bind the elements to the Data Island, and the datafld attribute to bind each element to an XML element, like this: Title: <span datasrc="#xmldso" datafld="TITLE"> Artist: <span datasrc="#xmldso" datafld="ARTIST"> Year: <span datasrc="#xmldso" datafld="YEAR"> or like this: Title: Artist: Year: If you have IE 5.0 or higher: See how the XML data is displayed inside the HTML elements. Note that if you use an HTML element, the data will be displayed on a new line. With the examples above, you will only see one line of your XML data. To navigate to the next line of data, you have to add some scripting to your code. Add a Navigation Script Navigation has to be performed by a script. To add navigation to the XML Data Island, create a script that calls the movenext() and moveprevious() methods of the Data Island. <script type="text/javascript"> function movenext() { x=xmldso.recordset if (x.absoluteposition < x.recordcount) { x.movenext() } } function moveprevious() { x=xmldso.recordset if (x.absoluteposition > 1) { x.moveprevious() } } 37 If you have IE 5.0 or higher: See how you can navigate through the XML records. All Together Now With a little creativity you can create a full application. If you use what you have learned on this page, and a little imagination, you can easily develop this into a full application. If you have IE 5.0 or higher: See how you can add a little fancy to this application. The XMLHttpRequest Object The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 9, and Netscape 7. What is an HTTP Request? With an HTTP request, a web page can make a request to, and get a response from a web server without reloading the page. The user will stay on the same page, and he or she will not notice that scripts might request pages, or send data to a server in the background. By using the XMLHttpRequest object, a web developer can change a page with data from the server after the page has loaded. Google Suggest is using the XMLHttpRequest object to create a very dynamic web interface: When you start typing in Google's search box, a JavaScript sends the letters off to a server and the server returns a list of suggestions. Is the XMLHttpRequest Object a W3C Standard? The XMLHttpRequest object is a JavaScript object, and is not specified in any W3C recommendation. However, the W3C DOM Level 3 "Load and Save" specification contains some similar functionality, but these are not implemented in any browsers yet. So, at the moment, if you need to send an HTTP request from a browser, you will have to use the XMLHttpRequest object. Creating an XMLHttpRequest Object For Mozilla, Firefox, Safari, Opera, and Netscape: var xmlhttp=new XMLHttpRequest() For Internet Explorer: var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") 38 Example <script type="text/javascript"> var xmlhttp function loadXMLDoc(url) { xmlhttp=null // code for Mozilla, etc. if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest() } // code for IE else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") } if (xmlhttp!=null) { xmlhttp.onreadystatechange=state_Change xmlhttp.open("GET",url,true) xmlhttp.send(null) } else { alert("Your browser does not support XMLHTTP.") } } function state_Change() { // if xmlhttp shows "loaded" if (xmlhttp.readyState==4) { // if "OK" if (xmlhttp.status==200) { // ...some code here... } else { alert("Problem retrieving XML data") } } } Try it yourself using JavaScript The syntax is a little bit different in VBScript: Try it yourself using VBScript Note: An important property in the example above is the onreadystatechange property. This property is an event handler which is triggered each time the state of the request changes. The states run from 0 (uninitialized) to 4 (complete). By having the function xmlhttpChange() check for the state changing, we can tell when the process is complete and continue only if it has been successful. Why are we Using Async in our Examples? 39 All the examples here use the async mode (the third parameter of open() set to true). The async parameter specifies whether the request should be handled asynchronously or not. True means that script continues to run after the send() method, without waiting for a response from the server. false means that the script waits for a response before continuing script processing. By setting this parameter to false, you run the risk of having your script hang if there is a network or server problem, or if the request is long (the UI locks while the request is being made) a user may even see the "Not Responding" message. It is safer to send asynchronously and design your code around the onreadystatechange event! More Examples Load a textfile into a div element with XML HTTP (JavaScript) Make a HEAD request with XML HTTP (JavaScript) Make a specified HEAD request with XML HTTP (JavaScript) List data from an XML file with XML HTTP (JavaScript) The XMLHttpRequest Object Reference Methods Method Description abort() Cancels the current request getAllResponseHeaders() Returns the complete set of http headers as a string getResponseHeader("headername") Returns the value of the specified http header open("method","URL",async,"uname","pswd") Specifies the method, URL, and other optional attributes of a request The method parameter can have a value of "GET", "POST", or "PUT" (use "GET" when requesting data and use "POST" when sending data (especially if the length of the data is greater than 512 bytes. The URL parameter may be either a relative or complete URL. The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing send(content) Sends the request setRequestHeader("label","value") Adds a label/value pair to the http header to be sent Properties Property Description onreadystatechange An event handler for an event that fires at every state change 40 readyState Returns the state of the object: 0 1 2 3 4 = = = = = uninitialized loading loaded interactive complete responseText Returns the response as a string responseXML Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties status Returns the status as a number (e.g. 404 for "Not Found" or 200 for "OK") statusText Returns the status as a string (e.g. "Not Found" or "OK") Save Data to an XML File Usually, we save data in databases. However, if we want to make the data more portable, we can store the data in an XML file. Create and Save an XML File Storing data in XML files is useful if the data is to be sent to applications on non-Windows platforms. Remember that XML is portable across all platforms and the data will not need to be converted! First we will learn how to create and save an XML file. The XML file below will be named "test.xml" and will be stored in the c directory on the server. We will use ASP and Microsoft's XMLDOM object to create and save the XML file: <% Dim xmlDoc, rootEl, child1, child2, p 'Create an XML document Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM") 'Create a root element and append it to the document Set rootEl = xmlDoc.createElement("root") xmlDoc.appendChild rootEl 'Create and append child elements Set child1 = xmlDoc.createElement("child1") Set child2 = xmlDoc.createElement("child2") rootEl.appendChild child1 rootEl.appendChild child2 'Add an XML processing instruction 'and insert it before the root element Set p=xmlDoc.createProcessingInstruction("xml","version='1.0'") xmlDoc.insertBefore p,xmlDoc.childNodes(0) 'Save the XML file to the c directory xmlDoc.Save "c:\test.xml" %> If you open the saved XML file it will look something like this ("test.xml"): 41 Real Form Example Now, we will look at a real HTML form example. We will first look at the HTML form that will be used in this example: The HTML form below asks for the user's name, country, and e-mail address. This information will then be written to an XML file for storage. "customers.htm": The action for the HTML form above is set to "saveForm.asp". The "saveForm.asp" file is an ASP page that will loop through the form fields and store their values in an XML file: <% dim xmlDoc dim rootEl,fieldName,fieldValue,attID dim p,i 'Do not stop if an error occurs On Error Resume Next Set xmlDoc = server.CreateObject("Microsoft.XMLDOM") xmlDoc.preserveWhiteSpace=true 'Create a root element and append it to the document Set rootEl = xmlDoc.createElement("customer") xmlDoc.appendChild rootEl 'Loop through the form collection for i = 1 To Request.Form.Count 'Eliminate button elements in the form if instr(1,Request.Form.Key(i),"btn_")=0 then 'Create a field and a value element, and an id attribute Set fieldName = xmlDoc.createElement("field") Set fieldValue = xmlDoc.createElement("value") Set attID = xmlDoc.createAttribute("id") 'Set the value of the id attribute equal to the name of 'the current form field attID.Text = Request.Form.Key(i) 'Append the id attribute to the field element 42 fieldName.setAttributeNode attID 'Set the value of the value element equal to 'the value of the current form field fieldValue.Text = Request.Form(i) 'Append the field element as a child of the root element rootEl.appendChild fieldName 'Append the value element as a child of the field element fieldName.appendChild fieldValue end if next 'Add an XML processing instruction 'and insert it before the root element Set p = xmlDoc.createProcessingInstruction("xml","version='1.0'") xmlDoc.insertBefore p,xmlDoc.childNodes(0) 'Save the XML file xmlDoc.save "c:\Customer.xml" 'Release all object references set xmlDoc=nothing set rootEl=nothing set fieldName=nothing set fieldValue=nothing set attID=nothing set p=nothing 'Test to see if an error occurred if err.number<>0 then response.write("Error: No information saved.") else response.write("Your information has been saved.") end if %> Note: If the XML file name specified already exists, it will be overwritten! The XML file that will be produced by the code above will look something like this ("Customer.xml"): <customer> XML DHTML Behaviors 43 Internet Explorer 5 introduced DHTML behaviors. Behaviors are a way to add DHTML functionality to HTML elements with the ease of CSS. Behaviors - What are They? IE5 introduced DHTML behaviors. Behaviors are a way to add DHTML functionality to HTML elements with the ease of CSS. How do behaviors work? By using XML we can link behaviors to any element in a web page and manipulate that element. DHTML behaviors do not use a <script> tag. Instead, they are using a CSS attribute called "behavior". This "behavior" specifies a URL to an HTC file which contains the actual behavior (The HTC file is written in XML). Syntax behavior: url(some_filename.htc) Note: The behavior attribute is only supported by IE 5 and higher, all other browsers will ignore it. This means that Mozilla, Firefox, Netscape and other browsers will only see the regular content and IE 5+ can see the DHTML behaviors. Example The following HTML file has a <style> element that defines a behavior for the element: |