zvenny
September 26th, 2000, 04:14 AM
Hi,
I've downloaded the Xerces-C code, samples and documentation from http://xml.apache.org .
I would like to implement this parser into an MFC application.
Can anyone get me started with this (hints, code,...) ?
Thanks...Sven
nordyj
October 12th, 2000, 12:20 PM
Hi Sven. Using the XML4C (Xerces) parser is actually not that bad, once you know how. Here's how I do it. First, you need to be sure that Visual Studio knows where the header and library files are for Xerces. Under the Tools|Options menu, on the 'Directories' tab, add the directories for the XML4C include files (the base include directory), and the library directory. Now, as for code, in your stdafx.h file, add the following code after all the AppWizard #include lines:
#include <dom/DOM.hpp>
#include <parsers/DOMParser.hpp>
#include <util/PlatformUtils.hpp>
#pramga comment(lib, "xerces-c_1.lib")
class XMLInit
{
public:
XMLInit(){XMLPlatformUtils::Initialize();}
~XMLInit(){};
};
XMLInit _Init;
Since I don't know your level of Visual C++ expertise, the #pragma comment statement links the xerces-c_1.lib file to your project. It performs the same operation as linking to the library from within the project properties. One catch about XML4C is that your application will die a horrible death if you do ANYTHING with the xml library before initializing it. Thats why I include the XMLInit class, then define a variable of type XMLInit right away. This way, initializing the library will be the very first thing that your app does, and we've removed one major head ache.
To open and parse an XML file, use the following code:
DOMParser xmlParser;
DOM_Document xmlDoc;
DOM_Element xmlRoot;
DOM_Element xmlElm;
xmlParser.parse("TestFile.xml");
xmlDoc=xmlParser.getDocument();
xmlRoot=xmlDoc.getDocumentElement();
xmlElm=xmlRoot.getElementsByTagName("SomeChildNode").item(0);
One thing that you have to be careful of is casting. This crops up primarily in two situations. The first is when trying to get a string from one of the xml objects. For example:
AfxMessageBox(xmlElm.getTagName());
This line will fail, because of the way that Xerces was written. While getTagName does indeed return a string, it returns a special string called DOMString (defined in DOMString.h), which is a very generalized string class, meant to support as many platforms as possible (Windows, Unix, OS/2, etc.) On the plus side, Xerces has a method for changing this text into something usable by our system:
AfxMessageBox(xmlElm.getTagName().transcode());
The transcode method converts the DOMString into your systems version of 'char *'.
The second situtation where casting can be confusing is when you want an element, but the function that you're using returns a node (in case you don't know, DOM_Element is derived from DOM_Node). If you assign a node to an element variable, your application will not compile. To properly cast a node to an element variable, do the following:
xmlElm=(DOM_Element&)xmlRoot.getFirstChild();
That's just a brief example for getting started. If you need more info, you can talk to the folks on the XML4C bulletin board at either news.alphaworks.ibm.com, or at http://www.alphaworks.ibm.com/discussion, and select the XML for C++ Discussion link.
I hope this helps you to get on your way!
Jamie Nordmeyer