相关链接
C++ —— Tinyxml2在Vs2017下相关使用1(附源码)
tinyxml2简介
TinyXML2是一个简单,小巧,高效,C++XML解析器,可以很容易地集成到其他程序中。TinyXML-2解析一个XML文档,并从中构建一个 可以读取、修改和保存的文档对象模型 (DOM)。XML代表“可扩展标记语言”。这是一个通用目的 用于描述任意数据的人类和机器可读标记语言。 为存储应用程序数据而创建的所有随机文件格式都可以 全部替换为 XML。一个解析器可以解决所有问题。
源码下载
一般来说,下载源码后无需编译仅将其中的tinyxml2.cpp、tinyxml2.h包含在自己的项目中就可以使用了。
(若需构建库使用CMake构建源码,然后利用Vs仅编译tinyxml2项目。)
使用tinyxml2注意事项
TinyXML与实体
TinyXML认得预定义的特殊“字符实体”,即:
& &
< <
> >
" "
' ‘
这些在XML文档读取时都会被辨认出来,并会被转化成等价的UTF-8字符。
代码写入xml
<?xml version="1.0" encoding="UTF-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfigurations Include="Debug|X64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfigurations>
<ProjectConfiguration Include="Release|X64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props"/>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props"/>
<ImportGroup Condition="Exists('$(QtMsBuild)\qt_defaults.props')">
<Import Project="$(QtMsBuild)\qt_defaults.props"/>
</ImportGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="QtSettings">
<QtInstall>5.12.4(64)</QtInstall>
<QtModules>core;gui;widgets;network;</QtModules>
<QtBuildConfig>debug</QtBuildConfig>
</PropertyGroup>
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')">
<Message Importance="High" Text="QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly."/>
</Target>
<ImportGroup Label="ExtensionSettings"/>
<ImportGroup Label="Shared"/>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform"/>
<Import Project="$(QtMsBuild)\Qt.props"/>
</ImportGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
<ClCompile>
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<DebugInformationFormat>None</DebugInformationFormat>
<Optimization>MaxSpeed</Optimization>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>false</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
</Project>
#include <iostream>
#include "tinyxml/tinyxml2.h"
int main()
{
#if 0 // 写入
tinyxml2::XMLDocument xml;
xml.Parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
tinyxml2::XMLElement* rootNode = xml.NewElement("Project");
rootNode->SetAttribute("DefaultTargets", "Build");
rootNode->SetAttribute("ToolsVersion", "15.0");
rootNode->SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
xml.InsertEndChild(rootNode);
{
tinyxml2::XMLElement *element_ItemGroup = xml.NewElement("ItemGroup");
element_ItemGroup->SetAttribute("Label", "ProjectConfigurations");
rootNode->InsertEndChild(element_ItemGroup);
{
tinyxml2::XMLElement *element_ProjectConfiguration = xml.NewElement("ProjectConfigurations");
element_ProjectConfiguration->SetAttribute("Include", "Debug|X64");
element_ItemGroup->InsertEndChild(element_ProjectConfiguration);
{
tinyxml2::XMLElement *element_ProjectConfiguration_Configuration = xml.NewElement("Configuration");
element_ProjectConfiguration_Configuration->InsertNewText("Debug");
element_ProjectConfiguration->InsertEndChild(element_ProjectConfiguration_Configuration);
}
{
tinyxml2::XMLElement *element_ProjectConfiguration_Platform = xml.NewElement("Platform");
element_ProjectConfiguration_Platform->InsertNewText("x64");
element_ProjectConfiguration->InsertEndChild(element_ProjectConfiguration_Platform);
}
}
{
tinyxml2::XMLElement *element_ProjectConfiguration = xml.NewElement("ProjectConfiguration");
element_ProjectConfiguration->SetAttribute("Include", "Release|X64");
element_ItemGroup->InsertEndChild(element_ProjectConfiguration);
{
tinyxml2::XMLElement *element_ProjectConfiguration_Configuration = xml.NewElement("Configuration");
element_ProjectConfiguration_Configuration->InsertNewText("Release");
element_ProjectConfiguration->InsertEndChild(element_ProjectConfiguration_Configuration);
}
{
tinyxml2::XMLElement *element_ProjectConfiguration_Platform = xml.NewElement("Platform");
element_ProjectConfiguration_Platform->InsertNewText("x64");
element_ProjectConfiguration->InsertEndChild(element_ProjectConfiguration_Platform);
}
}
}
{
tinyxml2::XMLElement *element_Import = xml.NewElement("Import");
element_Import->SetAttribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
rootNode->InsertEndChild(element_Import);
}
{
tinyxml2::XMLElement *element_Import = xml.NewElement("Import");
element_Import->SetAttribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
rootNode->InsertEndChild(element_Import);
}
{
tinyxml2::XMLElement *element_ImportGroup = xml.NewElement("ImportGroup");
element_ImportGroup->SetAttribute("Condition", "Exists('$(QtMsBuild)\\qt_defaults.props')");
{
tinyxml2::XMLElement *element_Import = xml.NewElement("Import");
element_Import->SetAttribute("Project", "$(QtMsBuild)\\qt_defaults.props");
element_ImportGroup->InsertEndChild(element_Import);
}
rootNode->InsertEndChild(element_ImportGroup);
}
{
tinyxml2::XMLElement *element_PropertyGroup = xml.NewElement("PropertyGroup");
element_PropertyGroup->SetAttribute("Condition", "'$(Configuration)|$(Platform)' == 'Debug|x64'");
element_PropertyGroup->SetAttribute("Label", "QtSettings");
{
{
tinyxml2::XMLElement *element_QtInstall = xml.NewElement("QtInstall");
element_QtInstall->InsertNewText("5.12.4(64)");
element_PropertyGroup->InsertEndChild(element_QtInstall);
}
{
tinyxml2::XMLElement *element_QtModules = xml.NewElement("QtModules");
element_QtModules->InsertNewText("core;gui;widgets;network;");
element_PropertyGroup->InsertEndChild(element_QtModules);
}
{
tinyxml2::XMLElement *element_QtBuildConfig = xml.NewElement("QtBuildConfig");
element_QtBuildConfig->InsertNewText("debug");
element_PropertyGroup->InsertEndChild(element_QtBuildConfig);
}
}
rootNode->InsertEndChild(element_PropertyGroup);
}
{
tinyxml2::XMLElement *element_Target = xml.NewElement("Target");
element_Target->SetAttribute("Name", "QtMsBuildNotFound");
element_Target->SetAttribute("BeforeTargets", "CustomBuild;ClCompile");
element_Target->SetAttribute("Condition", "!Exists('$(QtMsBuild)\\qt.targets') or !Exists('$(QtMsBuild)\\qt.props')");
{
tinyxml2::XMLElement *element_Message = xml.NewElement("Message");
element_Message->SetAttribute("Importance", "High");
element_Message->SetAttribute("Text", "QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly.");
element_Target->InsertEndChild(element_Message);
}
rootNode->InsertEndChild(element_Target);
}
{
tinyxml2::XMLElement *element_ImportGroup = xml.NewElement("ImportGroup");
element_ImportGroup->SetAttribute("Label", "ExtensionSettings");
rootNode->InsertEndChild(element_ImportGroup);
}
{
tinyxml2::XMLElement *element_ImportGroup = xml.NewElement("ImportGroup");
element_ImportGroup->SetAttribute("Label", "Shared");
rootNode->InsertEndChild(element_ImportGroup);
}
{
tinyxml2::XMLElement *element_ImportGroup = xml.NewElement("ImportGroup");
element_ImportGroup->SetAttribute("Label", "PropertySheets");
element_ImportGroup->SetAttribute("Condition", "'$(Configuration)|$(Platform)' == 'Debug|x64'");
rootNode->InsertEndChild(element_ImportGroup);
{
tinyxml2::XMLElement *element_Import = xml.NewElement("Import");
element_Import->SetAttribute("Project", "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props");
element_Import->SetAttribute("Condition", "exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')");
element_Import->SetAttribute("Label", "LocalAppDataPlatform");
element_ImportGroup->InsertEndChild(element_Import);
}
{
tinyxml2::XMLElement *element_Import = xml.NewElement("Import");
element_Import->SetAttribute("Project", "$(QtMsBuild)\\Qt.props");
element_ImportGroup->InsertEndChild(element_Import);
}
}
{
tinyxml2::XMLElement *element_ItemDefinitionGroup = xml.NewElement("ItemDefinitionGroup");
element_ItemDefinitionGroup->SetAttribute("Condition", "'$(Configuration)|$(Platform)' == 'Release|x64'");
element_ItemDefinitionGroup->SetAttribute("Label", "Configuration");
{
{
tinyxml2::XMLElement *element_ClCompile = xml.NewElement("ClCompile");
element_ItemDefinitionGroup->InsertEndChild(element_ClCompile);
{
tinyxml2::XMLElement *element_TreatWChar_tAsBuiltInType = xml.NewElement("TreatWChar_tAsBuiltInType");
element_TreatWChar_tAsBuiltInType->InsertNewText("true");
element_ClCompile->InsertEndChild(element_TreatWChar_tAsBuiltInType);
}
{
tinyxml2::XMLElement *element_TreatWChar_MultiProcessorCompilation = xml.NewElement("MultiProcessorCompilation");
element_TreatWChar_MultiProcessorCompilation->InsertNewText("true");
element_ClCompile->InsertEndChild(element_TreatWChar_MultiProcessorCompilation);
}
{
tinyxml2::XMLElement *element_DebugInformationFormat = xml.NewElement("DebugInformationFormat");
element_DebugInformationFormat->InsertNewText("None");
element_ClCompile->InsertEndChild(element_DebugInformationFormat);
}
{
tinyxml2::XMLElement *element_Optimization = xml.NewElement("Optimization");
element_Optimization->InsertNewText("MaxSpeed");
element_ClCompile->InsertEndChild(element_Optimization);
}
}
{
tinyxml2::XMLElement *element_Link = xml.NewElement("Link");
element_ItemDefinitionGroup->InsertEndChild(element_Link);
{
tinyxml2::XMLElement *element_SubSystem = xml.NewElement("SubSystem");
element_SubSystem->InsertNewText("Console");
element_Link->InsertEndChild(element_SubSystem);
}
{
tinyxml2::XMLElement *element_GenerateDebugInformation = xml.NewElement("GenerateDebugInformation");
element_GenerateDebugInformation->InsertNewText("false");
element_Link->InsertEndChild(element_GenerateDebugInformation);
}
}
}
rootNode->InsertEndChild(element_ItemDefinitionGroup);
}
xml.SaveFile("./temp_behaviac.xml");
xml.Clear();
system("pause");
return 0;
}
代码读取xml
int main()
{
tinyxml2::XMLDocument xml;
tinyxml2::XMLError errXml = xml.LoadFile("./temp_behaviac.xml");
if (tinyxml2::XML_SUCCESS == errXml)
{
tinyxml2::XMLElement* root = xml.RootElement();
if (root)
{
std::cout << root->Value()
<< " DefaultTargets=" << root->Attribute("DefaultTargets")
<< " ToolsVersion=" << root->Attribute("ToolsVersion")
<< " xmlns=" << root->Attribute("xmlns") << std::endl;
tinyxml2::XMLElement *XMLElement_ItemGroup = root->FirstChildElement();
if (XMLElement_ItemGroup)
{
std::cout << "\t" << XMLElement_ItemGroup->Value() << " Label=" << XMLElement_ItemGroup->Attribute("Label") << std::endl;
{
tinyxml2::XMLElement *XMLElement_ProjectConfigurations = XMLElement_ItemGroup->FirstChildElement();
if (!XMLElement_ProjectConfigurations) { return -1; }
std::cout << "\t\t" << XMLElement_ProjectConfigurations->Value() << " Include=" << XMLElement_ProjectConfigurations->Attribute("Include") << std::endl;
{
tinyxml2::XMLElement *XMLElement_Configuration = XMLElement_ProjectConfigurations->FirstChildElement();
if (!XMLElement_Configuration) { return -1; }
std::cout << "\t\t\t" << XMLElement_Configuration->Value() << " " << XMLElement_Configuration->GetText() << std::endl;
tinyxml2::XMLElement *XMLElement_Platform = XMLElement_Configuration->NextSiblingElement();
if (!XMLElement_Platform) { return -1; }
std::cout << "\t\t\t" << XMLElement_Platform->Value() << " " << XMLElement_Platform->GetText() << std::endl;
}
tinyxml2::XMLElement *XMLElement_ProjectConfiguration = XMLElement_ProjectConfigurations->NextSiblingElement();
if (!XMLElement_ProjectConfiguration) { return -1; }
std::cout << "\t\t" << XMLElement_ProjectConfiguration->Value() << " Include=" << XMLElement_ProjectConfiguration->Attribute("Include") << std::endl;
{
tinyxml2::XMLElement *XMLElement_Configuration = XMLElement_ProjectConfiguration->FirstChildElement();
if (!XMLElement_Configuration) { return -1; }
std::cout << "\t\t\t" << XMLElement_Configuration->Value() << " " << XMLElement_Configuration->GetText() << std::endl;
tinyxml2::XMLElement *XMLElement_Platform = XMLElement_Configuration->NextSiblingElement();
if (!XMLElement_Platform) { return -1; }
std::cout << "\t\t\t" << XMLElement_Platform->Value() << " " << XMLElement_Platform->GetText() << std::endl;
}
}
tinyxml2::XMLElement *XMLElement_Import = XMLElement_ItemGroup->NextSiblingElement();
if (!XMLElement_Import) { return -1; }
std::cout << "\t" << XMLElement_Import->Value() << " Project=" << XMLElement_Import->Attribute("Project") << std::endl;
tinyxml2::XMLElement *XMLElement_Import2 = XMLElement_Import->NextSiblingElement();
if (!XMLElement_Import2) { return -1; }
std::cout << "\t" << XMLElement_Import2->Value() << " Project=" << XMLElement_Import2->Attribute("Project") << std::endl;
tinyxml2::XMLElement *XMLElement_ImportGroup = XMLElement_Import2->NextSiblingElement();
if (!XMLElement_ImportGroup) { return -1; }
std::cout << "\t" << XMLElement_ImportGroup->Value() << " Condition=" << XMLElement_ImportGroup->Attribute("Condition") << std::endl;
{
tinyxml2::XMLElement *XMLElement_Import = XMLElement_ImportGroup->FirstChildElement();
if (!XMLElement_Import) { return -1; }
std::cout << "\t\t" << XMLElement_Import->Value() << " Project=" << XMLElement_Import->Attribute("Project") << std::endl;
}
tinyxml2::XMLElement *XMLElement_PropertyGroup = XMLElement_ImportGroup->NextSiblingElement();
if (!XMLElement_PropertyGroup) { return -1; }
std::cout << "\t" << XMLElement_PropertyGroup->Value() << " Condition=" << XMLElement_PropertyGroup->Attribute("Condition") << " Label=" << XMLElement_PropertyGroup->Attribute("Label") << std::endl;
{
tinyxml2::XMLElement *XMLElement_QtInstall = XMLElement_PropertyGroup->FirstChildElement();
if (!XMLElement_QtInstall) { return -1; }
std::cout << "\t\t" << XMLElement_QtInstall->Value() << " " << XMLElement_QtInstall->GetText() << std::endl;
tinyxml2::XMLElement *XMLElement_QtModules = XMLElement_QtInstall->NextSiblingElement();
if (!XMLElement_QtModules) { return -1; }
std::cout << "\t\t" << XMLElement_QtModules->Value() << " " << XMLElement_QtModules->GetText() << std::endl;
tinyxml2::XMLElement *XMLElement_QtBuildConfig = XMLElement_QtModules->NextSiblingElement();
if (!XMLElement_QtBuildConfig) { return -1; }
std::cout << "\t\t" << XMLElement_QtBuildConfig->Value() << " " << XMLElement_QtBuildConfig->GetText() << std::endl;
}
tinyxml2::XMLElement *XMLElement_Target = XMLElement_PropertyGroup->NextSiblingElement();
if (!XMLElement_Target) { return -1; }
std::cout << "\t" << XMLElement_Target->Value() << " Name=" << XMLElement_Target->Attribute("Name") << " BeforeTargets=" << XMLElement_Target->Attribute("BeforeTargets") << " Condition=" << XMLElement_Target->Attribute("Condition") << std::endl;
{
tinyxml2::XMLElement *XMLElement_Message = XMLElement_Target->FirstChildElement();
if (!XMLElement_Message) { return -1; }
std::cout << "\t\t" << XMLElement_Message->Value() << " Importance=" << XMLElement_Message->Attribute("Importance") << " Text=" << XMLElement_Message->Attribute("Text") << std::endl;
}
tinyxml2::XMLElement *XMLElement_ImportGroup1 = XMLElement_Target->NextSiblingElement();
if (!XMLElement_ImportGroup1) { return -1; }
std::cout << "\t" << XMLElement_ImportGroup1->Value() << " Label=" << XMLElement_ImportGroup1->Attribute("Label") << std::endl;
tinyxml2::XMLElement *XMLElement_ImportGroup2 = XMLElement_ImportGroup1->NextSiblingElement();
if (!XMLElement_ImportGroup2) { return -1; }
std::cout << "\t" << XMLElement_ImportGroup2->Value() << " Label=" << XMLElement_ImportGroup2->Attribute("Label") << std::endl;
tinyxml2::XMLElement *XMLElement_ImportGroup3 = XMLElement_ImportGroup2->NextSiblingElement();
if (!XMLElement_ImportGroup3) { return -1; }
std::cout << "\t" << XMLElement_ImportGroup3->Value() << " Label=" << XMLElement_ImportGroup3->Attribute("Label") << " Condition=" << XMLElement_ImportGroup3->Attribute("Condition") << std::endl;
{
tinyxml2::XMLElement *XMLElement_Import = XMLElement_ImportGroup3->FirstChildElement();
if (!XMLElement_Import) { return -1; }
std::cout << "\t\t" << XMLElement_Import->Value() << " Project=" << XMLElement_Import->Attribute("Project") << " Condition=" << XMLElement_Import->Attribute("Condition") << " Label=" << XMLElement_Import->Attribute("Label") << std::endl;
tinyxml2::XMLElement *XMLElement_Import2 = XMLElement_Import->NextSiblingElement();
if (!XMLElement_Import2) { return -1; }
std::cout << "\t\t" << XMLElement_Import2->Value() << " Project=" << XMLElement_Import2->Attribute("Project") << std::endl;
}
tinyxml2::XMLElement *XMLElement_ItemDefinitionGroup = XMLElement_ImportGroup3->NextSiblingElement();
if (!XMLElement_ItemDefinitionGroup) { return -1; }
std::cout << "\t" << XMLElement_ItemDefinitionGroup->Value() << " Condition=" << XMLElement_ItemDefinitionGroup->Attribute("Condition") << " Label=" << XMLElement_ItemDefinitionGroup->Attribute("Label") << std::endl;
{
tinyxml2::XMLElement *XMLElement_ClCompile = XMLElement_ItemDefinitionGroup->FirstChildElement();
if (!XMLElement_ClCompile) { return -1; }
std::cout << "\t" << XMLElement_ClCompile->Value() << std::endl;
{
tinyxml2::XMLElement *XMLElement_TreatWChar_tAsBuiltInType = XMLElement_ClCompile->FirstChildElement();
if (!XMLElement_TreatWChar_tAsBuiltInType) { return -1; }
std::cout << "\t\t" << XMLElement_TreatWChar_tAsBuiltInType->Value() << " " << XMLElement_TreatWChar_tAsBuiltInType->GetText() << std::endl;
tinyxml2::XMLElement *XMLElement_MultiProcessorCompilation = XMLElement_TreatWChar_tAsBuiltInType->NextSiblingElement();
if (!XMLElement_MultiProcessorCompilation) { return -1; }
std::cout << "\t\t" << XMLElement_MultiProcessorCompilation->Value() << " " << XMLElement_MultiProcessorCompilation->GetText() << std::endl;
tinyxml2::XMLElement *XMLElement_DebugInformationFormat = XMLElement_MultiProcessorCompilation->NextSiblingElement();
if (!XMLElement_DebugInformationFormat) { return -1; }
std::cout << "\t\t" << XMLElement_DebugInformationFormat->Value() << " " << XMLElement_DebugInformationFormat->GetText() << std::endl;
tinyxml2::XMLElement *XMLElement_Optimization = XMLElement_DebugInformationFormat->NextSiblingElement();
if (!XMLElement_Optimization) { return -1; }
std::cout << "\t\t" << XMLElement_Optimization->Value() << " " << XMLElement_Optimization->GetText() << std::endl;
}
tinyxml2::XMLElement *XMLElement_Link = XMLElement_ClCompile->NextSiblingElement();
if (!XMLElement_Link) { return -1; }
std::cout << "\t" << XMLElement_Link->Value() << std::endl;
{
tinyxml2::XMLElement *XMLElement_SubSystem = XMLElement_Link->FirstChildElement();
if (!XMLElement_SubSystem) { return -1; }
std::cout << "\t\t" << XMLElement_SubSystem->Value() << " " << XMLElement_SubSystem->GetText() << std::endl;
tinyxml2::XMLElement *XMLElement_GenerateDebugInformation = XMLElement_SubSystem->NextSiblingElement();
if (!XMLElement_GenerateDebugInformation) { return -1; }
std::cout << "\t\t" << XMLElement_GenerateDebugInformation->Value() << " " << XMLElement_GenerateDebugInformation->GetText() << std::endl;
}
}
}
}
root = nullptr;
}
system("pause");
return 0;
}
笔者
笔者 - jxd