vc++中使用xml

By Richard Anderson

Introduction

The Extensible Markup Language (XML) is one of those technologies that you just know you should start supporting in your applications sooner rather than later. Since it's official ratification in February 1998, it is fast becoming the de-facto way of exchanging and representing structured documents for many reasons ?suitability, easy of use, availability of tools, media coverage etc.

In this article I'll give you a flying start on how to add XML support to your application using the IE5 XML object model, in conjunction with the Visual C++ 6.0 compiler ?and (of course) COM.

In this first article I'll cover how to create a couple of XML documents from scratch. In subsequent articles I'll show you how to load and manipulate existing XML documents, as well as transforming XML documents into HTML (XHTML to be precise) by using the Extensible Stylesheet Language (XSL).

To get the most from this article you should have a basic understanding of XML concepts, specifically the structure of XML documents, and you also need to understand the basic concepts of the W3C Document Object Model (DOM), which is used to represent both XML and HTML documents in memory. I would love to introduce you to both of these standards, but this is only a short article. Such detail is best left to another article, or books such as XML Design and Implementation (ISBN 1861002289 http://www.wrox.com/Books/Book_Details.asp?ISBN=1861002289) published by Wrox Press.

The first sample we are going to write will produce an XML file that looks like this when viewed in IE5:

OK, I admit it, its nothing special or exciting, but the sample is very helpful in demonstrating some of the basic concepts about using the IE5 object model from C++. It shows how to create an XML document object, and how DOM nodes are created and added to it. These nodes represent the COMDeveloper element (represented by the start and end tags) and the 'Hello XML' text shown in the picture.

Using the Import Directive

All the samples in this article use the #import directive to de-compile the IE5 type library into useable C++ classes and smart pointers. This directive is part of what's known as the native COM compiler support. I've chosen to write the samples using this approach for several reasons. It is simple to use, helps to clearly demonstrate the usage of the IE5 objects without complicating the code, and ?most importantly ?it doesn't require you to go through the somewhat painful process of trying to get the latest SDK header files before you start.

Lets start by taking a look at the import directive that creates the C++ wrapper classes for the IE5 object model:


	#import "msxml.dll" rename_namespace("xml")

The most obvious point here is that the type library for IE5 is contained within MSXML.DLL. As this is located in the windows system directory, I've not had to specify a full path. The rename_namespace attribute defines all of the created wrapper classes in a C++ namespace called xml. This is good because it prevents possible

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值