Anda di halaman 1dari 8

Lecture 4 Week 4 Different uses of XML

• On the local machine, XML can be used to


• store configuration files,
• attach meta-data to documents (information about the document) in
XML in Action a well-defined, extensible format that can be processed using
widely available XML tools.

• XML can be used to create documents that can be


Outline
• used by various entities in a company, or to exchange data between
- Different uses of XML two incompatible databases in a neutral and auto-documented
- Traditional documents, Structured Documents and Dynamic Documents form.
- Some applications of XML using server-side programming
• XML documents can be published
• to the Web,
• in WML for wireless phones,
• on paper….. etc
1 2

DWAX 2010.1 DWAX 2010.1

Different uses of XML Why is XML suitable for all of this

• A convenient use of XML is to • XML file separates the


• include new elements to an HTML document, like price or
reference, that will be processed on the server and rendered as
HTML on the client. • Structure definition (DTD, Schema)

• One of the most important uses of XML is in e-commerce, • Data (XML file itself)
• where a set of tags can be agreed upon by several companies doing
business together.
• Formatting (XSLT or CSS)
• XML can also be used as the basic format
• to exchange messages between processes in distributed into three different files that can be manipulated
applications, allowing more flexibility in communications.
independently

3 4

DWAX 2010.1 DWAX 2010.1

1
Structured, Static and Dynamic documents Structured, Static and Dynamic documents
• What is a Structured Document ? • What is a Structured Document ? … contd.
The term Structured Document describes the concept that a document can be
viewed in a number of ways:

Formatting Rendering
• Logical Structure: This refers to the organization as seen by the
author - a book might consist of a preface, a table of contents, some chapter, a
bibliography and an index. The chapters might contain sections, which in turn,
contain paragraphs and graphics, ... Logical Physical Output
Document Document Document
• Physical Structure: This is often the structure as seen by the publisher
- a book might consist of a cover, and pages (which exist as galleys before
they are divided into pages). The pages might be divided into paragraphs
or drawings etc.

• Output Structure : This is the document as it is rendered - textbook


might be printed on paper or exist on the Web, etc.

5 6

DWAX 2010.1 DWAX 2010.1

Structured, Static and Dynamic documents Structured, Static and Dynamic documents
• Why do we need structure in documents (electronic) • What is a Static Document?
 Portability • Hardcopy document model
 Re-usability • No interaction with information
 Inter-system operability • No response from document
 Ease of storage and retrieval • User takes a passive role
 Low distribution costs • Examples of Static Mediums:- Books- Movies- Paintings-
Sculpture- Television
 Quick access
 Ease of maintenance and updating
• What is a Dynamic Document?
 Longevity
• Information can change
 Ease of organization and re-organization
• Allows interaction with information
 Appearance easily changed
• Produces a response to interaction
• Disadvantages
• User takes an active role
• High start-up costs
• Examples of Dynamic Mediums:- Computer Games - The Internet-
• Huge initial time commitment
7 Interactive Television 8

DWAX 2010.1 DWAX 2010.1

2
XML on the server XML on the Server – Example (ASP)
 Why?  Accessing data from XML file using ASP
