tinyxml2使用方法

tinyxml2使用方法最近项目中使用到xml,最终选用了tinyxml2,学习后写个使用总结。

大家好,又见面了,我是你们的朋友全栈君。

最近项目中使用到xml,最终选用了tinyxml2,学习后写个总结。

TinyXml2 主要类型:

XMLNode: XMLNode is a base class for every object that is in the XMLDocument Object Model (DOM), except XMLAttributes.Nodes have siblings, aparent, and children which can be navigated. A node is always in a XMLDocument.The type of a XMLNode can be queried, and it can be cast to its more definedtype.

XMLDocument: A Document binds together all the functionality.It can be saved,loaded, and printed to the screen.All Nodes are connected and allocated to aDocument.If the Document is deleted, all its Nodes are also deleted.

A XMLDocumentallocates memory for all its Nodes.When the XMLDocument gets deleted, all itsNodes will also be deleted.

A Document cancontain:         Element   (container or leaf)

                                                                 Comment(leaf)

                                                                 Unknown(leaf)

                                                                 Declaration(leaf )

XMLDeclaration: In correct XML the declaration is the first entry in the file.

                   <?xmlversion=”1.0″ standalone=”yes”?>

         TinyXML-2will happily read or write files without a declaration,however.    The text of the declaration isn’tinterpreted. It is parsed and written as a string.

XMLComment: 对应于XML文档中的注释部分的对象。

XMLElement: The element is a container class. It has a value, the elementname,and can contain other elements, text, comments, and unknowns.Elements alsocontain an arbitrary number of attributes.

XMLText:         Note that a text nodecan have child element nodes, for example:

         <root>Thisis <b>bold</b></root>

         Atext node can have 2 ways to output the next. “normal” output

         andCDATA. It will default to the mode it was parsed from the XML file and

         yougenerally want to leave it alone, but you can change the output mode with

         SetCData()and query it with CData().

XMLAttribute: An attribute is a name-value pair. Elements have an arbitrary

         numberof attributes, each with a unique name.

         @noteThe attributes are not XMLNodes. You may only query theNext() attribute in alist.

XMLUnknown:  Any tag that TinyXML-2doesn’t recognize is saved as an unknown. It is a tag of text, but should notbe modified.It will be written back to the XML, unchanged, when the file issaved.DTD tags get thrown into XMLUnknowns.

从上面的介绍可知,除了XMLAttribute以外,其他的都继承自XMLNode

tinyxml2把xml文档建立成一棵DOM树,具体实现用的是firstchild–nextsiblingtree,下图是对该树的模型的一个简单介绍:

tinyxml2使用方法

firstchild-nextsibling是一种多叉树常用的实现方法,每个结点只需要知道它的第一个孩子结点(first child node)和它的下一个兄弟结点(next sibling node),这样一整棵树的结构就会建立起来,也可以用根结点的指针为起点来对整棵树进行遍历。

写xml文件,有些意外的XMLText应该可以插入子节点,但是最后生成的文件并没有子节点。

static void Write()
{
	XMLDocument Doc;
	XMLDeclaration* pDecaration=Doc.NewDeclaration("This is a Declaration!");
	Doc.LinkEndChild(pDecaration);
	XMLComment* pComment = Doc.NewComment("This is a Document Comment!");
	Doc.LinkEndChild(pComment);
	XMLElement* pElementRoot = Doc.NewElement("School");
	Doc.LinkEndChild(pElementRoot);
	XMLComment* pCommentRoot = Doc.NewComment("This is a School Comment!");
	pElementRoot->LinkEndChild(pCommentRoot);
	{
		XMLElement* pElementTeachers = Doc.NewElement("teachers");
		pElementRoot->LinkEndChild(pElementTeachers);
		pElementTeachers->LinkEndChild(Doc.NewElement("Wang"));
		pElementTeachers->LinkEndChild(Doc.NewElement("Li"));
		pElementTeachers->LinkEndChild(Doc.NewElement("Zhao"));
	}
	XMLElement* pElementStudents = Doc.NewElement("students");
	pElementRoot->LinkEndChild(pElementStudents);
	{
		XMLElement* pElementLiMing = Doc.NewElement("LiMing");
		pElementLiMing->SetText("Li Ming is a good Student!");
		pElementLiMing->SetAttribute("sex", "male");
		pElementLiMing->SetAttribute("height", 174);
		pElementLiMing->SetAttribute("weight", 80.4);
		pElementLiMing->SetAttribute("Is_good_at_math", false);
		pElementStudents->LinkEndChild(pElementLiMing);
	}
	{
		XMLElement* pElementCuiHua = Doc.NewElement("CuiHua");
		XMLElement* pElementSex = Doc.NewElement("sex");
		pElementSex->SetText("female");
		XMLText* pText = Doc.NewText("this is a Text!");
		pText->LinkEndChild(pElementSex);
		pElementCuiHua->LinkEndChild(pText);
		pElementStudents->LinkEndChild(pElementCuiHua);
	}
	{
		XMLElement* pElementHanmeimei = Doc.NewElement("Hanmeimei");
		pElementStudents->LinkEndChild(pElementHanmeimei);
		XMLText* pTextCData = Doc.NewText("this is a CData Text:if (a < b && a < 0)");
		pTextCData->SetCData(true);
		pElementHanmeimei->LinkEndChild(pTextCData);
	}

	XMLUnknown* pUnknow = Doc.NewUnknown("this is a Unknow!");
	pElementRoot->LinkEndChild(pUnknow);
	Doc.SaveFile("test.xml");
}

