转载:http://www.cnblogs.com/1024Planet/p/4401929.html

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<person>
<name>Alan</name>
<age>26</age>
<height>165</height>
<weight>65</weight>
<introduction>C senior engineer</introduction>
</person>

解析代码

#include <stdio.h>
#include "tinyxml.h" int tinyxmlTest(void); int main(int argc, char* argv[])
{
tinyxmlTest();
return ;
} int tinyxmlTest(void)
{
#if (1)
char* xmlStr = "\
<person>\
<name>Alan</name>\
<age></age>\
<height></height>\
<weight></weight>\
<introduction>C senior engineer</introduction>\
</person>"; TiXmlDocument* myDocument = new TiXmlDocument();
myDocument->Parse(xmlStr); #else
TiXmlDocument* myDocument = new TiXmlDocument();
myDocument->LoadFile("person.xml");
#endif
//.....person.....
TiXmlElement* rootElement = myDocument->RootElement();
if (rootElement == NULL || strcmp(rootElement->Value(), "person"))
return ;
printf("%s:\t%s\n", rootElement->Value(), rootElement->GetText()); //.....name.....
TiXmlElement* element = rootElement->FirstChildElement();
if (element == NULL || strcmp(element->Value(), "name"))
return ;
printf("%s:\t%s\n", element->Value(), element->GetText()); //.....age.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "age"))
return ;
printf("%s:\t%s\n", element->Value(), element->GetText()); //.....height.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "height"))
return ;
printf("%s:\t%s\n", element->Value(), element->GetText()); //.....weight.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "weight"))
return ;
printf("%s:\t%s\n", element->Value(), element->GetText()); //.....introduction.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "introduction"))
return ;
printf("%s:\t%s\n\n", element->Value(), element->GetText()); return ;
}

上面是网友贴的代码,我根据实际开发中遇到的问题,贴出自己的代码

第一种:

    char* xmlStr = "\<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<data type=\"maplist\">\
<person>\
<name>messi</name>\
<age></age>\
<height></height>\
<weight></weight>\
</person>\
<person>\
<name>kobe</name>\
<age></age>\
<height></height>\
<weight></weight>\
</person>\
<person>\
<name>Alan</name>\
<age></age>\
<height></height>\
<weight></weight>\
</person>\
/data>";

遍历取到每一个person下面节点的name、age、height、weight的value 和text

TiXmlDocument* myDocument ;

myDocument.Parse(xmlStr));

TiXmlElement* rootElement = myDocument.RootElement();

if (rootElement)
{
TiXmlElement* element = rootElement->FirstChildElement(); if (element)
{ for (TiXmlNode* SubItem = rootElement->FirstChild();SubItem!=NULL;)
{
TiXmlElement* AppNameElement = SubItem->ToElement()->FirstChildElement(); TiXmlElement* nameElement = AppNameElement->NextSiblingElement();
TiXmlElement* ageElement = AppPermit_typeElement->NextSiblingElement(); TiXmlElement* heightElement = AppPermit_typeElement->NextSiblingElement(); TiXmlElement* weightElement = AppPermit_typeElement->NextSiblingElement(); printf("%s:\t%s\n", nameElement ->Value(), nameElement ->GetText());
printf("%s:\t%s\n", ageElement ->Value(), ageElement ->GetText());
printf("%s:\t%s\n", heightElement ->Value(), heightElement ->GetText());
printf("%s:\t%s\n", weightElement ->Value(), weightElement ->GetText()); SubItem=SubItem->NextSibling();
}
}
}

第二种:

char* xmlStr = "\<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<data type=\"vector\">\
<Country>USA</Country>\
<Country>China</Country>\
</data>";
       TiXmlDocument myDocument;

        myDocument.Parse(xmlStr);

        TiXmlElement* rootElement = myDocument.RootElement();

        if (rootElement)
{
TiXmlElement* element = rootElement->FirstChildElement(); if (element)
{
for (TiXmlNode* SubItem = rootElement->FirstChild();SubItem!=NULL;)
{
TiXmlElement* ExtNameElement = SubItem->ToElement(); printf("%s:\t%s\n", NameElement->Value(), NameElement->GetText()); SubItem=SubItem->NextSibling();
}
}
}
}

注:如果你获取的字符串是Unicode的,必须转码为UTF-8

