Simple XML parsing on WinCE 4.2 using C ++ and MSXML 3.0
This article will show you how to parse XML files on WinCE using MSXML 3.0, a XML Parser from Microsoft.
Okay, there are using articles around und showing you how to parse XML, but not that same for wince using Visual C ++. A lot of examples are for. 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 orts version 4.0 that includes des 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 wocould 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 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 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 cocould 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/>
It's very simple. There isn't a lot to this, just a couple of elements and a couple of attributes. So, load this document. This is done with the following code:
Variant_bool bsuccess = false;
HR = m_ixmldoc-> load (ccomvariant (szxmlfile), & bsuccess );
Szxmlfile is the name of the XML file you want to load. please note that this cocould easily be a file that resides on a Web server; thus, you cocould specify a File URL. bsuccess will contain true if all is well.
Now, before moving on to the next bit of code, I need to introduce you to a useful function I wrote:
Void ccexml: displaychildren (ixmldomelement * pparent)
This function is going to traverse an element/node recursively. It looks like this:
Void ccexml: displaychildren (ixmldomelement * pparent)
{
Static ixmldomnode * pnextsib = NULL;
Static ixmldomnode * pchild = NULL;
If (pparent = NULL) // finished child
{
Return;
}
Displaychild (pparent );
Do
{
Pnextsib = pchild;
Pparent-> get_firstchild (& pchild );
If (pchild = NULL)
Pnextsib-> get_nextsibling (& pchild );
Displaychildren (ixmldomelement *) pchild );
}
While (pchild! = NULL );
}
Within this function is another important function:
Void ccustomerxml: displaychild (ixmldomelement * pchild)
This is a pure virtual function. This means the user of your class needs to implement this function, more on this later.
Okay, back to the Code. If the document has been loaded successfully, you can start to traverse it. This is done with the following piece of code:
Ccomptr <ixmldomelement> irooterelm;
HR = m_ixmldoc-> get_documentelement (& irooterelm );
If (failed (HR) | irooterelm = NULL) // empty XML file
{
MessageBox (null, l "empty document! ", L" error loading XML ",
Mb_iconstop );
Return false;
}
Ixmldomnode * inode = NULL;
Ixmldomnodelist * List = NULL;
Irooterelm-> get_childnodes (& list );
Long amount;
List-> get_length (& amount );
For (INT I = 0; I <amount; I ++)
{
List-> get_item (I, & inode );
Displaychildren (ixmldomelement *) inode );
}
By using this and the displaychildren function, the whole of the document is traversed.
Up to now, you have created the functionality to traverse the whole XML document, but what about manipulating the document? You don't just want to traverse the document. You actually want to do something with it!
(Continued)
SQL Server application platform Optimization
Download this whitepaper for a ready-made solution that delivers comprehensive infrastructure optimization in accordance with Microsoft's Apo model using HP polyserve software in conjunction with HP bladesystem C-class products and Microsoft SQL Server Enterprise edition.»
Simplified SQL Server deployment and management
Download this whitepaper for an efficient approach to SQL Server deployment and management with HP polyserve software for Microsoft SQL Server. the software supports both Microsoft SQL Server 2000 and SQL Server 2005, and between des a component called Matrix server, which is shared data clustering software. it allows Groups of servers and storage to work and be managed together flexibly to satisfy application requirements.»
Consolidating Microsoft SQL Server
The phenomenal success of Microsoft's SQL Server database has led to "SQL Server sprawl," which can be costly due to inefficient use of hardware, software, and administrative resources. learn how HP's polyserver software for Microsoft SQL server can reset lidate SQL server environments and improve resource availability.»
Easing the migration to Microsoft SQL Server
SQL Server 2005 is quickly becoming the database solution of choice. the task of SQL Server 2005 migration need not be as difficult, time-consuming, or costly as one might anticipate. software tools like HP polyserve migration manager can dramatically reduce the complexity of migration and slash deployment time by up to 66 percent.»
Remember the displaychild function I mentioned briefly earlier? For those who have forgotten, it is used within the displaychildren function and it is a pure virtual function that needs to be implemented. this function does the manipulation; hence the use of a pure virtual function that allows the user to do what they want with the element passed to it. the following displaychild function is a demonstration for the xml I showed you earlier in this article. because the XML was about MERS MERs, I have created a class called ccustomerxml that derives from your main XML class. this is how I manipulate the XML file:
Void ccustomerxml: displaychild (ixmldomelement * pchild)
{
BSTR nodetype, nodename;
Pchild-> get_nodetypestring (> nodetype );
Pchild-> get_nodename (& nodename );
If (wcscmp (nodename, text ("customer") = 0)
{
Cstring strattrib = parsexml (pchild );
If (! Strattrib. isempty ())
{
Cstring stratt;
Int item = 0;
Do
{
Stratt = getattributes (strattrib );
If (! Stratt. isempty ())
{
Switch (item)
{
Case 0: // CUSTOMER NAME
Afxmessagebox (stratt );
Break;
Case 1: // customer tag
Afxmessagebox (stratt );
Break;
}
Item ++;
}
}
While (! Stratt. isempty ());
}
}
}
The first check is to see whether this node is actually called "customer. "If it is, you start to manipulate the XML. you use another helper function, called parsexml, here. the function is shown below:
Cstring ccexml: parsexml (ixmldomelement * node)
{
Hresult hR = s_ OK;
BSTR nodename, nodevalue;
Ccombstr cname, cvalue;
Cstring strvalue;
Ixmldomnamednodemap * namednodemap = NULL;
HR = node-> get_attributes (& namednodemap );
If (succeeded (HR) & namednodemap! = NULL ){
Long listlength;
HR = namednodemap-> get_length (& listlength );
For (long I = 0; I <listlength; I ++ ){
Ixmldomnode * listitem = NULL;
HR = namednodemap-> get_item (I, & listitem );
// Node name
Listitem-> get_nodename (& nodename );
// Node value ie. Release = "V1.0" value is V1.0
Ccomvariant nodeval;
Nodeval. internalclear ();
Cvalue. Empty ();
Cname. Empty ();
Listitem-> get_nodevalue (& nodeval );
Nodevalue = nodeval. bstrval;
Cname. appendbstr (nodename );
Cvalue. appendbstr (nodevalue );
Cstring strxml (cname );
Cstring strval (cvalue );
Cstring S;
S. Format (L "% s: % s", strxml, strval );
Strvalue + = strval + L ",";
}
}
Return strvalue;
}
Briefly, what this function does is return the attributes associated with the node as one comma-separated string. in the example, it wocould return onlinegolf, OLG. the Code then uses another helper function, "getattributes," which returns each token within this string. thus, it wowould first return "onlinegolf" and then next time around und "OLG."
In my example function, I have a switch statement to know which attribute of the element I am dealing. remember, this function is written for the XML supplied, so I know the attributes are "name" and "tag. "If I added another attribute to the XML, I wocould just have another case statement for it. in my example function, I just display the element attribute to the screen, but in a real app, you wocould store them someplace or do something with them.
Fitting it all together
I have supplied a base class in this article for download. to use it, you will need to derive another class from it and, as mentioned, implement the displaychild function. using the example I have given in this article shoshould give you a start on how to do this. the base class is called ccexml, a simple derived class. it wowould look like the following:
Class ccustomerxml: Public ccexml
{
Public:
Ccustomerxml ();
Virtual ~ Ccustomerxml ();
Void displaychild (ixmldomelement * pchild );
};
All you need to do now is implement the displaychild function to match your needs. To start traversing the XML, you just need to make the following call:
Initialiseandparse (xmlfilename );
This function is found in the main base class. I have shown the main base class below just for information:
Class ccexml
{
Public:
Ccexml ();
Virtual ~ Ccexml ();
Bool initialiseandparse (lpctstr szxmlfile );
Ccomptr <ixmldomdocument> ccexml: createemptydomdocument ();
// This is a pure virtual function that needs to be written
// The derived class
Virtual void displaychild (ixmldomelement * pchild) = 0 {}
Cstring parsexml (ixmldomelement * node );
Cstring getattributes (cstring & strattribs );
Hresult dasetattribute (BSTR bstrname, BSTR bstrvalue,
Ixmldomnode * pnode,
Ixmldomdocument * pdocument );
Hresult daaddchild (BSTR name, int ntype, ixmldomnode ** pout,
Ixmldomnode * pnode,
Ixmldomdocument * pdocument );
Ixmldomdocument * getdocument () {return m_ixmldoc ;}
PRIVATE:
Void displaychildren (ixmldomelement * pparent );
Protected:
Ccomptr <ixmldomdocument> m_ixmldoc;
};
There are a couple of other functions within here that I have not mentioned; these are dasetattribute and daaddchild. what these functions do is pretty self-explanatory. the former sets a nodes attribute to a given value, while the latter adds an element to a given node. you shoshould study these functions; they are very useful! Here is a quick example on adding a node and creating an XML file:
Ccomvariant vtype (node_element );
Ixmldomnode * pnoddy, * pout;
// First, create an empty document
Ccomptr <ixmldomdocument> m_ixmldoc = createemptydomdocument ();
// Create a node and append to document
// Root Node
M_ixmldoc-> createnode (vtype, l "MERs", l "", & pnoddy );
M_ixmldoc-> appendchild (pnoddy, & pdetailsnode );
Name = l "details ";
Daaddchild (name, node_element, & pout, pdetailsnode, m_ixmldoc );
Ixmldomnode * pcust;
Name = l "customer ";
Daaddchild (name, node_element, & pcust, pout, m_ixmldoc );
// Add attribute to marque
Cstring strcustomer = l "onlinegolf ";
Ccombstr bstrcustomer (strcustomer );
Dasetattribute (L "name", bstrcustomer, pcust, m_ixmldoc );
// Save the XML
M_ixmldoc-> Save (ccomvariant (szsavexmlfile ));
This will create the following XML:
<Customers>
<Details>
<Customer name = "onlinegolf"/>
<Details/>
<Customers/>
You can see that the daaddchild and dasetattribute functions are very useful and easy to use.