生成的文件:

<?This is a Declaration!?>
<!--This is a Document Comment!-->
<School>
    <!--This is a School Comment!-->
    <teachers>
        <Wang/>
        <Li/>
        <Zhao/>
    </teachers>
    <students>
        <LiMing sex="male" height="174" weight="80.400000000000006" Is_good_at_math="false">Li Ming is a good Student!</LiMing>
        <CuiHua>this is a Text!</CuiHua>
        <Hanmeimei><![CDATA[this is a CData Text:if (a < b && a < 0)]]></Hanmeimei>
    </students>
    <!this is a Unknow!>
</School>

读xml文件

static void Read()
{
	XMLDocument Doc;
	Doc.LoadFile("test.xml");
	XMLElement* pElementRoot = Doc.RootElement();
	{
		XMLElement* pElementTeachers = pElementRoot->FirstChildElement("teachers");
		pElementTeachers->FirstChildElement("Wang");
		pElementTeachers->FirstChildElement("Li");
		pElementTeachers->FirstChildElement("Zhao");
	}
	XMLElement* pElementStudents = pElementRoot->FirstChildElement("students"); 
	{
		XMLElement* pElementLiMing = pElementStudents->FirstChildElement("LiMing");
		const char* pText=pElementLiMing->GetText();
		const char* pSex=pElementLiMing->Attribute("sex");
		int iHeight=pElementLiMing->IntAttribute("height");
		double dbHeight=pElementLiMing->DoubleAttribute("weight");
		bool bIsGood=pElementLiMing->BoolAttribute("Is_good_at_math");
	}
	{
		XMLElement* pElementCuiHua = pElementStudents->FirstChildElement("CuiHua");
		XMLNode* pNode=pElementCuiHua->FirstChild();
		XMLText* pText = pNode->ToText();
		//XMLElement* pElementSex = pText->FirstChildElement("sex");
		//const char* pSex = pElementSex->GetText();
	}
	{
		XMLElement* pElementHanmeimei = pElementStudents->FirstChildElement("Hanmeimei");
		XMLText* pTextCData = pElementHanmeimei->FirstChild()->ToText();
		bool bCData=pTextCData->CData();
	}
}

最后说下中文的问题,由于tinyxml2使用utf8编码,如果输入中文,输出的是乱码,以下是解决方案,使用字符转换,需要c++11的支持。

#include <string>
#include <vector>
#include <codecvt>
#ifdef UNICODE
typedef wchar_t tchar;
#else
typedef char tchar;
#endif

typedef std::basic_string < tchar, std::char_traits<tchar>, std::allocator<tchar> > tstring;
std::string unicode_to_utf8(std::wstring const& strUnicode)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>> cutf8;
	return cutf8.to_bytes(strUnicode);
}
std::wstring utf8_to_unicode(std::string const& strutf8)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>> cutf8;
	return cutf8.from_bytes(strutf8);
}
std::wstring gb2312_to_unicode(std::string const &strGb2312)
{
	std::vector<wchar_t> buff(strGb2312.size());
#ifdef _MSC_VER
	std::locale loc("zh-CN");
#else
	std::locale loc("zh_CN.GB18030");
#endif
	wchar_t* pwszNext = nullptr;
	const char* pszNext = nullptr;
	mbstate_t state = {};
	int res = std::use_facet<std::codecvt<wchar_t, char, mbstate_t> >
		(loc).in(state,
		strGb2312.data(), strGb2312.data() + strGb2312.size(), pszNext,
		buff.data(), buff.data() + buff.size(), pwszNext);

	if (std::codecvt_base::ok == res)
	{
		return std::wstring(buff.data(), pwszNext);
	}
	return L"";
}

