加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
exmlparser.cpp 13.13 KB
一键复制 编辑 原始数据 按行查看 历史
云之梦歌 提交于 2023-06-11 11:15 . 修复置节点值闪退
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
#include "tinyxml/tinyxml.h"
#include "include_exmlparser_header.h"
#include <Windows.h>
#include <stdio.h>
#include "elib/krnllib.h"
#include "elib/fnshare.h"
/*// 返回数组的数据部分首地址及成员数目。
void* GetAryElementInfo(void* pAryData, LPINT pnElementCount)
{
LPINT pnData = (LPINT)pAryData;
INT nArys = *pnData++; // 取得维数。
// 计算成员数目。
INT nElementCount = 1;
while (nArys > 0)
{
nElementCount *= *pnData++;
nArys--;
}
if (pnElementCount != NULL)
*pnElementCount = nElementCount;
return (void*)pnData;
}
*/
int GetAttributeCount(TiXmlElement* xe)
{
TiXmlAttribute* attr = xe->FirstAttribute();
int i = 0;
if (attr) {
for (TiXmlAttribute* curr = attr;
curr != nullptr;
curr = curr->Next())
i++;
}
return i;
}
//XML文档
typedef struct _EXMLDocument {
//真为已经创建 假为没有创建
int CreateFlag;
//文档
TiXmlDocument* pXMLDoucument;
}_EXMLDocument, * EXMLDocument;
//加载
//str file,int encoding
ECMDIMPL(XmlDoc, 0, LoadXmlFile)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
EXMLDocument exml = (EXMLDocument)pArgInf[0].m_pCompoundData;
LPSTR file = pArgInf[1].m_pText;
int encoding = pArgInf[2].m_int;
exml->pXMLDoucument = new TiXmlDocument();
exml->CreateFlag = 1;
if (encoding == 0)
exml->pXMLDoucument->LoadFile(file, TIXML_ENCODING_UTF8);
else if (encoding == 1)
exml->pXMLDoucument->LoadFile(file, TIXML_ENCODING_UNKNOWN);
//exml->pXMLRootNode = exml->pXMLDoucument->RootElement();
pRetData->m_bool = TRUE;
}
//解析
//str xmltext
ECMDIMPL(XmlDoc, 1, ParserXml)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
EXMLDocument exml = (EXMLDocument)pArgInf[0].m_pCompoundData;
LPSTR str = pArgInf[1].m_pText;
exml->pXMLDoucument = new TiXmlDocument();
exml->pXMLDoucument->Parse(str);
//exml->pXMLRootNode = exml->pXMLDoucument->RootElement();
exml->CreateFlag = 1;
pRetData->m_bool = TRUE;
}
//创建
//str name
ECMDIMPL(XmlDoc, 2, CreateXml)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
EXMLDocument exml = (EXMLDocument)pArgInf[0].m_pCompoundData;
LPSTR name = pArgInf[1].m_pText;
if (exml->CreateFlag == 1)
{
delete exml->pXMLDoucument;
}
TiXmlElement* root = new TiXmlElement(name);
exml->pXMLDoucument = new TiXmlDocument();
exml->pXMLDoucument->InsertEndChild(*root);
pRetData->m_bool = TRUE;
}
//生成文本
ECMDIMPL(XmlDoc, 3, ToXmlString)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
EXMLDocument exml = (EXMLDocument)pArgInf[0].m_pCompoundData;
pRetData->m_pText = (char*)exml->pXMLDoucument->Value();
}
//保存
//str file
ECMDIMPL(XmlDoc, 4, SaveXml)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
EXMLDocument exml = (EXMLDocument)pArgInf[0].m_pCompoundData;
LPSTR file = pArgInf[1].m_pText;
exml->pXMLDoucument->SaveFile(file);
pRetData->m_bool = TRUE;
}
//取根节点
ECMDIMPL(XmlDoc, 5, GetRootNode)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
EXMLDocument exml = (EXMLDocument)pArgInf[0].m_pCompoundData;
pRetData->m_int = (int)exml->pXMLDoucument->RootElement();
}
//取文档说明
ECMDIMPL(XmlDoc, 6, GetDeclaration)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
EXMLDocument exml = (EXMLDocument)pArgInf[0].m_pCompoundData;
TiXmlDeclaration* declaration = exml->pXMLDoucument->ToDeclaration();
if (declaration != nullptr) {
LPSTR rst = new char[32];
LPSTR v = (char*)declaration->Version();
LPSTR e = (char*)declaration->Encoding();
LPSTR s = (char*)declaration->Standalone();
sprintf(rst, "%s,%s,%s",
v == nullptr ? "" : v,
e == nullptr ? "" : e,
s == nullptr ? "" : s);
pRetData->m_pText = rst;
}
else {
pRetData->m_pText = (char*)"";
}
}
//取子节点数
//int node
ECMDIMPL(XmlDoc, 7, GetChildCount)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe == nullptr)
{
pRetData->m_int = 0;
return;
}
TiXmlElement* attr = xe->FirstChildElement();
int i = 0;
if (attr) {
for (TiXmlElement* curr = attr;
curr != nullptr;
curr = curr->NextSiblingElement())
i++;
}
pRetData->m_int = i;
}
//取属性
//int node,str name
ECMDIMPL(XmlDoc, 8, GetAttribute)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
LPSTR name = (LPSTR)pArgInf[2].m_pText;
if (xe != nullptr)
{
pRetData->m_pText = (char*)xe->Attribute(name);
}
else {
pRetData->m_pText = (char*)"";
}
}
//取属性数
//int node
ECMDIMPL(XmlDoc, 9, GetAttributeCount)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe != nullptr)
{
pRetData->m_int = GetAttributeCount(xe);
}
else {
pRetData->m_int = 0;
}
}
//取子节点
//int node,str name
ECMDIMPL(XmlDoc, 10, GetChildNode)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe == nullptr)
{
pRetData->m_int = 0;
return;
}
LPSTR name = (LPSTR)pArgInf[2].m_pText;
TiXmlNode* tn = xe->FirstChild(name);
if (tn != nullptr)
{
if (tn->Type() == TiXmlNode::TINYXML_ELEMENT)
{
pRetData->m_int = (int)tn;
}
}
else
{
pRetData->m_int = 0;
}
}
//添加节点
//int pnode,int cnode
ECMDIMPL(XmlDoc, 11, AddNode)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
TiXmlElement* child = (TiXmlElement*)pArgInf[2].m_int;
if (xe != nullptr && child != nullptr)
{
xe->InsertEndChild(*child);
pRetData->m_bool = TRUE;
}
else {
pRetData->m_bool = FALSE;
}
}
//置属性
//int node,str name,str value
ECMDIMPL(XmlDoc, 12, SetAttribute)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe == nullptr)
{
pRetData->m_bool = FALSE;
return;
}
LPSTR name = (LPSTR)pArgInf[2].m_pText;
LPSTR value = (LPSTR)pArgInf[3].m_pText;
if (xe != nullptr)
{
xe->SetAttribute(name, value);
pRetData->m_bool = TRUE;
}
else
{
pRetData->m_bool = FALSE;
}
}
//插入节点
//int pnode,int cnode,int index
ECMDIMPL(XmlDoc, 13, InstNode)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
TiXmlElement* child = (TiXmlElement*)pArgInf[1].m_int;
if (xe == nullptr || child == nullptr)
{
pRetData->m_bool = FALSE;
return;
}
int index = pArgInf[2].m_int;
TiXmlElement* attr = xe->FirstChildElement();
int i = 0;
if (attr) {
for (TiXmlElement* curr = attr;
curr != nullptr;
curr = curr->NextSiblingElement(), i++)
{
if (i == index)
{
xe->InsertAfterChild(curr, *child);
pRetData->m_bool = TRUE;
return;
}
i++;
}
if (i >= index)
{
xe->LinkEndChild(child);
pRetData->m_bool = TRUE;
return;
}
}
xe->LinkEndChild(child);
pRetData->m_bool = TRUE;
}
//取所有属性
//int node,str[] array
ECMDIMPL(XmlDoc, 14, GetAttributes)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe == nullptr)
{
pRetData->m_int = 0;
return;
}
NotifySys(NRS_FREE_ARY, (DWORD)pArgInf->m_dtDataType, (DWORD)pArgInf[2].m_ppAryData);
TiXmlAttribute* attr = xe->FirstAttribute();
int i = GetAttributeCount(xe);
if (i == 0)
{
pRetData->m_int = 0;
return;
}
i = i * 2;
LPSTR* sarry = (LPSTR*)NotifySys(NRS_MALLOC, (DWORD)((i + 2) * sizeof(int)), 0);
attr = xe->FirstAttribute();
if (attr) {
int j = 2;
for (TiXmlAttribute* curr = attr;
curr != nullptr;
curr = curr->Next(), j += 2)
{
sarry[j] = (LPSTR)curr->Name();
int k = j + 1;
sarry[k] = (LPSTR)curr->Value();
}
}
sarry[0] = (LPSTR)1;
sarry[1] = (LPSTR)i;
*pArgInf[2].m_ppAryData = sarry; // 将新内容写回该数组变量。
pRetData->m_int = i;
}
//取所有子节点
//int node,int[] array
ECMDIMPL(XmlDoc, 15, GetChildNodes)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe == nullptr)
{
pRetData->m_int = 0;
return;
}
TiXmlElement* attr = xe->FirstChildElement();
int i = 0;
if (attr) {
for (TiXmlElement* curr = attr;
curr != nullptr;
curr = curr->NextSiblingElement())
i++;
}
if (i == 0)
{
pRetData->m_int = 0;
return;
}
NotifySys(NRS_FREE_ARY, (DWORD)pArgInf->m_dtDataType, (DWORD)pArgInf[2].m_ppAryData);
int* array = (int*)NotifySys(NRS_MALLOC, (DWORD)((i + 2) * sizeof(int)), 0);
attr = xe->FirstChildElement();
if (attr) {
int j = 2;
for (TiXmlElement* curr = attr;
curr != nullptr;
curr = curr->NextSiblingElement(), j++)
{
array[j] = (int)curr;
}
}
array[0] = 1;
array[1] = i;
*pArgInf[2].m_ppAryData = array;
pRetData->m_int = i;
}
//移除子节点
//int node,int cnode
ECMDIMPL(XmlDoc, 16, RemoveChild)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
TiXmlElement* child = (TiXmlElement*)pArgInf[2].m_int;
if (xe != nullptr && child != nullptr)
{
xe->RemoveChild(child);
pRetData->m_bool = TRUE;
}
else {
pRetData->m_bool = FALSE;
}
}
//移除属性
//int node,str name
ECMDIMPL(XmlDoc, 17, RemoveAttribute)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe == nullptr)
{
pRetData->m_bool = FALSE;
return;
}
LPSTR name = (LPSTR)pArgInf[2].m_pText;
if (xe != nullptr)
{
xe->RemoveAttribute(name);
pRetData->m_bool = TRUE;
}
else {
pRetData->m_bool = FALSE;
}
}
//释放文档
ECMDIMPL(XmlDoc, 18, ReleaseXml)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
EXMLDocument exml = (EXMLDocument)pArgInf[0].m_pCompoundData;
exml->CreateFlag = 0;
delete exml->pXMLDoucument;
pRetData->m_bool = TRUE;
}
//创建节点
//str name
ECMDIMPL(XmlDoc, 19, CreateNode)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
LPSTR name = (LPSTR)pArgInf[1].m_pText;
TiXmlElement* xe = new TiXmlElement(name);
pRetData->m_int = (int)xe;
}
//释放节点
//int node
ECMDIMPL(XmlDoc, 20, ReleaseNode)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe != nullptr)
{
delete xe;
pRetData->m_bool = TRUE;
}
else
{
pRetData->m_bool = FALSE;
}
}
//添加注释
//int node,str value
ECMDIMPL(XmlDoc, 21, AddComment)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
LPSTR value = (LPSTR)pArgInf[2].m_pText;
if (xe != nullptr)
{
TiXmlComment* cmt = new TiXmlComment(value);
xe->InsertEndChild(*cmt);
pRetData->m_bool = TRUE;
}
else
{
pRetData->m_bool = FALSE;
}
}
//取节点值
//int node
ECMDIMPL(XmlDoc, 22, GetNodeValue)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe != nullptr)
{
pRetData->m_pText = (char*)xe->GetText();
}
else {
pRetData->m_pText = (char*)"";
}
}
//取节点名称
//int node
ECMDIMPL(XmlDoc, 23, GetNodeName)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe != nullptr)
{
pRetData->m_pText = (char*)xe->Value();
}
else {
pRetData->m_pText = (char*)"";
}
}
//置节点值
//int node,str val
ECMDIMPL(XmlDoc, 24, SetNodeValue)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
LPSTR value = pArgInf[2].m_pText;
if (xe != nullptr && value != nullptr)
{
TiXmlNode* node = xe->FirstChild();
if (node != nullptr)
{
if (node->Type() == node->TINYXML_TEXT)
{
xe->ReplaceChild(node, TiXmlText(value));
pRetData->m_bool = TRUE;
return;
}
}
else
{
xe->InsertEndChild(TiXmlText(value));
pRetData->m_bool = TRUE;
return;
}
}
pRetData->m_bool = FALSE;
}
//置节点名
//int node,str name
ECMDIMPL(XmlDoc, 25, SetNodeName)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
LPSTR name = pArgInf[2].m_pText;
if (xe != nullptr && name != nullptr)
{
xe->SetValue(name);
pRetData->m_bool = TRUE;
}
else {
pRetData->m_bool = FALSE;
}
}
//包含有属性
//int node
ECMDIMPL(XmlDoc, 26, ContainsAttr)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe != nullptr)
{
if (xe->FirstAttribute() != nullptr)
pRetData->m_bool = TRUE;
}
else {
pRetData->m_bool = FALSE;
}
}
//包含有子级
//int node
ECMDIMPL(XmlDoc, 27, ContainsChild)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
if (xe != nullptr)
{
if (xe->FirstChildElement() != nullptr)
pRetData->m_bool = TRUE;
}
else {
pRetData->m_bool = FALSE;
}
}
//存在属性
//int node,str name
ECMDIMPL(XmlDoc, 28, ExistsAttr)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
LPSTR name = pArgInf[2].m_pText;
if (xe != nullptr && name != nullptr)
{
if (xe->Attribute(name) != nullptr)
pRetData->m_bool = TRUE;
}
else {
pRetData->m_bool = FALSE;
}
}
//存在子节点
//int node,str name
ECMDIMPL(XmlDoc, 29, ExistsChild)(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
TiXmlElement* xe = (TiXmlElement*)pArgInf[1].m_int;
LPSTR name = pArgInf[2].m_pText;
if (xe != nullptr && name != nullptr)
{
if (xe->FirstChildElement(name) != nullptr)
pRetData->m_bool = TRUE;
}
else {
pRetData->m_bool = FALSE;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化