1.声明:
TiXmlDocument *m_pXml; //<!-- Xml parse -->
m_pXml = new TiXmlDocument;
最好用动态生成.
2.加载xml文件:
1.xml:
<?xml version="1.0" encoding="GB2312" standalone="yes"?>
<author name="hgy413" sex="man">
text is hgy
<childItem1 childname1="hgy">
child text is hgy
</childItem1>
<childItem2>
</childItem2>
</author>
//<!-- new codes -->
wchar_t wcPath[MAX_PATH] = {0};
::GetModuleFileName(NULL, wcPath, MAX_PATH);
//<!-- convert unicode to asii -->
int nLen = WideCharToMultiByte(CP_ACP, 0, wcPath, -1, 0, NULL, NULL, NULL);
char *pPath = (char *)::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nLen);
::ZeroMemory(pPath, nLen);
WideCharToMultiByte(CP_ACP, 0, wcPath, -1, pPath, nLen, NULL, NULL);
//<!-- cut \\ tail -->
for (int i=nLen-1; i>0; i--)
{
if ('\\' == pPath[i])
{
break;
}
else
{
pPath[i] = '\0';
}
}
char FileName[] = "1.xml";
strcat(pPath, FileName);
//-----------------------------------------
// 判断XML是否被加载成功,不然清掉对象
//-----------------------------------------
if (!m_pXml->LoadFile(pPath))
{
delete m_pXml;
m_pXml = NULL;
}
3.解析:
1 TiXmlDocument:文档类,它代表了整个xml文件。
2TiXmlDeclaration:声明类,它表示文件的声明部分。
3 TiXmlComment:注释类,它表示文件的注释部分。
4TiXmlElement:元素类,它是文件的主要部分,并且支持嵌套结构,一般使用这种结构来分类的存储信息,它可以包含属性类和文本类。
5 TiXmlAttribute/TiXmlAttributeSet:元素属性,它一般嵌套在元素中,用于记录此元素的一些属性。
6 TiXmlText:文本对象,它嵌套在某个元素内部。
TinyXml使用文档对象模型(DOM)来解析xml文件,这种模型的处理方式为在分析时,一次性的将整个XML文档进行分析,并在内存中形成对应的树结构,同时,向用户提供一系列的接口来访问和编辑该树结构。这种方式占用内存大,但可以给用户提供一个面向对象的访问接口,对用户更为友好
1 TiXmlDocument:文档类
另存为xml文件:
m_pXml->SaveFile("1.xml");
2TiXmlDeclaration:声明类
//-----------------------------------------------------
// 一般来说文档的第一行就是声明对象,你可以把文档对象的第一个子节点转换为声明对象
// <?xml version="1.0" encoding="GB2312" standalone="yes"?>
//-----------------------------------------------------
TiXmlDeclaration *pDeclare = m_pXml->FirstChild()->ToDeclaration();
if (pDeclare)
{
pText = pDeclare->Version(); //<!-- pText == "1.0" -->
pText = pDeclare->Encoding();//<!-- pText == "GB2312" -->
pText = pDeclare->Standalone();//<!-- pText == "yes" -->
}
4TiXmlElement:元素类:
元素为一个容器类,它具有元素名称,并可以包含其它元素,文本,注释和未知节点,这些对象统称为元素的节点,即节点可以为元素、文本、注释和未知节点类型。元素也可以包含任意个数的属性
TiXmlElement *pRoot = m_pXml->RootElement();//<!-- author根结点 -->
pText = m_pXml->Value(); //<!-- pText =="d:\project\XMLDemo\Debug\1.xml" -->
if (pRoot)
{
pText = pRoot->Value();//<!-- pText == "author" -->
//------------------------------------------------------
// 这里和list/tree的遍历相似,先得到第一个值,然后做while循环
//------------------------------------------------------
TiXmlAttribute *pRootAttribute = pRoot->FirstAttribute();
while (pRootAttribute)
{
pText = pRootAttribute->Name();//<!--第一次: pText =="name", 第二次: pText =="sex">
pText = pRootAttribute->Value();//<!--第一次: pText == "hgy413", 第二次: pText =="man">
pRootAttribute = pRootAttribute->Next();
}
//------------------------------------------------------
// 如果直接写成 pRoot->FirstChildElement();返回的是第一个元素
//------------------------------------------------------
char Star[] = "childItem1";
TiXmlElement *pElement = pRoot->FirstChildElement(Star);
//<!-- 同样遍历pElement之后的所有元素 -->
while (pElement)
{
TiXmlAttribute *pElementAttribute = pElement->FirstAttribute();
while (pElementAttribute)
{
pText = pElementAttribute->Name();
pText = pElementAttribute->Value();
pElementAttribute = pElementAttribute->Next();
}
pText = pElement->GetText();
//------------------------------------------------------
// NextSiblingElement同样可以写入star,那么查询的是下一个同名的元素位置
//------------------------------------------------------
pElement = pElement->NextSiblingElement();
}
}
节点名:
元素函数总结
ValueStr //返回元素名称
SetValue //设置元素名称
Parent //返回父节点对象
FirstChild //返回第一个子节点
LastChild //返回最后一个子节点
IterateChildren //返回下一个子节点
InsertEndChild //在最后一个子节点后插入子节点
InsertBeforeChild //在指定的子节点前插入子节点
InsertAfterChild //在指定的子节点后插入子节点
ReplaceChild //替换指定的子节点
RemoveChild //删除指定的子节点
Clear //删除所有的子节点
PreviousSibling //返回同级中前一个节点
NextSibling //返回同级中后一个节点
NextSiblingElement //返回同级中后一个元素
FirstChildElement //返回第一个子元素节点
Attribute //返回元素中的属性值
QueryValueAttribute //返回元素中的属性值
SetAttribute //设置元素中的属性值
FirstAttribute //返回元素中第一个属性对象
LastAttribute //返回元素中最后一个属性对象
RemoveAttribute //删除元素中指定的属性对象
属性类
属性为名称="值"对,元素可以具有属性值,但名称必须唯一