std::string unicode_to_gb2312(std::wstring const& strUnicode)
{
#ifdef _MSC_VER
	std::locale loc("zh-CN");
#else
	std::locale loc("zh_CN.GB18030");
#endif
	const wchar_t* pwszNext = nullptr;
	char* pszNext = nullptr;
	mbstate_t state = {};

	std::vector<char> buff(strUnicode.size() * 2);
	int res = std::use_facet<std::codecvt<wchar_t, char, mbstate_t> >
		(loc).out(state,
		strUnicode.data(), strUnicode.data() + strUnicode.size(), pwszNext,
		buff.data(), buff.data() + buff.size(), pszNext);

	if (std::codecvt_base::ok == res)
	{
		return std::string(buff.data(), pszNext);
	}
	return "";
}
inline std::string tstring_to_utf8(tstring const& strToConvert){
#ifdef UNICODE
	return unicode_to_utf8(strToConvert);
#else
	auto strUnicode=gb2312_to_unicode(strToConvert);
	return unicode_to_utf8(strUnicode);
#endif 
}
inline tstring utf8_to_tstring(std::string const& strToConvert){
#ifdef UNICODE
	return utf8_to_unicode(strToConvert);
#else
	auto strUnicode = utf8_to_unicode(strToConvert);
	return unicode_to_gb2312(strUnicode);
#endif 
}

这时候,我们可能需要对XMLElement,XMLDocument等做个wrapper,封装字符转换。感谢
ml232528给出的解决方案。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132973.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • QThread如何优雅实现暂停(挂起)功能

    QThread如何优雅实现暂停(挂起)功能一、实现思路QThread中有start、quit,但是没有pause,那么我们想要实现这个功能。我们继承QThread,重写run();第一反应是不是应该添加个标志,在run()中判断暂停状态。嗯,没错,不过我们不能用普通变量,否则有线程非安全风险。这里使用C++提供的原子类型std::atomic_bool。线程暂停期间,不能空跑消耗cpu,故我们使用Qt条件变量QWaitCondition,配合QMutex。大概就是这么点内容吧,实现代码如下:Thread.h#include&lt

    2022年5月27日
    41
  • BGP与Anycast

    BGP与AnycastBGP属于一项技术,一般应用于IDC多线机房,也就是把接入到机房的多条线路融合为一体。实现多线单IP。因为最早的多线机房都是双线双IP,现在很多双线机房开始利用该技术,让用户使用IP的时候操作更简单些Anycast技术具有以下优势:一、不同客户端将访问不同目的主机,此过程对客户端透明,从而实现了目的主机的负载均衡;二、当任意目的主机接入的网络出现故障,导致该目的主机不可达…

    2022年5月24日
    71
  • webrtc技术原理_webrtc开源项目

    webrtc技术原理_webrtc开源项目一、概述webrtc冗余打包方式有三种:Red(rfc2198)、Ulpfec(rfc5109)、Flexfec(草案)。其中Red和Ulpfec要成对使用。二、RedFEC简单将老报文打包到新包上。如下图所示,冗余度为1时,RFC2198打包情况:这种方法在音视频领域几乎不使用,因为冗余包只能保护特定一个报文,这种方法带宽占用量很大,恢复能力有限,性价比很低。只是早期的T38……

    2022年8月11日
    6
  • Pytorch版本、CUDA版本与显卡驱动版本的对应关系

    Pytorch版本、CUDA版本与显卡驱动版本的对应关系参考链接:INSTALLINGPREVIOUSVERSIONSOFPYTORCH解决PyTorch与CUDA版本不匹配1.CUDA驱动和CUDAToolkit对应版本注:驱动是向下兼容的,其决定了可安装的CUDA和CUDAToolkit的最高版本。2.CUDA及其可用PyTorch对应版本(参考官网,欢迎评论区补充)注:虽然有的卡CUDA版本可更新至新版本,且PyTorch也可对应更新至新版本。但有的对应安装包无法使用,有可能是由于卡太旧的原因。3.安装指导(1)指定安装PyTor

    2022年6月16日
    362
  • Python + allure 报告[通俗易懂]

    Python + allure 报告[通俗易懂]安装Windows安装allure需要先安装scoop,确保安装了PowerShell5(或更高版本,包括PowerShellCore)和.netFramework4.5(或更高版本)。然后打开PowerShell运行:iex(new-objectnet.webclient).downloadstring(‘https://get.scoop.sh’)安装allure:sco…

    2022年7月26日
    10
  • Xenu-web开发死链接检测工具应用

    Xenu-web开发死链接检测工具应用Xenu是一款深受业界好评,并被广泛使用的死链接检测工具。时常检测网站并排除死链接,对网站的SEO非常重要,因为大量死链接存在会降低用户和搜索引擎对网站的信任。我们推荐的死链接检测工具Xenu主要具有以下特征:需要下载安装,不到1M大小,用户界面非常简洁,操作简单。检测彻底:能够检测到图片、框架、插件、背景、样式表、脚本和java程序中的链接。报告形式合理多样,死链

    2022年7月23日
    15

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号