Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
XML stands for eXtensible Markup Language and was approved by
the World Wide Web Consortium (W3C) in February 1998.
XML is not really a new language, it's a meta-language. It is used to define other languages.
XML creates documents
that are well structured and as a result all languages based on XML are also
well structured. What this means is that the data in XML is more easily
used.
So, XML is the foundation for a whole new way of communicating
across the Internet, because it enables businesses and their computer systems to
communicate more easily.
XML is used to create other markup languages
XML is used to create many other markup languages. One of
the best known is XSL. XSL is made out of XML. The relationship between XML and
XSL is very important. If XML is the raw data, XSL is used to
change that raw data into a usable format of data.
For example XSL may be used to put color into the XML, or to
change the XML into something else.
The development of the XML standard is guided by the W3C.
Who is the W3C? (WorldWide Web Consortium)
The W3C was founded in October 1994. Its goal is to
develop common protocols through an "international industry
consortium". http://www.w3.org
At the W3C, you can find:
a repository of
information about the WWW for developers and users
reference code
implementations for the various standards
various prototype
and sample applications to demonstrate use of new technology being
fostered at the W3C
The W3C is a collection of interested parties that meet to
thrash out various new Internet technologies. Their process
is quite substantial and inclusive and results in a consensus
decision (although their are stories on the web of strong-arming
within the working groups), however the end result does carry
substantial momentum.
How do you work with the W3C?
First someone (a group, a company, a person) makes a note submission on a technology or
technical issue to the W3C. After a LOT of discussion
and consulting, a draft proposal is created.
With luck and hard work, a recommendation may be achieved. At this
time, the technology is considered ready for widespread
industry use.
The idea which started as a Note has now become 'official'. According
to the W3C, this technology is ready for use.
What the W3C is not
What the W3C is not, is a "policeperson". They do not
enforce the agreements made or the technologies designed.
Each of the parties involved can (and sometimes do) "extend" or
"improve" upon the designs
Not only does XML hold your data, but it can also hold the structure and even
classification of the information inside your document. Using XML the document
carries information about itself!
This is possible because in XML you can define
your own tags. This is what makes it so much better than HTML which only
has a few tags for display - but XML is interested in so much more than
just the display!
Creating new tags in XML
As long as you follow the rules - you can easily create new XML
tags.
XML tags often look the same as HTML tags. Here are some
XML tags:
<book_title>XML PROGRAMMING WITH VB AND
ASP</book_title>
<author>Mark Wilson</author>
<publisher>Manning Publications</publisher>
<copydate>2000</copydate>
<retailprice>$1,000,000</retailprice>
<ISBN> 1884777872</ISBN>
What are the rules of XML?
There are 10 basic rules to XML.
Lets take a look at the goals set out for XML by the W3C:
XML shall be straightforwardly usable over the Internet.
XML shall support a wide variety of applications.
XML shall be compatible with SGML.
It shall be easy to write programs which process XML
documents.
The number of optional features in XML is to be kept to the
absolute minimum, ideally zero.
XML documents should be human-legible and reasonably
clear.
It's important to stress here that it is just beginning. Hardly
any web sites are making use of it, for the simple reason that
hardly any browsers support it.
Microsoft Internet Explorer 4.0, 5.0, 5.5 and the upcoming 6.0
all support XML.
Netscape has some support for XML in Communicator/Navigator 6.0.
and it also has extensive support for RDF (Resource Description
Format) to present what Netscape calls "full information
integration on the desktop."
RDF
RDF is an XML application which is ideally suited to data-modeling.
Through the support of Netscape and the Open Directory Project (http://www.dmoz.org)
RDF is being used to catalog the Internet.
<BOOK>...<TITLE>XML programming for VB and ASP
developers</TITLE>...</BOOK>
Attributes
Attributes are parts or properties or of elements.
<BOOK TITLE="XML programming for VB and ASP
developers">...</BOOK>
An interesting similarity between attributes and elements is
that this book element contains an attribute of publisher:
<Book Publisher="Manning">XML programming for VB and ASP
developers</Book>
Remember that attributes cannot be hierarchical - they cannot contain
sub-elements. But it you could also change the attribute into its own
element. Then you would use:
<Publication>
<Book>XML
programming for VB and ASP developers</Book>
<Publisher>Manning</Publisher>
</Publication>
Entities
Any file or web resource that can be "included" into an XML file
is an entity. Entity is also used to refer to special
character representations and substitutions of text strings and
includes.
An example of using entities to substitute entities for text
strings are:
<!ENTITY BookName "XML programming for VB and ASP
developers">
Now you can use the entity &BookName ; in a document and wherever you
refer to it, the entire string of "XML programming for VB and ASP
developers" will be substituted. In VB, this is similar to
using the constant.
Let's take a look at some examples of programming with XML.
Creating the XML objects
For ASP developers, use the following syntax to create a new DOMDocument object programmatically - this is the same
CreateObject syntax for creating the other objects.
<%
Dim xmlDoc
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
%>
and for VB programmers you can either use the CreateObject code
above, or you can add a reference to your project to the
"Microsoft XML, version 3.0" object and then you can use:
Dim xmlDoc As New DOMDocument
Getting the Microsoft XML objects onto your PC
To use these code examples which use the Microsoft XML
objects (such as DOMDocument), you first have to add a
reference to your project to the "Microsoft XML 3.0" object -
naturally you must have these objects installed on your development
and target distribution system where you will be installing your
solution.
There are two easy ways to get the Microsoft XML objects onto
your systems. You can either have IE5/5.5 (or the upcoming 6.0) installed, or you can
extend IE4 with the XML redistributable which you can download from
the Microsoft website.
This is how you can fill a drop down box by loading the content
of an XML file!
Private Sub cmdRefresh_Click()
Dim xmlCustomers As MSXML.DOMDocument
Dim xmlNode As MSXML.IXMLDOMNode
lstCustomers.Clear
'Call DLL to get array filled with Values
Set xmlCustomers = objCustomer.GetCustomerList()
For Each xmlNode In xmlCustomers.documentElement.childNodes
lstCustomers.AddItem xmlNode.childNodes(1).firstChild.nodeValue
& "_
" &
xmlNode.childNodes(2).firstChild.nodeValue lstCustomers.ItemData(lstCustomers.NewIndex) =_
xmlNode.childNodes(0).firstChild.nodeValue
Next xmlNode
End Sub
The simplest way to debug is to use display the "xml" property
of a DOMDocument and see if you have managed to load your document.
If this doesn't return or display anything then there was an error
loading, then use the following line of code to view the last
error
xmlDoc.xml
To find the last error, access the parseError collection.
One of the cool things about XMLHTTP is the ability to post XML
data to an HTTP server or get XML back from an HTTP server.
This is an easy way to become truly server independent because a
program doesn't mind who or what the HTTP server it's talking to
is, or even what operating system it is running on - all the HTTP
server needs to do is have the capability to receive HTTP calls
such as POST or GET and then pass the call on to some form of
program (ASP, CGI, Java Servlets, etc).
So you can look at the combination of XML via the HTTP calls as
a kind of middleware or glue to tie systems together.
In our case, we are going to open a connection to the host web
server (the Personal Web Server 1.0a) on my PC and then use an ASP
file which will give us a reply.
To make the connection, we will make use of an object which
ships with IE5 and its called XMLHTTP. Basically, you open a
"POST" connection to your webserver (which is all a web browser
does anyway!) and then using this connection, you "talk" to the
PEOPLEVB.ASP file and it will return your pick list in XML
format.
In the VB part of this example, the code to connect to the web
server is:
'create the objects
Dim xmlHttp As new XMLHTTPRequest
Dim xmlDoc As DOMDocument
'open the POST (or GET) connection to the web server
xmlHttp.Open "POST", "http://localhost/xmlcode/demo.asp", False
'establish the connection
xmlHttp.Send
'receive the response - note there are different types of
responses, binary and text is also supported
Set xmlDoc = xmlHttp.responseXML
On the web server side, we will use some ASP in the demo.asp
file which is being called. Make sure your server side ASP
file sets the ContentType to "text/xml" and to send the XML back to
the client, use:
<%
'make a variant object
Dim xmlDoc
'place the DOMDocument into the xmlDoc object
Set xmlDoc = Server.CreateObject("MSXML.DOMDocument")
'load the XML document object here
xmlDoc.load Server.MapPath("people.xml")
'set the content type to XML - note other types are
supported
Response.contenttype = "text/xml"
'now send back the DOMDocument you have loaded above
response.write xmlDoc
%>
Now you have a simple VB application, which communicates with an
ASP file on a web server and receives a DOMDocument as a
response.
If you are using ADO 2.1, you can save your recordset to an XML file using the Save
method on the ADO recordset object.
The Save method accepts two arguments, FileName and
PersistFormat. The trick is in the PersistFormat because you can use "adPersistADTG" which
is a binary format and you can use adPersistXML for an XML format.
Try this VB code to save your ADO recordset to XML:
Private Sub cmdSaveAdoFile_Click()
Dim PEOPLE As ADODB.Recordset
Dim ObjConn As New ADODB.Connection
'open an existing ODBC connection to your database
ObjConn.Open "VBXML"
'execute a select SQL statement against the database
Set PEOPLE = ObjConn.Execute("select * from People")
'save the recordset as an XML file
PEOPLE.Save "c:\adodemo.xml", adPersistXML
End Sub
Now, to load that XML file from XML back into an ADO recordset,
you can use this code for the load button:
Private Sub cmdLoadAdoFile_Click()
Dim PEOPLE As ADODB.Recordset
Set PEOPLE = New ADODB.Recordset
'open your XML file and load it
PEOPLE.Open "c:\adodemo.xml", "Provider=MSPersist"
'lets have a message box popup the number of records
MsgBox PEOPLE.RecordCount
End Sub
And that's all there is to it. Now you know how to save and load a
recordset to an XML file in a few lines of code.