– processing done on server, only result sent to client – Map the absolute path to the XML file
• don't have worry about browser compatibility issues
• strPath = Server.MapPath( “test.xml" )
• can hide data from user
– can use different stylesheets and xsl for different – test.xml:
browsers <?xml version="1.0"?>
– systems can provide better performance and <test>
<child>
maintainability for data-driven web sites by generating <text>Text of first child element</text>
and caching frequently accessed pages ahead of time </child>
on the server. <child>
<text>Text of second child element</text>
 Possible Negative: </child>
</test>
– places higher load on server
9 10

DWAX 2010.1 DWAX 2010.1

 Accessing data from XML file using ASP (cont.)  Accessing data from XML file using ASP (cont.)
– Saving nodes from XML document to variables:
Set xmlRoot = xmlFile.DocumentElement
– Create a DOM object for the XML file to be loaded Set xmlNodes = xmlFile.DocumentElement.ChildNodes
into:
Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" ) – Displaying content of nodes:
<%
xmlFile.Async = False
For Each xmlItem In xmlNodes
strText = xmlItem.text
%>
– Load the XML file into the variable <li>
<% =strText %>
xmlFile.Load( strPath ) </li>
<%
Next
%>
11 12

DWAX 2010.1 DWAX 2010.1

3
 Adding content to the XML file:  Adding an attribute to the element:
– Create a new node:
– Create a new node: Set xmlNode = xmlFile.CreateElement(“child”)
Set xmlNode = xmlFile.CreateElement(“child”) – Creating an empty element
– Creating an empty element Call xmlNode.SetAttribute( “attName", “attValue” )
– Creates an attribute called attName with a value of attValue
xmlNode.Text = “Text of third child element”
xmlNode.Text = “Text of third child element”
– Adds some text into the ‘child’ node – Adds some text into the ‘child’ node
Call xmlRoot.AppendChild(xmlNode) Call xmlRoot.AppendChild(xmlNode)
– Appends the new node to the root element stored in – Appends the new node to the root element stored in xmlRoot
xmlRoot – See:
http://www.devguru.com/technologies/xmldom/quickref/node_appendC
– Save the file: hild.html for another example using appendChild()

Call xmlFile.Save(strPath) – Save the file:


Call xmlFile.Save(strPath)

13 14

DWAX 2010.1 DWAX 2010.1

Some XML DOM methods Using XML on the server - Example


 selectNode(xpath_expression)
Files used:
– Will return a collection of nodes that matches the
xpath expression. Example:  default.asp
xmlNodes = xmlRoot.selectNode(child/text)  forums.xml
 removeChild(child_to_be_removed)  addForum.asp
– Will remove the child from the parent. The function  forum1.xml
returns the child. Example:  formatting.xsl
xmlChild = xmlRoot.removeChild(xmlRoot.childNodes.item(0))
 addPost.asp
This will remove the first child node from xmlRoot, and store the node in the
xmlChild variable. Check
http://www.devguru.com/technologies/xml_dom/16053.asp
15 Code for this example available on vUWS 16

DWAX 2010.1 DWAX 2010.1

4
default.asp
 Get absolute path to xml file (needed by
msxml when parsing the file)
– Server.MapPath("forums.xml")
 Create a DOM object:
– Server.CreateObject("Microsoft.XMLDOM")
 Example:
Dim strPath, xmlFile
strPath = Server.MapPath("forums.xml")
Set xmlFile =
Server.CreateObject("Microsoft.XMLDOM")
xmlFile.Async = False
17 18

DWAX 2010.1 DWAX 2010.1

 Accessing element content


 Loading the xml file: – elementName.text
– xmlFile.Load(strPath)  Accessing attribute content
• Uses the absolute path to the xml file – elementName.getAttribute(“attributeName”)
• Will return true if file loaded successfully
 Accessing nodes  Loop through the nodes, display the attribute and
element content:
– DocumentElement
For Each xmlItem In xmlNodes
– ChildNodes strFileName = xmlItem.getAttribute( "filename" )
– xmlFile.DocumentElement.ChildNodes strTitle = xmlItem.text
%>
• Will get a collection containing all the child nodes <li>
of the root node <a href = "<% =strFileName %>"><% =strTitle %></a>
</li>
19
<% 20
Next
DWAX 2010.1 DWAX 2010.1

5
default.asp
 Clicking the “Forum 1 Name” link will open
forum1.xml in the browser
– forum1.xml has a link to an xsl (formatting.xsl)
that will format the xml for display

21 22

DWAX 2010.1 DWAX 2010.1

forum1.xml addPost.asp

23 24

DWAX 2010.1 DWAX 2010.1

6
addForum.asp addForum.asp
 Preforms two tasks:
– Displays the form that gets the new forum’s
information
– Provides the script for creating the forum

25 26

DWAX 2010.1 DWAX 2010.1

 Adding elements and attributes:  Saving the new xml file on the server:
– Create a new element node – Call xmlFile.Save(strPath)
• Set xmlNode =
xmlFile.CreateElement(“name”)
– “name” is the name given to the new element
• xmlNode.text = “element text”
– Append the new element node to root
element
• Call xmlRoot.AppendChild(xmlNode)
– Adding an attribute to root node:
• xmlRoot.SetAttribute(“attributeName”,
“attributeContent”) 27 28

DWAX 2010.1 DWAX 2010.1

7
XML & PHP XML & ASP
 For intro on accessing xml with php using  Accessing XML data using ASP:
– Part 1:
DOM, see: http://www.4guysfromrolla.com/webtech/101200-1.shtml
http://www.w3schools.com/dom/dom_examples.asp  DevGuru:
 XML manipulation using PHP: – selectNodes() method example:
http://www.w3schools.com/dom/dom_examples.asp http://www.devguru.com/technologies/xmldom/quickref/node_selectNodes.html
– selectSingleNode() method example:
http://www.php.net/manual/en/intro.dom.php http://www.devguru.com/technologies/xmldom/quickref/node_selectSingleNode.html

 ASP 101:
– Saving HTML form data to XML using ASP:
http://www.asp101.com/articles/michael/htmlxml/
– Appending to XML with ASP:
http://www.asp101.com/articles/michael/htmlxml/
29 30

DWAX 2010.1 DWAX 2010.1

Anda mungkin juga menyukai