[WIP] Added TreeXML.h/.cpp
This commit is contained in:
parent
21e2b6a9b5
commit
ca43928e52
46
inc/hgl/util/xml/TreeXML.h
Normal file
46
inc/hgl/util/xml/TreeXML.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include<hgl/type/List.h>
|
||||||
|
#include<hgl/type/StringView.h>
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
namespace xml
|
||||||
|
{
|
||||||
|
//TreeXML可以直接将整个XML解晰到一个树形结构中
|
||||||
|
//对于访问解晰非常的友好
|
||||||
|
|
||||||
|
//但由于要一次性保存整个树形结构,所以对内存消耗较大,且必须等到整个XML解晰完才可以访问。
|
||||||
|
//所以TreeXML仅限于对小型XML文件的解晰,对于大型XML文件,还是使用XMLParse进行流式解晰比较好。
|
||||||
|
|
||||||
|
class TreeXML
|
||||||
|
{
|
||||||
|
char *xml_raw_data; ///<整体XML原始数据
|
||||||
|
int xml_raw_size; ///<整体XML原始数据长度
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
using XSVList=List<U8StringView>;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
XSVList ElementNameList; ///<所有元素点名字文本视图
|
||||||
|
XSVList AttsList; ///<所有属性点名字文本视图
|
||||||
|
XSVList InfoList; ///<所有属性点信息文本视图
|
||||||
|
XSVList DataList; ///<数据文本视图
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
class Node
|
||||||
|
{
|
||||||
|
U8StringView element_name; ///<元素点名字视图
|
||||||
|
U8StringView atts; ///<属性点文字视图
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
TreeXML(char *,int);
|
||||||
|
};//class TreeXML
|
||||||
|
|
||||||
|
TreeXML *ParseXMLToTree(char *,int);
|
||||||
|
}//namespace xml
|
||||||
|
}//namespace hgl
|
59
src/xml/TreeXML.cpp
Normal file
59
src/xml/TreeXML.cpp
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#include<hgl/util/xml/TreeXML.h>
|
||||||
|
#include<hgl/type/Stack.h>
|
||||||
|
#include<expat.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
namespace xml
|
||||||
|
{
|
||||||
|
TreeXML::TreeXML(char *data,int size)
|
||||||
|
{
|
||||||
|
xml_raw_data=data;
|
||||||
|
xml_raw_size=size;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
constexpr const char XML_UTF8_Charset[]="utf-8";
|
||||||
|
|
||||||
|
class XMLTreeParse
|
||||||
|
{
|
||||||
|
TreeXML *root;
|
||||||
|
|
||||||
|
Stack<TreeXML *> tree_stack;
|
||||||
|
|
||||||
|
TreeXML *current;
|
||||||
|
};
|
||||||
|
|
||||||
|
void TreeXMLStartElement(XMLTreeParse *xtp,const XML_Char *name,const XML_Char **atts)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void TreeXMLCharData(XMLTreeParse *xtp,const XML_Char *str,int len)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void TreeXMLEndElement(XMLTreeParse *xtp,const XML_Char *name)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}//namespace
|
||||||
|
|
||||||
|
TreeXML *ParseXMLToTree(char *xml_raw_text,int xml_raw_size)
|
||||||
|
{
|
||||||
|
if(!xml_raw_text||xml_raw_size<=0)return(nullptr);
|
||||||
|
|
||||||
|
XMLTreeParse xtp;
|
||||||
|
|
||||||
|
TreeXML *root=new TreeXML(xml_raw_text,xml_raw_size);
|
||||||
|
|
||||||
|
XML_Parser xml=XML_ParserCreate(XML_UTF8_Charset);
|
||||||
|
|
||||||
|
XML_SetUserData(xml,(void *)&xtp);
|
||||||
|
|
||||||
|
XML_SetElementHandler(xml,(XML_StartElementHandler)TreeXMLStartElement,(XML_EndElementHandler)TreeXMLEndElement);
|
||||||
|
XML_SetCharacterDataHandler(xml,(XML_CharacterDataHandler)TreeXMLCharData);
|
||||||
|
|
||||||
|
XML_Parse(xml,xml_raw_text,xml_raw_size,true);
|
||||||
|
}
|
||||||
|
}//namespace xml
|
||||||
|
}//namespace hgl
|
Loading…
x
Reference in New Issue
Block a user