This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
Uncategorized
The Understanding XML Game
An exploration of XML in database management systems
Code Samples 1
Code Samples 2
Using XML to manage a wizard
An enhancement of the Microsoft XML Class Generator
Format XML
Sending Binary Data in XML to server
Retrieving a Registry subtree as XML
An XML Chat room
Base64 Encoder
A very simple way to generate multiple HTML combos from XML.
Retrieving a Registry subtree as XML
Retrieve Records
VB XML Parser
ITSE Namespace Navigator
Re: For Next Loop in XXL
Enabling the tab key in a TEXTAREA
Date sorting (advanced) combined with the Muenchian Method for grouping
Extracting an item from a delimited list
<< System.XML
WCF, WS, SOAP >>

By :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.
First posted :02/02/2001
Times viewed :25585

 
Lê isto em Português Lea esto en Español Lire cet article en Français Read this in English

Learn XML

What you should already know
What is XML?
How does XML work?
XML and Web browsers
Entities,  elements and attributes
Programming with XML
Communicating with the server using HTTPRequest
Working with ADO and XML
Well done!

What you should already know

To read and understand this a basic understanding of HTML would be useful.  To understand the code provided, you should know VB or ASP.

Get the source code files

  Get the source code here!

What is XML?

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.

Notes

A Note is just an idea - there is no commitment to following up on the Note. 

Working Drafts

This is a work in progress - a group within the W3C is actively working on this.

Proposed Recommendations

Some consensus has been achieved within the work group.  It has gone up for review.

Recommendations

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

Read more

To see some of the activities and projects within the W3C, go to http://www.w3.org/Consortium/Activities

How does XML work?

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.
  • The XML design should be prepared quickly.
  • The design of XML shall be formal and concise.
  • XML documents shall be easy to create.
  • Terseness in XML markup is of minimal importance.

XML and Web browsers

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.

Entities, elements and attributes

Elements

Elements look like this:

<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.

Programming with XML

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.

Loading XML elements into a combo-box

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

Accessing the XML

If you want to get to the XML in your DOMDocument, then in ASP you can use:

Response.Write(xmlDoc.xml)

or in VB you can use:

MsgBox xmlDoc.xml

Handling errors and debugging

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.

xmlDoc.parseError.reason

Communicating with the server HTTPRequest

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.

Receiving an XML DOMDocument object in VB

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.

Working with ADO and XML

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.

Well done!

You are now well on your way to understanding XML and working with it.  Read the further reading examples in the focus area.


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

View all Jobs (Add yours)
View all CV (Add yours)






    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP