首页>
技术资讯>
详情

WinCE读写XML文件

2016-05-14 来源:佚名 阅读量: 0
关键词: WINCE

    This article will show you how to parse XML files on WinCE using MSXML 3.0, a XML parser from Microsoft.

    Okay, there are many articles around showing you how to parse XML, but not that many for WinCE using Visual C++. A lot of examples are for the .NET environment using Visual Basic .NET and C#. I'd like to share my experience here with parsing XML on Windows CE 4.2. The classes I present can be run on any device that has WinCE4.2 with MSXML 3.0 installed.

    Please note that I am using MSXML 3.0 as of writing this article; this is the latest version supported by WinCE 4.2. WinXP/Win2K supports version 4.0 that includes updated interfaces and supports more XPath expressions.

    The prerequisites for this article are that you have some experience with XML and the ways in which it can be manipulated using DOM or SAX. This article uses the former. Also, some experience of COM would be helpful.

    A Brief Overview of DOM (Document Object Model)

    DOM presents an XML document as a tree structure, just like a file hierarchy in Windows Explorer. The tree has a root node, the root node has parent nodes, the parent nodes have child nodes—I think you get the picture. You can refer to these nodes as elements, with other elements embedded inside them. These elements contain text and attributes that are manipulated through this DOM tree. The contents of these elements can be modified or deleted, and we can create new elements.

    MSXML—Microsoft's XML Parser

    MSXML is based on COM; it comes with Internet Explorer 5 and above. This component has many functions that help you traverse the XML document, access nodes within it, delete these nodes, update these nodes, insert nodes, and more. It is worth noting that MSXML also supports XSLT and XPath. I won't be using these technologies in this article, but just so you know, these are supported.

    That was a brief description of DOM and MSXML. If you need to know more, the Internet is a great resource that has many articles on the aforementioned topics.

    Initialising MSXML

    Now, on to some code. The first thing you need to do to use MSXML is to initialise it, Remember, I mentioned earlier that this is a COM component, so you first need to initialise COM:

    HRESULT hr = ::CoInitializeEx( NULL, COINIT_MULTITHREADED );This will return S_OK if all is well.

    You now need to create an instance of the MSXML COM object. I do the following for this:

    hr = m_iXMLDoc.CoCreateInstance( __uuidof( DOMDocument ) );So, what is m_iXMLDoc I hear you ask? It is of type IXMLDOMDocument and represents the top level of the XML document. It is worth pointing out now that WinCE4.2 only supports MSXML 3.0. If you were using Microsoft Windows XP, for example, this supports MSXML 4.0; thus, we could use IXMLDOMDocument4, which supports other methods.

    I have wrapped m_iXMLDoc up into a ATL smart pointer. This is done to avoid having to release the object myself (I may forget!); thus, you have:

    CComPtr<IXMLDOMDocument> m_iXMLDoc;If this function call succeeds, it will return S_OK.

    The next bit of code looks quite odd but is needed only if you are using Pocket PC:

    CComQIPtr<IObjectSafety,&IID_IObjectSafety> iSafety( m_iXMLDoc );

    if ( iSafety )

    {

    DWORD dwSupported, dwEnabled;

    iSafety->GetInterfaceSafetyOptions( IID_IXMLDOMDocument,

    &dwSupported, &dwEnabled

    );

    iSafety->SetInterfaceSafetyOptions( IID_IXMLDOMDocument,

    dwSupported, 0 );

    }

    This was taken off the Internet. I can't remember from where, so apologies to the person who wrote this, but without it, things don't seem to work; it is needed to mark the MSXML control as safe.

    Loading the XML

    You now have initialised COM and created the MSXML object; this in turn now lets us use the functionality supplied by the MSXML object. I am now going to load a very basic XML file. It looks like the following:

    <CustomerList>

    <Customers>

    <customer name="OnLineGolf" tag="OLG"/>

    <customer name="BetterGolf" tag="BG"/>

    <Customers/>

    <CustomerList/>

&

热门推荐 查看更多