目录
2.xml封装 xml_helper.h xml_helper.c
https://codeload.github.com/michaelrsweet/mxml/zip/refs/tags/v3.2
#ifndef _XML_HELPER_H_
#define _XML_HELPER_H_
#pragma comment(lib,"ws2_32.lib")
#define int8 char
#define uint8 unsigned char
#define int16 short
#define uint16 unsigned short
#define int32 int
#define uint32 unsigned int
#define int64 long long
#define uint64 unsigned long long
#define FALSE 0
#define TRUE 1
#include "mxml.h"
/**************************************************************************/
/**********************添加新节点或者新属性的接口**************************/
/**************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
//函数:
//作用:创建根节点
mxml_node_t* XmlCreateRootNode();
#define CreateRootNode() \
XmlCreateRootNode()
//函数
//作用:删除节点
void XmlDeleteNode(mxml_node_t *node);
#define DeleteNode(node) \
XmlDeleteNode(node)
//函数
//作用:解析
// rootNode = CreateRootNode();
mxml_node_t* XmlParseNode(const char *pStr);
#define ParseNode(pStr) \
XmlParseNode(pStr)
//函数
//作用:打印节点数据 需要手动释放free
char* XmlPrintNode(mxml_node_t *node);
#define PrintNode(node) \
XmlPrintNode(node)
//函数: XmlAddNode
//作用: 添加一个节点
int XmlAddNode(mxml_node_t *ptFather, const int8 *szElemName, mxml_node_t **pptNewNode);
#define AddNode(ptFather, szElemName, pptNewNode) \
XmlAddNode(ptFather, szElemName, pptNewNode)
//函数:XmlElemSetAttrInt
//作用:为节点设置s32类型的属性
int XmlElemSetAttrInt(mxml_node_t *ptNode, const int8 *szAttrName, int nAttrValue);
#define SetAttrInt(ptNode, szAttrName, nAttrValue) \
XmlElemSetAttrInt(ptNode, szAttrName, nAttrValue)
//函数:XmlElemSetAttrU32
//作用:为节点设置u32类型的属性
int XmlElemSetAttrU32(mxml_node_t *ptNode, const int8 *szAttrName, uint32 dwAttrValue);
#define SetAttrU32(ptNode, szAttrName, dwAttrValue) \
XmlElemSetAttrU32(ptNode, szAttrName, dwAttrValue)
int XmlElemSetAttrStr(mxml_node_t *ptNode, const int8 *szAttrName, int8 *attr);
#define SetAttrStr(ptNode, szAttrName, attr) \
XmlElemSetAttrStr(ptNode, szAttrName, attr)
int XmlSetNodeStr(mxml_node_t *pNode, const int8 *szValue);
#define SetNodeStr(pNode, szValue) \
XmlSetNodeStr(pNode, szValue)
int XmlSetNodeInt(mxml_node_t *pNode, int nValue);
#define SetNodeInt(pNode, nValue) \
XmlSetNodeInt(pNode, nValue)
int XmlGetNodeStr(mxml_node_t *pNode, const int8 *szValue);
#define GetNodeStr(pNode, szValue) \
XmlGetNodeStr(pNode, szValue)
int XmlGetNodeInt(mxml_node_t *pNode, int* nValue);
#define GetNodeInt(pNode, nValue) \
XmlGetNodeInt(pNode, nValue)
int XmlElemGetAttrStr(mxml_node_t *ptNode, const int8 *szAttrName, int8 *attr);
#define GetAttrStr(ptNode, szAttrName, attr) \
XmlElemGetAttrStr(ptNode, szAttrName, attr)
//函数:XmlAddElemStr
//作用:在父节点ptRoot下面,生成一个节点,它的值为字符串类型。
int XmlAddElemStr(mxml_node_t *ptRoot, const int8 *szElemName, const int8 *szValue, mxml_node_t **pptElem);
#define AddElemStr(ptRoot, szElemName, szValue, pptElem) \
XmlAddElemStr(ptRoot, szElemName, szValue, pptElem)
//函数:XmlAddElemInt
//作用:在父节点ptRoot下面,生成一个节点,它的值为int类型。
int XmlAddElemInt(mxml_node_t *ptRoot, const int8 *szElemName, int nValue, mxml_node_t **pptElem);
#define AddElemInt(ptRoot, szElemName, nValue, pptElem) \
XmlAddElemInt(ptRoot, szElemName, nValue, pptElem)
int XmlAddElemU8(mxml_node_t *ptRoot, const int8 *szElemName, uint8 wValue, mxml_node_t **pptElem);
//函数:XmlAddElemU16
//作用:在父节点ptRoot下面,生成一个节点,它的值为u32类型。
int XmlAddElemU16(mxml_node_t *ptRoot, const int8 *szElemName, uint16 wValue, mxml_node_t **pptElem);
#define AddElemU16(ptRoot, szElemName, wValue, pptElem) \
XmlAddElemU16(ptRoot, szElemName, wValue, pptElem)
//函数:XmlAddElemU32
//作用:在父节点ptRoot下面,生成一个节点,它的值为u32类型。
int XmlAddElemU32(mxml_node_t *ptRoot, const int8 *szElemName, uint32 dwValue, mxml_node_t **pptElem);
#define AddElemU32(ptRoot, szElemName, dwValue, pptElem) \
XmlAddElemU32(ptRoot, szElemName, dwValue, pptElem)
//函数: XmlAddElemU64
//作用: 在父节点ptRoot下面,生成一个节点,它的值是u64类型的。
int XmlAddElemU64(mxml_node_t *ptRoot, const int8 *szElemName, uint64 qwValue, mxml_node_t **pptElem);
#define AddElemU64(ptRoot, szElemName, qwValue, pptElem) \
XmlAddElemU64(ptRoot, szElemName, qwValue, pptElem)
//函数:XmlAddElemDouble
//作用:在父节点ptRoot下面,生成一个节点,它的值为double类型。
int XmlAddElemDouble(mxml_node_t *ptRoot, const int8 *szElemName, double fValue);
//函数: XmlAddElemBool
//作用: 在父节点ptRoot下面,生成一个节点,它的值是int型。
int XmlAddElemBool(mxml_node_t *ptRoot, const int8 *szElemName, int bValue, mxml_node_t **pptElem);
#define AddElemBool(ptRoot, szElemName, bValue, pptElem) \
XmlAddElemBool(ptRoot, szElemName, bValue, pptElem)
//函数: XmlAddElemCDATA
//作用: 在父节点ptRoot下面,生成一个节点,它的值为CDATA类型。
int XmlAddElemCDATA(mxml_node_t *ptRoot, const int8 *szElemName, const int8 *szData, mxml_node_t **pptElem);
#define AddElemCDATA(ptRoot, szElemName, szData, pptElem) \
XmlAddElemCDATA(ptRoot, szElemName, szData, pptElem)
/**************************************************************************/
/**********************获取子节点中内容的接口******************************/
/**************************************************************************/
///XmlGetFirstChildWithName
///作用:找到ptParent的第一个名为szName的孩子节点
mxml_node_t *XmlGetFirstChildWithName(mxml_node_t *ptParent, const int8 *szName);
#define GetFirstChildWithName(ptParent, szName) \
XmlGetFirstChildWithName(ptParent, szName)
//函数名: XmlGetNextSiblingWithName
//作用: 找到ptNode的下一个名字为szName的兄弟节点
mxml_node_t *XmlGetNextSiblingWithName(mxml_node_t *ptNode, const int8 *szName);
#define GetNextSiblingWithName(ptNode, szName) \
XmlGetNextSiblingWithName(ptNode, szName)
int XmlGetSonNode(mxml_node_t *ptParent, const int8 *szNodeName, mxml_node_t **pptSonNode);
#define GetSonNode(ptParent, szNodeName, pptSonNode) \
XmlGetSonNode(ptParent, szNodeName, pptSonNode)
int XmlGetSonNodeValue_CData(mxml_node_t *ptParent, const int8 *szNodeName, const int8 **pszText);
int XmlGetSonNodeValue_Str(mxml_node_t *ptParent, const int8 *szNodeName, const int8 **szText);
#define GetSonNodeValue_Str(ptParent, szNodeName, szText) \
XmlGetSonNodeValue_Str(ptParent, szNodeName, szText)
//函数: XmlGetSonNodeValue_Str_Copy
//作用: 获取ptRoot的指定子节点的字符串值的备份
int XmlGetSonNodeValue_Str_Copy(mxml_node_t *ptRoot, const int8 *szSonNodeName, int8 *szBuf, uint32 dwBufSize);
#define GetSonNodeValue_Str_Copy(ptRoot, szSonNodeName, szBuf, dwBufSize) \
XmlGetSonNodeValue_Str_Copy(ptRoot, szSonNodeName, szBuf, dwBufSize)
int XmlGetSonNodeValue_Int(mxml_node_t *ptParent, const int8 *szNodeName, int *pnValue);
#define GetSonNodeValue_Int(ptParent, szNodeName, pnValue) \
XmlGetSonNodeValue_Int(ptParent, szNodeName, pnValue)
int XmlGetSonNodeValue_u8(mxml_node_t *ptParent, const int8 *szNodeName, uint8 *byValue);
#define GetSonNodeValue_u8(ptParent, szNodeName, byValue) \
XmlGetSonNodeValue_u8(ptParent, szNodeName, byValue)
int XmlGetSonNodeValue_u16(mxml_node_t *ptParent, const int8 *szNodeName, uint16 *pwValue);
#define GetSonNodeValue_u16(ptParent, szNodeName, pwValue) \
XmlGetSonNodeValue_u16(ptParent, szNodeName, pwValue)
int XmlGetSonNodeValue_u32(mxml_node_t *ptParent, const int8 *szNodeName, uint32 *pdwValue);
#define GetSonNodeValue_u32(ptParent, szNodeName, pdwValue) \
XmlGetSonNodeValue_u32(ptParent, szNodeName, pdwValue)
int XmlGetSonNodeValue_u64(mxml_node_t *ptParent, const int8 *szNodeName, uint64 *pqwValue);
#define GetSonNodeValue_u64(ptParent, szNodeName, pqwValue) \
XmlGetSonNodeValue_u64(ptParent, szNodeName, pqwValue)
int XmlGetSonNodeValue_Bool(mxml_node_t *ptParent, const int8 *szNodeName, int *pbValue);
#define GetSonNodeValue_Bool(ptParent, szNodeName, pbValue) \
XmlGetSonNodeValue_Bool(ptParent, szNodeName, pbValue)
/**************************************************************************/
/**********************获取指定节点的内容的接口****************************/
/**************************************************************************/
int XmlGetNodeValue_str(mxml_node_t *ptNode, const int8 **pszStr);
#define GetNodeValue_str(ptNode, pszStr) \
XmlGetNodeValue_str(ptNode, pszStr)
int XmlGetNodeValue_u32(mxml_node_t *ptNode, uint32 *pdwValue);
#define GetNodeValue_u32(ptNode, pdwValue) \
XmlGetNodeValue_u32(ptNode, pdwValue)
int XmlGetNodeValue_u16(mxml_node_t *ptNode, uint16 *pwValue);
#define GetNodeValue_u16(ptNode, pwValue) \
XmlGetNodeValue_u16(ptNode, pwValue)
/**************************************************************************/
/**********************获取指定节点的属性的接口****************************/
/**************************************************************************/
int XmlGetAttr_U32(mxml_node_t *ptNode, const int8 *szAttrName, uint32 *pdwValue);
#define GetAttr_U32(ptNode, szAttrName, pdwValue) \
XmlGetAttr_U32(ptNode, szAttrName, pdwValue)
int XmlGetAttr_Int(mxml_node_t *ptNode, const int8 *szAttrName, int *pnValue);
#define GetAttr_Int(ptNode, szAttrName, pnValue) \
XmlGetAttr_Int(ptNode, szAttrName, pnValue)
int XmlGetAttr_U16(mxml_node_t *ptNode, const int8 *szAttrName, uint16 *pwValue);
#define GetAttr_U16(ptNode, szAttrName, pwValue) \
XmlGetAttr_U16(ptNode, szAttrName, pwValue)
int XmlGetAttr_U8(mxml_node_t *ptNode, const int8 *szAttrName, uint8 *pbyValue);
#define GetAttr_U8(ptNode, szAttrName, pbyValue) \
XmlGetAttr_U8(ptNode, szAttrName, pbyValue)
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif //_XML_HELPER_H_
#include <stdio.h>
#include <string.h>
#include "xml_helper.h"
static int Strncmp(int8 *strSource, int8 *strcmpValue, int size)
{
if (size <= 0 || strlen(strSource) <= 0 || strlen(strcmpValue) <= 0)
{
return -1;
}
int8 *pTmpArr = (int8 *)malloc((size + 1) * sizeof(int8));
memset(pTmpArr, 0, size + 1);
memcpy(pTmpArr, strSource, size);
int nRet = strcmp(pTmpArr, strcmpValue);
if (pTmpArr)
{
free(pTmpArr);
pTmpArr = NULL;
}
return nRet;
}
mxml_type_t _MxmlType(mxml_node_t *node)
{
return MXML_OPAQUE;
}
mxml_node_t* XmlParseNode(const char *pStr)
{
#if 0
mxml_node_t *rootNode = mxmlNewXML("1.0");
if (rootNode)
{
mxmlSAXLoadString(rootNode, pStr, _MxmlType, MXML_NO_CALLBACK, NULL);
}
#else
mxml_node_t *rootNode = mxmlLoadString(NULL, pStr, MXML_OPAQUE_CALLBACK);
#endif
return rootNode;
}
mxml_node_t* XmlCreateRootNode()
{
return mxmlNewXML("1.0");
}
void XmlDeleteNode(mxml_node_t *node)
{
if (node)
{
mxmlDelete(node);
}
}
char* XmlPrintNode(mxml_node_t *node)
{
/*输出没有需加上<?xml version=\"1.0\" encoding=\"utf-8\"?>*/
char sTmpArr[4096] = { 0 };
mxmlSaveString(node, sTmpArr, 4096, MXML_NO_CALLBACK);
int len = strlen(sTmpArr);
char *newArr = (char*)malloc(len + 128);
if (!newArr)
{
return NULL;
}
//判断是否有xml version之类的
if (Strncmp(sTmpArr, "<?xml", 5) != 0)
{
sprintf(newArr, "%s\n%s", "<?xml version=\"1.0\" encoding=\"utf-8\"?>", sTmpArr);
}
else
{
sprintf(newArr, "%s", sTmpArr);
}
return newArr;
}
int XmlAddNode(mxml_node_t *ptFather, const int8 *szNodeName, mxml_node_t **pptNewNode)
{
mxml_node_t *ptNode = NULL;
if (NULL == ptFather || NULL == szNodeName || NULL == pptNewNode)
{
printf("ptFather %p, szNodeName %p{%s}, pptNewNode %p\n",
ptFather, szNodeName, (szNodeName ? szNodeName : ""), pptNewNode);
return -1;
}
ptNode = mxmlNewElement(ptFather, szNodeName);
if (NULL == ptNode)
{
printf("mxmlNewElement() failed, {%s}\n", szNodeName);
return -1;
}
*pptNewNode = ptNode;
return 0;
}
int XmlElemSetAttrInt(mxml_node_t *ptNode, const int8 *szAttrName, int nAttrValue)
{
int8 szBuf[64] = {0};
sprintf(szBuf, "%d", nAttrValue);
//mxmlElementSetAttr没有返回值,但是,根据源码,它是有可能失败的
mxmlElementSetAttr(ptNode, szAttrName, szBuf);
return 0;
}
int XmlElemSetAttrStr(mxml_node_t *ptNode, const int8 *szAttrName, int8 *attr)
{
//mxmlElementSetAttr没有返回值,但是,根据源码,它是有可能失败的
mxmlElementSetAttr(ptNode, szAttrName, attr);
return 0;
}
int XmlElemSetAttrU32(mxml_node_t *ptNode, const int8 *szAttrName, uint32 dwAttrValue)
{
int8 szBuf[64] = {0};
sprintf(szBuf, "%lu", dwAttrValue);
//mxmlElementSetAttr没有返回值,但是,根据源码,它是有可能失败的
mxmlElementSetAttr(ptNode, szAttrName, szBuf);
return 0;
}
int XmlSetNodeStr(mxml_node_t *pNode, const int8 *szValue)
{
mxml_node_t *ptText = mxmlNewText(pNode, 0, szValue);
if (NULL == ptText)
{
printf("mxmlNewText() failed, value{%s}\n", szValue);
return -1;
}
return 0;
}
int XmlSetNodeInt(mxml_node_t *pNode, int nValue)
{
int8 szBuf[64] = {0};
sprintf(szBuf, "%d", nValue);
mxml_node_t *ptText = mxmlNewText(pNode, 0, szBuf);
//ptText->type = MXML_OPAQUE;
if (NULL == ptText)
{
printf("mxmlNewText() failed, value{%s}\n", szBuf);
return -1;
}
return 0;
}
int XmlGetNodeStr(mxml_node_t *pNode, const int8 *szValue)
{
if (!pNode)
{
return -1;
}
return 0;
}
int XmlGetNodeInt(mxml_node_t *pNode, int* nValue)
{
if (!pNode)
{
return -1;
}
char szValue[64] = { 0 };
sprintf(szValue, "%s", pNode->child->value.opaque);
*nValue = atoi(szValue);
return 0;
}
int XmlElemGetAttrStr(mxml_node_t *ptNode, const int8 *szAttrName, int8 *attr)
{
if (!ptNode)
{
return -1;
}
char *pValue = mxmlElementGetAttr(ptNode, szAttrName);
sprintf(attr, "%s", pValue);
return 0;
}
int XmlAddElemStr(mxml_node_t *ptRoot, const int8 *szElemName, const int8 *szValue, mxml_node_t **pptElem)
{
mxml_node_t *ptElem = NULL;
mxml_node_t *ptText = NULL;
ptElem = mxmlNewElement(ptRoot, szElemName);
if (NULL == ptElem)
{
printf("mxmlNewElement() failed, {%s}\n", szElemName);
return -1;
}
ptText = mxmlNewText(ptElem, 0, pTmpArr);
if (NULL == ptText)
{
printf("mxmlNewText() failed, Elem{%s}, value{%s}\n", szElemName, szValue);
return -1;
}
if (pptElem)
{
*pptElem = ptElem;
}
return 0;
}
int XmlAddElemInt(mxml_node_t *ptRoot, const int8 *szElemName, int nValue, mxml_node_t **pptElem)
{
int8 szBuf[64] = {0};
sprintf(szBuf, "%d", nValue);
return XmlAddElemStr(ptRoot, szElemName, szBuf, pptElem);
}
//自己加的
int XmlAddElemU8(mxml_node_t *ptRoot, const int8 *szElemName, uint8 wValue, mxml_node_t **pptElem)
{
int8 szBuf[64] = {0};
sprintf(szBuf, "%u", wValue);
return XmlAddElemStr(ptRoot, szElemName, szBuf, pptElem);
}
int XmlAddElemU16(mxml_node_t *ptRoot, const int8 *szElemName, uint16 wValue, mxml_node_t **pptElem)
{
int8 szBuf[64] = {0};
sprintf(szBuf, "%u", wValue);
return XmlAddElemStr(ptRoot, szElemName, szBuf, pptElem);
}
int XmlAddElemU32(mxml_node_t *ptRoot, const int8 *szElemName, uint32 dwValue, mxml_node_t **pptElem)
{
int8 szBuf[64] = {0};
sprintf(szBuf, "%lu", dwValue);
return XmlAddElemStr(ptRoot, szElemName, szBuf, pptElem);
}
int XmlAddElemU64(mxml_node_t *ptRoot, const int8 *szElemName, uint64 qwValue, mxml_node_t **pptElem)
{
int8 szBuf[64] = {0};
sprintf(szBuf, "%llu", qwValue);
return XmlAddElemStr(ptRoot, szElemName, szBuf, pptElem);
}
int XmlAddElemDouble(mxml_node_t *ptRoot, const int8 *szElemName, double fValue)
{
int8 szBuf[64] = {0};
sprintf(szBuf, "%lf", fValue);
return XmlAddElemStr(ptRoot, szElemName, szBuf, NULL);
}
int XmlAddElemBool(mxml_node_t *ptRoot, const int8 *szElemName, int bValue, mxml_node_t **pptElem)
{
const int8 *szStr = (FALSE == bValue ? "false" : "true");
return XmlAddElemStr(ptRoot, szElemName, szStr, pptElem);
}
int XmlAddElemCDATA(mxml_node_t *ptRoot, const int8 *szElemName, const int8 *szData, mxml_node_t **pptElem)
{
mxml_node_t *ptElem = NULL;
mxml_node_t *ptCDATA = NULL;
if (NULL == ptRoot || NULL == szElemName || NULL == szData)
{
return -1;
}
ptElem = mxmlNewElement(ptRoot, szElemName);
if (NULL == ptElem)
{
return -1;
}
ptCDATA = mxmlNewCDATA(ptElem, szData);
if (NULL == ptCDATA)
{
return -1;
}
if (pptElem)
{
*pptElem = ptElem;
}
return 0;
}
mxml_node_t *XmlGetFirstChildWithName(mxml_node_t *ptParent, const int8 *szName)
{
mxml_node_t *ptChild = NULL;
ptChild = mxmlGetFirstChild(ptParent);
///1 没有任何孩子节点
if (NULL == ptChild)
{
return NULL;
}
///2 第1个孩子节点正是期望的
if (MXML_ELEMENT == mxmlGetType(ptChild) &&
0 == strcmp(szName, mxmlGetElement(ptChild)))
{
return ptChild;
}
///3 第1个孩子节点不是期望的
return XmlGetNextSiblingWithName(ptChild, szName);
}
mxml_node_t *XmlGetNextSiblingWithName(mxml_node_t *ptNode, const int8 *szName)
{
mxml_node_t *ptSibling = NULL;
if (NULL == ptNode || NULL == szName)
{
return NULL;
}
ptSibling = mxmlGetNextSibling(ptNode);
while (ptSibling)
{
if (MXML_ELEMENT == mxmlGetType(ptSibling) && 0 == strcmp(szName, mxmlGetElement(ptSibling)))
{
return ptSibling;
}
ptSibling = mxmlGetNextSibling(ptSibling);
}
return NULL;
}
int XmlGetSonNode(mxml_node_t *ptParent, const int8 *szNodeName, mxml_node_t **pptSonNode)
{
mxml_node_t *ptNode = NULL;
ptNode = mxmlFindElement(ptParent, ptParent, szNodeName, NULL, NULL, MXML_DESCEND);
if (NULL == ptNode)
{
printf("mxmlFindElement() failed, name{%s}\n", szNodeName);
return -1;
}
*pptSonNode = ptNode;
return 0;
}
int XmlGetSonNodeValue_CData(mxml_node_t *ptParent, const int8 *szNodeName, const int8 **pszText)
{
mxml_node_t *ptElem = NULL;
mxml_node_t *ptCData = NULL;
const int8 *szText = NULL;
ptElem = mxmlFindElement(ptParent, ptParent, szNodeName, NULL, NULL, MXML_DESCEND);
if (NULL == ptElem)
{
printf("do not find element. {%s}\n", szNodeName);
return -1;
}
ptCData = mxmlGetFirstChild(ptElem);
while (NULL != ptCData)
{
if (MXML_ELEMENT == ptCData->type && 0 == Strncmp(ptCData->value.element.name, "![CDATA[", 8))
{
break;
}
ptCData = mxmlGetNextSibling(ptCData);
}
if (NULL == ptCData)
{
//当没有找到CData节点时,认为是清空
//注意: 之所以返回"]]",是因为mxml解析出来的cdata数据是以"]]"结尾的
*pszText = "]]";
return 0;
}
szText = mxmlGetCDATA(ptCData);
if (NULL == szText)
{
return -1;
}
*pszText = szText;
return 0;
}
//函数:_GetNodeValue
//作用:在ptRoot的子节点中找到名为szNodeName的节点,并将它的值通过pszOut返回。
//注意:pszOut不需要调用者释放。
int XmlGetSonNodeValue_Str(mxml_node_t *ptParent, const int8 *szNodeName, const int8 **szText)
{
mxml_node_t *ptText = NULL;
ptText = mxmlFindPath(ptParent, szNodeName);
if (NULL == ptText)
{
printf("mxmlFindPath() failed, {%s}\n", szNodeName);
return -1;
}
if (MXML_ELEMENT == ptText->type)
{ ///说明节点是空的,认为内容是""
*szText = "";
return 0;
}
if (MXML_OPAQUE != ptText->type)
{
printf("node{%s} is NOT MXML_OPAQUE\n", szNodeName);
return -1; ///这种情况,例如:<owner></owner>
}
if (NULL == ptText->value.opaque)
{
printf("node{%s} value is NULL\n", szNodeName);
return -1;
}
*szText = ptText->value.opaque;
return 0;
}
int XmlGetSonNodeValue_Str_Copy(mxml_node_t *ptRoot, const int8 *szSonNodeName, int8 *szBuf, uint32 dwBufSize)
{
int eCgiRet = 0;
const int8 *szText = NULL;
if (NULL == ptRoot || NULL == szSonNodeName || NULL == szBuf || 0 == dwBufSize)
{
return -1;
}
eCgiRet = XmlGetSonNodeValue_Str(ptRoot, szSonNodeName, &szText);
if (0 != eCgiRet)
{
printf("XmlGetSonNodeValue_Str() failed %d, szSonNodeName{%s}\n", eCgiRet, szSonNodeName);
return eCgiRet;
}
if (strlen(szText) >= dwBufSize)
{
printf("buffer too small, len %d, dwBufSize %lu\n", strlen(szText), dwBufSize);
return -1;
}
sprintf(szBuf, "%s", szText);
return 0;
}
int XmlGetSonNodeValue_Int(mxml_node_t *ptParent, const int8 *szNodeName, int *pnValue)
{
int eCgiRet = 0;
const int8 *szText = NULL;
int nNum = 0;
int bOverFlow = FALSE;
if (NULL == ptParent || NULL == szNodeName || NULL == pnValue)
{
return -1;
}
eCgiRet = XmlGetSonNodeValue_Str(ptParent, szNodeName, &szText);
if (0 != eCgiRet)
{
return eCgiRet;
}
if (0 == strcmp("true", szText))
{
*pnValue = 1;
return 0;
}
if (0 == strcmp("false", szText))
{
*pnValue = 0;
return 0;
}
nNum = atoi(szText);
*pnValue = nNum;
return 0;
}
int XmlGetSonNodeValue_u32(mxml_node_t *ptParent, const int8 *szNodeName, uint32 *pdwValue)
{
int eCgiRet = 0;
const int8 *szText = NULL;
uint32 dwNum = 0;
int bOverFlow = FALSE;
if (NULL == ptParent || NULL == szNodeName || NULL == pdwValue)
{
return -1;
}
eCgiRet = XmlGetSonNodeValue_Str(ptParent, szNodeName, &szText);
if (0 != eCgiRet)
{
return eCgiRet;
}
dwNum = atol(szText);
*pdwValue = dwNum;
return 0;
}
int XmlGetSonNodeValue_u64(mxml_node_t *ptParent, const int8 *szNodeName, uint64 *pqwValue)
{
int eCgiRet = 0;
const int8 *szText = NULL;
uint64 qwNum = 0;
int bOverFlow = FALSE;
if (NULL == ptParent || NULL == szNodeName || NULL == pqwValue)
{
return -1;
}
eCgiRet = XmlGetSonNodeValue_Str(ptParent, szNodeName, &szText);
if (0 != eCgiRet)
{
return eCgiRet;
}
qwNum = atoll(szText);
*pqwValue = qwNum;
return 0;
}
int XmlGetSonNodeValue_Bool(mxml_node_t *ptParent, const int8 *szNodeName, int *pbValue)
{
int eCgiRet = 0;
const int8 *szText = NULL;
int bValue = FALSE;
eCgiRet = XmlGetSonNodeValue_Str(ptParent, szNodeName, &szText);
if (0 != eCgiRet)
{
printf("XmlGetSonNodeValue_Str() failed %d\n", eCgiRet);
return eCgiRet;
}
if (0 == strcmp("true", szText))
{
bValue = TRUE;
}
else if (0 == strcmp("false", szText))
{
bValue = FALSE;
}
else
{
printf("unknown text{%s}, node{%s}\n", szText, szNodeName);
return -1;
}
if (NULL != pbValue)
{
*pbValue = bValue;
}
return 0;
}
int XmlGetSonNodeValue_u8(mxml_node_t *ptParent, const int8 *szNodeName, uint8 *pbyValue)
{
int eCgiRet = 0;
int nValue = 0;
if (NULL == pbyValue)
{
return -1;
}
eCgiRet = XmlGetSonNodeValue_Int(ptParent, szNodeName, &nValue);
if (0 != eCgiRet)
{
return eCgiRet;
}
if (nValue < 0 || nValue > (uint8)-1)
{
printf("invalid value: %d\n", nValue);
return -1;
}
*pbyValue = nValue;
return 0;
}
int XmlGetSonNodeValue_u16(mxml_node_t *ptParent, const int8 *szNodeName, uint16 *pwValue)
{
int eCgiRet = 0;
int nValue = 0;
if (NULL == pwValue)
{
return -1;
}
eCgiRet = XmlGetSonNodeValue_Int(ptParent, szNodeName, &nValue);
if (0 != eCgiRet)
{
return eCgiRet;
}
if (nValue < 0 || nValue > (uint16)-1)
{
printf("invalid value: %d\n", nValue);
return -1;
}
*pwValue = nValue;
return 0;
}
int XmlGetNodeValue_str(mxml_node_t *ptNode, const int8 **pszStr)
{
const int8 *szText = NULL;
szText = mxmlGetOpaque(ptNode);
if (NULL == szText)
{
*pszStr = ""; ///拿不到认为节点是空的
}
else
{
*pszStr = szText;
}
return 0;
}
int XmlGetNodeValue_u32(mxml_node_t *ptNode, uint32 *pdwValue)
{
const int8 *szStr = NULL;
int eCgiRet = 0;
uint32 dwNum = 0;
int bOverFlow = FALSE;
eCgiRet = XmlGetNodeValue_str(ptNode, &szStr);
if (0 != eCgiRet)
{
printf("XmlGetNodeValue_u32() failed %d\n", eCgiRet);
return eCgiRet;
}
dwNum = atol(szStr);
*pdwValue = dwNum;
return 0;
}
int XmlGetNodeValue_u16(mxml_node_t *ptNode, uint16 *pwValue)
{
const int8 *szStr = NULL;
int eCgiRet = 0;
uint32 dwNum = 0;
int bOverFlow = FALSE;
eCgiRet = XmlGetNodeValue_str(ptNode, &szStr);
if (0 != eCgiRet)
{
printf("XmlGetNodeValue_u16() failed %d\n", eCgiRet);
return eCgiRet;
}
dwNum = atoi(szStr);
if (dwNum > (uint32)0xFFFF)
{
printf("node value too large for uint16, dwNum %lu\n", dwNum);
return -1;
}
*pwValue = dwNum;
return 0;
}
int XmlGetAttr_U32(mxml_node_t *ptNode, const int8 *szAttrName, uint32 *pdwValue)
{
const int8 *szValue = NULL;
szValue = mxmlElementGetAttr(ptNode, szAttrName);
if (NULL == szValue)
{
printf("get attr failed, name {%s}\n", szAttrName);
return -1;
}
*pdwValue = atol(szValue);
return 0;
}
int XmlGetAttr_Int(mxml_node_t *ptNode, const int8 *szAttrName, int *pnValue)
{
const int8 *szValue = NULL;
szValue = mxmlElementGetAttr(ptNode, szAttrName);
if (NULL == szValue)
{
printf("get attr failed, name {%s}\n", szAttrName);
return -1;
}
*pnValue = atoi(szValue);
return 0;
}
int XmlGetAttr_U16(mxml_node_t *ptNode, const int8 *szAttrName, uint16 *pwValue)
{
int eCgiRet = 0;
int nValue = 0;
eCgiRet = XmlGetAttr_Int(ptNode, szAttrName, &nValue);
if (0 != eCgiRet)
{
return eCgiRet;
}
if (nValue < 0 || nValue > 65535)
{
printf("value %d, is not in [0, 65535]\n", nValue);
return -1;
}
*pwValue = nValue;
return 0;
}
int XmlGetAttr_U8(mxml_node_t *ptNode, const int8 *szAttrName, uint8 *pbyValue)
{
int eCgiRet = 0;
int nValue = 0;
eCgiRet = XmlGetAttr_Int(ptNode, szAttrName, &nValue);
if (0 != eCgiRet)
{
return eCgiRet;
}
if (nValue < 0 || nValue > 255)
{
printf("value %d, is not in [0, 255]\n", nValue);
return -1;
}
*pbyValue = nValue;
return 0;
}
#include <stdio.h>
#include "xml_helper.h"
/*
<?xml version="1.0" encoding="utf-8" ?>
<root>
<AAA>344</AAA>
<part id = "01" option="abbb">
<name>张三</name>
<age>20</age>
<sex>男</sex>
</part>
<listTest>
<test>
<x>1</x>
</test>
<test>
<x>2</x>
</test>
<listTest>
<arrList>
<arr>
<xxx>abc</xxx>
<yyy>1</yyy>
<arr>
<arr>
<xxx>cde</xxx>
<yyy>2</yyy>
<arr>
</arrList>
</root>
*/
int main()
{
mxml_node_t *treeNode = CreateRootNode();
mxml_node_t *rootNode = NULL;
mxml_node_t *partNode = NULL;
mxml_node_t *nameNode = NULL;
mxml_node_t *ageNode = NULL;
mxml_node_t *sexNode = NULL;
mxml_node_t *listTestNode = NULL;
mxml_node_t *arrListNode = NULL;
mxml_node_t *testNode = NULL;
mxml_node_t *arrNode = NULL;
mxml_node_t *AANode = NULL;
AddNode(treeNode, "root", &rootNode);
AddNode(rootNode, "part", &partNode);
AddElemStr(rootNode, "AA", "344", NULL);
AddNode(rootNode, "listTest", &listTestNode);
AddNode(rootNode, "arrList", &arrListNode);
SetAttrStr(partNode, "id", "01");
SetAttrStr(partNode, "option", "abbb");
AddElemStr(partNode, "name", "张三", &nameNode);
AddElemStr(partNode, "sex", "male", &sexNode);
AddElemInt(partNode, "age", 20, &ageNode);
for (int i = 0; i < 2; i++)
{
AddElemInt(listTestNode, "test", i + 1, NULL); //x
}
char tmpArr[100];
for (int i = 0; i < 2; i++)
{
sprintf(tmpArr, "xvdwerwer_%d", i + 1);
AddNode(arrListNode, "arr", &arrNode);
AddElemStr(arrNode, "xxx", tmpArr, NULL);
AddElemInt(arrNode, "yyy", i+1, NULL);
}
char *pSt = XmlPrintNode(rootNode);
DeleteNode(rootNode);
//解析------------
mxml_node_t* pRoot = ParseNode(pSt);
char *pSt2 =PrintNode(pRoot);
rootNode = NULL;
listTestNode = NULL;
testNode = NULL;
int nRet = 0;
nRet = GetSonNode(pRoot, "root", &rootNode);
if (nRet != 0)
{
return -1;
}
//获取属性
partNode = NULL;
GetSonNode(rootNode, "part", &partNode);
char sId[100] = { 0 };
if (!AANode)
{
GetAttrStr(partNode, "id", sId);
}
//解析listTest
nRet = GetSonNode(rootNode, "listTest", &listTestNode);
if (nRet != 0)
{
return -1;
}
testNode = GetFirstChildWithName(listTestNode, "test");
while (testNode)
{
int a;
GetNodeInt(testNode, &a);
testNode = GetNextSiblingWithName(testNode, "test");
}
//解析arrList
nRet = GetSonNode(rootNode, "arrList", &arrListNode);
if (nRet != 0)
{
return -1;
}
arrNode = GetFirstChildWithName(arrListNode, "arr");
char sTmp[100];
while (arrNode)
{
memset(sTmp, 0, 100);
GetSonNodeValue_Str_Copy(arrNode, "xxx", sTmp, 100);
int a;
GetSonNodeValue_Int(arrNode, "yyy", &a);
arrNode = GetNextSiblingWithName(arrNode, "arr");
}
DeleteNode(pRoot);
return 0;
}
解决方案:https://blog.csdn.net/qq_16628589/article/details/118548539