update TreeXML
This commit is contained in:
parent
f330a6672e
commit
78149024ee
@ -1,7 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/List.h>
|
||||
#include<hgl/type/String.h>
|
||||
#include<hgl/type/StringView.h>
|
||||
#include<hgl/type/KeyValue.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace xml
|
||||
@ -12,35 +15,55 @@ namespace hgl
|
||||
//但由于要一次性保存整个树形结构,所以对内存消耗较大,且必须等到整个XML解晰完才可以访问。
|
||||
//所以TreeXML仅限于对小型XML文件的解晰,对于大型XML文件,还是使用XMLParse进行流式解晰比较好。
|
||||
|
||||
class TreeXML
|
||||
struct TreeXMLData
|
||||
{
|
||||
char *xml_raw_data; ///<整体XML原始数据
|
||||
int xml_raw_size; ///<整体XML原始数据长度
|
||||
|
||||
protected:
|
||||
|
||||
using XSVList=List<U8StringView>;
|
||||
|
||||
protected:
|
||||
public:
|
||||
|
||||
U8StringView xml_raw_data; ///<XML原始数据
|
||||
|
||||
XSVList ElementNameList; ///<所有元素点名字文本视图
|
||||
XSVList AttsList; ///<所有属性点名字文本视图
|
||||
XSVList InfoList; ///<所有属性点信息文本视图
|
||||
XSVList DataList; ///<数据文本视图
|
||||
|
||||
public:
|
||||
|
||||
class Node
|
||||
{
|
||||
U8StringView element_name; ///<元素点名字视图
|
||||
U8StringView atts; ///<属性点文字视图
|
||||
};
|
||||
|
||||
class TreeXMLNode
|
||||
{
|
||||
/**
|
||||
<node atts="info">
|
||||
abcdefg
|
||||
</root>
|
||||
|
||||
element_name root
|
||||
atts atts=info
|
||||
data abcdefg
|
||||
*/
|
||||
|
||||
TreeXMLData * xml_raw_data;
|
||||
|
||||
int element_name; ///<元素点名字视图
|
||||
List<KeyValue<int,int>> atts; ///<属性点文字视图
|
||||
int data; ///<数据文本视图
|
||||
|
||||
private:
|
||||
|
||||
TreeXMLNode(TreeXMLData *,int);
|
||||
|
||||
void AddAtts(int,int);
|
||||
void SetData(int);
|
||||
|
||||
public:
|
||||
|
||||
TreeXML(char *,int);
|
||||
};//class TreeXML
|
||||
const U8StringView *GetElementName()const;
|
||||
|
||||
TreeXML *ParseXMLToTree(char *,int);
|
||||
const U8StringView *GetAtts(const U8String &);
|
||||
const U8StringView *GetAtts(const U8StringView &);
|
||||
|
||||
const U8StringView *GetData()const;
|
||||
};
|
||||
|
||||
TreeXMLNode *ParseXMLToTree(U8StringView);
|
||||
}//namespace xml
|
||||
}//namespace hgl
|
||||
|
@ -6,10 +6,43 @@ namespace hgl
|
||||
{
|
||||
namespace xml
|
||||
{
|
||||
TreeXML::TreeXML(char *data,int size)
|
||||
TreeXMLNode::TreeXMLNode(TreeXMLData *xdata,int name)
|
||||
{
|
||||
xml_raw_data=data;
|
||||
xml_raw_size=size;
|
||||
xml_raw_data=xdata;
|
||||
element_name=name;
|
||||
|
||||
data=-1;
|
||||
}
|
||||
|
||||
void TreeXMLNode::AddAtts(int name,int info)
|
||||
{
|
||||
atts.Add(Pair<int,int>(name,info));
|
||||
}
|
||||
|
||||
void TreeXMLNode::SetData(int d)
|
||||
{
|
||||
data=d;
|
||||
}
|
||||
|
||||
const U8StringView *TreeXMLNode::GetElementName()const
|
||||
{
|
||||
if(!xml_raw_data||element_name<0)
|
||||
return nullptr;
|
||||
|
||||
return xml_raw_data->ElementNameList.At(element_name);
|
||||
}
|
||||
|
||||
const U8StringView *TreeXMLNode::GetAtts(const U8String &name)
|
||||
{
|
||||
if(!xml_raw_data||atts.IsEmpty()||name.IsEmpty())
|
||||
return nullptr;
|
||||
|
||||
for(int i=0;i<atts.GetCount();i++)
|
||||
{
|
||||
if(xml_raw_data->AttsList[atts[i].left]==name)
|
||||
return xml_raw_data->InfoList.At(atts[i].right);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
namespace
|
||||
@ -38,13 +71,16 @@ namespace hgl
|
||||
}
|
||||
}//namespace
|
||||
|
||||
TreeXML *ParseXMLToTree(char *xml_raw_text,int xml_raw_size)
|
||||
TreeXMLNode *ParseXMLToTree(U8StringView sv)
|
||||
{
|
||||
if(!xml_raw_text||xml_raw_size<=0)return(nullptr);
|
||||
if(sv.IsEmpty())
|
||||
return(nullptr);
|
||||
|
||||
XMLTreeParse xtp;
|
||||
|
||||
TreeXML *root=new TreeXML(xml_raw_text,xml_raw_size);
|
||||
TreeXMLData *xml_data=new TreeXMLData;
|
||||
|
||||
xml_data->xml_raw_data=sv;
|
||||
|
||||
XML_Parser xml=XML_ParserCreate(XML_UTF8_Charset);
|
||||
|
||||
@ -53,7 +89,7 @@ namespace hgl
|
||||
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);
|
||||
XML_Parse(xml,(char *)sv.c_str(),sv.length(),true);
|
||||
}
|
||||
}//namespace xml
|
||||
}//namespace hgl
|
||||
|
Loading…
x
Reference in New Issue
Block a user