C++ tinyXml直接解析XML字符串的更多相关文章

  1. tinyXml直接解析XML字符串

    一直都用tinyxml直接LoadFile来解析XML,发现原来也可以直接解析XML字符串. XML文件: <?xml version=\"1.0\" encoding=\& ...

  2. C++解析XML字符串

    项目交互遇到了需要VC++中解析XML字符串,故花了点时间了解了下VC++中解析XML的诸多方法主要包括三种:msxml(微软提供).markup.TinyXml. 开始花了点时间使用msxml3,虽 ...

  3. 解析xml字符串时报“前言中不允许有内容”错误。

    一,问题出现经过: j基于java语言webservic服务端接收客户端 传来的xml字符串用 解析时总报:org.dom4j.DocumentException: Error on line 1 o ...

  4. dom4j解析xml字符串实例

    DOM4J 与利用DOM.SAX.JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异.功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml ...

  5. java解析xml字符串(用dom4j)

    package com.smsServer.Dhst; import java.util.HashMap; import java.util.Iterator; import java.util.Ma ...

  6. java解析xml字符串为实体(dom4j解析)

    package com.smsServer.Dhst; import java.util.HashMap; import java.util.Iterator; import java.util.Ma ...

  7. .NET解析xml字符串,通过反射给实体类对象赋值,获取实体类数据列表

    /// <summary> /// 解析xml字符串 转换为实体类列表数据 /// </summary> /// <param name="xmlStr&quo ...

  8. ORACLE 解析xml字符串-转载的

    --------------------------方法一------------------------------------- 1.xml字符串 /* <orderlist>     ...

  9. java解析xml字符串方法

    一,用DOM4J  针对无重复标签的xml字符串格式,如下: 针对此种情况可用DOM4J解析法,引入 dom4j的相关jar包代码如下: Document document=DocumentHelpe ...

随机推荐

  1. xp上使用vsphere client报错问题

    出现该问题的原因是新版本的esxi和vcenter中增强了加密强度,而Windows XP和Windows Server 2003未能达到所需加密强度,client发起的链接被esxi和vcenter ...

  2. mysqli_report

    MYSQLI_REPORT_OFF Turns reporting off MYSQLI_REPORT_ERROR Report errors from mysqli function calls M ...

  3. Spring-事务管理(Transaction)

    1.事务介绍 事务(Transaction):访问并能更新数据库中数据项的一个程序执行单元. 事务是一系列的动作,它们综合在一起才是一个完整的工作单元,这些动作必须要么全部完成,要么什么都不做,如果有 ...

  4. ext3日志模式

    ext3日志模式 http://blog.sina.com.cn/s/blog_5d4ab4b40100dosx.html ext3支持多种日志模式 ext3 是ext2文件系统的高一级版本,完全兼容 ...

  5. npm设置淘宝镜像

    npm config set registry https://registry.npm.taobao.org --global npm config set disturl https://npm. ...

  6. Scala系统学习(三):Scala基础语法

    如果您熟悉Java语言语法和编程,那么学习Scala将会很容易.Scala和Java之间最大的句法差异在于行结束字符的分号(;) 是可选的. 当编写Scala程序时,它可以被定义为通过调用彼此的方法进 ...

  7. R之ddlpy函数学习[转载]

    转自:https://www.cnblogs.com/aloiswei/p/6032513.html 1.函数 ddply(.data, .variables, .fun = NULL, ..., . ...

  8. [LeetCode] 181. Employees Earning More Than Their Managers_Easy tag: SQL

    The Employee table holds all employees including their managers. Every employee has an Id, and there ...

  9. RNN实现字符级语言模型 - 恐龙岛(自己写RNN前向后向版本+keras版本)

    问题描述:样本为所有恐龙名字,为了构建字符级语言模型来生成新的名称,你的模型将学习不同的名称模式,并随机生成新的名字. 在这里你将学习到: 如何存储文本数据以便使用rnn进行处理. 如何合成数据,通过 ...

  10. vs计算代码行数

    1.用vs打开程序 2.编辑——查找——在文件中查找 3.查找内容^b*[^:b#/]+.*$   应用正则表达式,在整个解决方案中,文件类型空 4.查找全部,仔细盯着右下角数字,查找完毕后会自动消失 ...