65.9K
CodeProject 正在变化。 阅读更多。
Home

CXMLFile – 一个简单的 C++ XML 解析器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.82/5 (25投票s)

2008年3月19日

CPOL

4分钟阅读

viewsIcon

216411

downloadIcon

10170

一篇关于简单快速的 C++ XML 解析器的文章。

引言

本文介绍了一个简单快速的 C++ XML 解析器类。通常需要一个有效的 XML 解析器,能够加载 XML 文档、验证它并浏览它。在 .NET 环境中,对处理多种类型的 XML 文档有很大的原生支持,但原生的 C++、MFC 等却缺少这种支持。但是,有一种 COM 替代方案可用于 XML 文件解析和处理,但需要花费一些时间来学习并以正确的方式使用它。

本文旨在让 C++ 开发人员的生活比以往更容易。这是对以最简单的方式处理格式良好的 XML 文档的支持:加载、验证和浏览它。 它支持以下 XML 元素

  • 一个简单的 TAG 元素,如 <Element>
  • 一个简单的 ATTRIBUTE 元素,如 Attribute="Value"
  • 一个简单的 TEXT 元素,如 [Text]

以下是一个受支持的简单 XML 文件示例

    <?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>

呈现的 XML 类能够加载此类型的 XML 文档,检查其格式是否良好,并浏览其内容。只有两个类提供此功能。

第一个类称为 CXMLFile 类,其主要目的是加载 XML 文件,验证其结构,并从其内容创建 XML 元素集合。 此 XML 元素集合将在系统内存中表示加载的 XML 文件。 然后可以轻松地修改此集合的内部结构,即修改 XML 文件本身。 该类还支持从硬盘或内存流加载 XML 文件,这是一种特殊的用法(例如在某些 Web 服务器上)。 CXMLFile 类还可以将系统内存中的 XML 元素集合输出到硬盘上的文件。

第二个类称为 CXMLElement 类。它被前一个类使用,并将由开发人员在浏览或修改系统内存中 XML 文件的内部结构时使用,也就是说,在修改 XML 元素集合的内部结构时使用。它具有追加此集合并浏览它的基本支持。它可以提供有关集合中当前 XML 元素的名称、类型或值的信息。

背景

CodeProject 上有很多关于这个主题的文章,这是对这些文章的贡献。希望读者和开发人员能在他们的日常工作中发现它有用。

Using the Code

从硬盘加载 XML 文档非常容易。 请参见下面的示例

#include "XMLFile.h"

...

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

要从内存流加载 XML 文档

...

// lpData and dwDataSize are obtained elsewhere

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

要将 XML 元素集合保存到硬盘上的文件,请执行以下操作

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

在调用 LoadFromFile()CXMLFile 类的方法)之后,将完成自定义 XML 文件的验证和解析。 如果 XML 文件格式良好,它将作为 CXMLElement 元素的集合加载到系统内存中。 可以使用 CXMLFile 类的另一个名为 GetRoot() 的方法来访问此集合。 见下文

CXMLEElement* pRoot = xmlFile.GetRoot();

在系统内存中获得指向 XML 集合的根元素的指针之后,可以执行一些操作。 集合的根元素是 CXMLEElement 类类型。 以下是可用的方法

// 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();

使用以下方法修改 XML 元素集合的内部结构

// 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);

使用第一组 CXMLEElement 类方法,可以浏览 XML 元素集合。使用第二组 CXMLEElement 类方法,可以创建不同类型的新 XML 元素并将它们附加到现有元素。

说到 XML 元素的类型,这里列出了它们

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

关注点

我总是很难轻松地加载 XML 文档并对其进行操作。现在,我有用的类可以减少我将来需要此类工作时的开发时间。我现在也能够轻松解析 Web 上使用的 RSS 源。我计划很快将这种基本支持扩展到 HTML,或格式不太好的 XML 文档(当我找到更多空闲时间时)。

© . All rights reserved.