DuiLib 源码分析之解析xml类CMarkup & CMarkupNode 头文件

xml使用的还是比较多的,duilib界面也是通过xml配置实现的

duilib提供了CMarkkup和CMarkupNode类解析xml,使用起来也是比较方便的,比较好奇它是怎么实现的,如果自己来写一个

解析又需要怎样架构,架构之路还很遥远。。。

先来看看头文件吧,CMarkup主要是用于分割xml,判断xml格式是否正确;CMarkupNode主要是将CMarkup分割的xml,获取节点中的属性,

最多支持64个属性

 enum
{
XMLFILE_ENCODING_UTF8 = ,//定义编码,默认使用utf8
XMLFILE_ENCODING_UNICODE = ,
XMLFILE_ENCODING_ASNI = ,
}; class CMarkup;
class CMarkupNode; class UILIB_API CMarkup//其实比较容易看懂
{
friend class CMarkupNode;
public:
CMarkup(LPCTSTR pstrXML = NULL);
~CMarkup(); bool Load(LPCTSTR pstrXML);
bool LoadFromMem(BYTE* pByte, DWORD dwSize, int encoding = XMLFILE_ENCODING_UTF8);//从内存中加载解析
bool LoadFromFile(LPCTSTR pstrFilename, int encoding = XMLFILE_ENCODING_UTF8);//从文件中加载解析
void Release();
bool IsValid() const; void SetPreserveWhitespace(bool bPreserve = true);
void GetLastErrorMessage(LPTSTR pstrMessage, SIZE_T cchMax) const;
void GetLastErrorLocation(LPTSTR pstrSource, SIZE_T cchMax) const; CMarkupNode GetRoot(); private:
typedef struct tagXMLELEMENT
{
ULONG iStart;//节点开始指针位置
ULONG iChild;//第一个孩子节点指针位置
ULONG iNext;//下一个兄弟节点指针位置
ULONG iParent;//父亲节点指针位置
ULONG iData;//节点属性结束的位置
} XMLELEMENT; LPTSTR m_pstrXML;//xml内存开始位置
XMLELEMENT* m_pElements;//数组储存节点信息
ULONG m_nElements;
ULONG m_nReservedElements;
TCHAR m_szErrorMsg[];
TCHAR m_szErrorXML[];
bool m_bPreserveWhitespace; private:
bool _Parse();
bool _Parse(LPTSTR& pstrText, ULONG iParent);//解析xml函数,主要是这里解析
XMLELEMENT* _ReserveElement();//如果数组不够长则扩充
inline void _SkipWhitespace(LPTSTR& pstr) const;
inline void _SkipWhitespace(LPCTSTR& pstr) const;
inline void _SkipIdentifier(LPTSTR& pstr) const;
inline void _SkipIdentifier(LPCTSTR& pstr) const;
bool _ParseData(LPTSTR& pstrText, LPTSTR& pstrData, char cEnd);//解析属性的值
void _ParseMetaChar(LPTSTR& pstrText, LPTSTR& pstrDest);//处理一些转义符号
bool _ParseAttributes(LPTSTR& pstrText);//解析属性
bool _Failed(LPCTSTR pstrError, LPCTSTR pstrLocation = NULL);
}; class UILIB_API CMarkupNode
{
friend class CMarkup;
private:
CMarkupNode();
CMarkupNode(CMarkup* pOwner, int iPos); public:
bool IsValid() const; CMarkupNode GetParent();
CMarkupNode GetSibling();
CMarkupNode GetChild();
CMarkupNode GetChild(LPCTSTR pstrName); bool HasSiblings() const;
bool HasChildren() const;
LPCTSTR GetName() const;
LPCTSTR GetValue() const;//这里获取不到节点的值,它返回的IData其实是属性末尾 bool HasAttributes();
bool HasAttribute(LPCTSTR pstrName);
int GetAttributeCount();
LPCTSTR GetAttributeName(int iIndex);
LPCTSTR GetAttributeValue(int iIndex);
LPCTSTR GetAttributeValue(LPCTSTR pstrName);
bool GetAttributeValue(int iIndex, LPTSTR pstrValue, SIZE_T cchMax);
bool GetAttributeValue(LPCTSTR pstrName, LPTSTR pstrValue, SIZE_T cchMax); private:
void _MapAttributes();//将之前分割好的xml映射到m_aAttributes中储存起来 enum { MAX_XML_ATTRIBUTES = }; typedef struct
{
ULONG iName;
ULONG iValue;
} XMLATTRIBUTE; int m_iPos;
int m_nAttributes;
XMLATTRIBUTE m_aAttributes[MAX_XML_ATTRIBUTES];
CMarkup* m_pOwner;//指向CMarkup指针,节点属性及值都是从这里获取
};

简单说一下这两个类的工作原理,首先用CMarkup加载xml入内存,在将其分割字符串,建立节点树的关系(通过XMLELEMENT*指向节点位置关系,iStart指向节点开始部分,iData指向节点末尾,iChild,iNext,iParent储存节点之间的关系),获取节点是属性则通过CMarkupNode根据节点iStart,iData的值进行属性提取。CMarkupNode用于获取属性值及获取父亲,兄弟,儿子节点,熟悉了这两个类的作用,使用起来就比较方便了,具体实现下次再详细说明。

上一篇:下拉的DIV+CSS+JS二级树型菜单


下一篇:PostgreSQL Replication之第三章 理解即时恢复(2)