YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   Home Products | Purchase Support | Downloads  
View in English
View in Japanese
View in
참고
View in Français
View in Italiano
View in 中文(繁體)
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
VX++ Cross-Platform C/C++
Overview
Download
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

C++ XML Parser

1

This article is about a simple and fast C++ XML parser class. There is often a need for an effective XML parser that is able to load the XML document, validate it, and browse it. In .NET environment there is a large native support for handling a lot of types of XML documents, but the same native support is missing from the original C++, MFC etc. There is, however, a COM alternative for XML file parsing and handling but it takes some time to learn it, and to use it in the right way.

This article is a simple attempt to make a C++ developer's life a bit easier than it usually is. This is support for handling the well-formed XML documents in the simplest possible way: load it, validate it, and browse it. This supports the following XML elements:

  • A simple TAG element, like <Element>
  • A simple ATTRIBUTE element, like Attribute="Value"
  • A simple TEXT element, like [Text]

Below is an example of a simple XML file that is supported:

 

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <note>
        <to>Tove</to>
        <from>Jani</from>

        <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
    </note>

The presented XML classes are able to load this type of XML document, check if it is well-formed, and browse throughout its content. There are only two classes that provide this functionality.

The first class is called the CXMLFile class, and its main purpose is to load an XML file, validate its structure, and create an XML element collection out of its content. This collection of XML elements will represent the loaded XML file in the system memory. Its easy then to modify the inner struture of this collection, that is, to modify the XML file itself. This class also supports the loading of XML files from the hard-disk or from the memory stream, which is a special usage (ie. on some web server). The CXMLFile class can also output the XML element collection from the system memory to the file on the hard-disk.

The second class is called the CXMLElement class. It is used by the previous class, and will be used by the developer when browsing or modifying the inner structure of an XML file in the system memory, that is, when modifying the inner structure of the XML element collection. It has the basic support for the appending of this collection, and browsing it. It can provide information regarding the name, type or value of the current XML element from the collection.

2

There are many articles on the considering this topic, and this is a small contribution to these articles population. Hope that the readers and developers will find it useful in their everyday work.

3

It's quite easy to load an XML document from the hard-disk. See an example below:

#include <span class="code-string">"XMLFile.h"</span>

...

_TCHAR lpszXMLFilePath[] = _T("A path to the XML file here...");
CXMLFile xmlFile;
if (xmlFile.LoadFromFile(lpszXMLFilePath))
{
   // Success
}
else
{
   // Error
}

To load an XML document from the memory stream:

...

// lpData and dwDataSize are obtained elsewhere

CXMLFile xmlFile;
if (xmlFile.LoadFromStream(lpData, dwDataSize))
{
    // Success
}
else
{
    // Error
}

To save the XML element collection to the file on the hard-disk, do the following:

if (xmlFile.SaveToFile(lpszXMLFilePath))
{
    // Success
}
else
{
    // Error
}

After the call to LoadFromFile(), a method of the CXMLFile class, the validation and parsing of the custom XMLfile will be done. If the XML file is well-formed, it will be loaded in the system memory as collection of CXMLElementelements. One can gain access to this collection using another method of the CXMLFile class called GetRoot(). See below:

CXMLEElement* pRoot = xmlFile.GetRoot();

Having the pointer to the root-element of the XML collection in the system memory, there are some things that can be done here. The root-element of the collection is of the CXMLEElement class type. Here are the methods available:

 

// Returns the name of the current XML element
LPTSTR GetElementName();
// Returns the type of the current XML element
XML_ELEMENT_TYPE GetElementType();
// Returns the number of child elements of the current XML element
int GetChildNumber();
// Returns the first child element of the current XML element
CXMLElement* GetFirstChild();
// Returns the current child element of the current XML element
CXMLElement* GetCurrentChild();
// Returns the next child element of the current XML element
CXMLElement* GetNextChild();
// Returns the last child element of the current XML element
CXMLElement* GetLastChild();
// Sets the value of the current XML element (valid only for attribute elements)
void SetValue(LPTSTR lpszValue);
// Gets the value of the current XML element (valid only for attribute elements)
LPTSTR GetValue();

Modify the inner structure of the XML element collection using the following methods:

// Create the new XML element of the specified type
void Create(LPTSTR lpszElementName, XML_ELEMENT_TYPE type);
// Appends the new XML element to the end of the collection of the current XML element
void AppendChild(CXMLElement* lpXMLChild);

Using the first group of CXMLEElement class methods, one can browse the XML element collection. Using the second group of CXMLEElement class methods, one can create new XML elements of different types and append them to existing ones.

Speaking about the types of XML elements, here are they listed:

XET_TAG // TAG element
XET_ATTRIBUTE // ATTRIBUTE element
XET_TEXT // TEXT element

Points of Interest

I always had a problem with loading XML documents easily and manipulating with them. Now, I have useful classes that decrease my future development time when this type of work is required. I am also able now to easily parse RSS feeds that are used all over the Web. I am planning to extend this basic support to HTML, or XML documents that are not-so-well-formed, soon (when I find some more free time).

C_XML_Parser.zip

News:

1 UCanCode Advance E-XD++ CAD Drawing and Printing Solution Source Code Solution for C/C++, .NET V2023 is released!

2 UCanCode Advance E-XD++ HMI & SCADA Source Code Solution for C/C++, .NET V2023 is released!

3 UCanCode Advance E-XD++ GIS SVG Drawing and Printing Solution Source Code Solution for C/C++, .NET V2023 is released!


Contact UCanCode Software

To buy the source code or learn more about with:

 

Ask any questions by MSN: UCanCode@hotmail.com Yahoo: ucan_code@yahoo.com


 

Copyright ?1998-2023 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@UCanCode.net