提交 19e7aab8 编写于 作者: 小代码2016's avatar 小代码2016

tinyxml2 基本使用

上级 cdbc74f5
<?xml version="1.0" encoding="UTF-8"?>
<personList>
<person id="1001" name="khl001" age="34" manager="true">
<intro>这是一段介绍. 这是一段介绍.</intro>
</person>
<person id="1003" name="khl003" age="48">
<intro>这是一段介绍. 这是一段介绍.</intro>
</person>
</personList>
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name=".\bin\Release\khl_xml_test" errors="0" failures="0" tests="21" time="0.02751" timestamp="2023-02-01T19:48:40Z" doctest_version="2.4.9">
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_edit_node.cpp" name="tinyxml2_edit_node/add_node" time="0.010682" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_edit_node.cpp" name="tinyxml2_edit_node/remove_node" time="0.008068" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_find_node.cpp" name="tinyxml2_find_node/find_node" time="0.000273" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_load.cpp" name="tinyxml2_load/load_by_path" time="0.000182" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_load.cpp" name="tinyxml2_load/load_by_path_file_not_found" time="0.000147" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_load.cpp" name="tinyxml2_load/load_by_file" time="0.000196" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_load.cpp" name="tinyxml2_load/load_by_charbuffe" time="0" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_load.cpp" name="tinyxml2_load/load_by_charbuffe" time="0" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_output.cpp" name="tinyxml2_output/add_node" time="0.004744" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\test_tinyxml2_output.cpp" name="tinyxml2_output/remove_node" time="0.002015" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\testmain.cpp" name="test 001" time="0.000314" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\testmain.cpp" name="test 003" time="0.00062" status="run"/>
<testcase classname="F:\2023\blog_code\tinyxml2-study\test\tmp_test.cpp" name="test_tmp" time="0.000269" status="run"/>
</testsuite>
</testsuites>
......@@ -20,17 +20,86 @@ TEST_CASE("tinyxml2_edit_node")
SUBCASE("add_node")
{
simpleXmlPath = getXmlPath("/data/simple.xml");
xmlEditResultPath = getXmlPath("/data/edit_result.xml");
xmlEditResultPath = getXmlPath("/data/edit_add_node_result.xml");
xmlError = doc.LoadFile(simpleXmlPath.c_str());
CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError);
auto rootEl = doc.RootElement();
const char* id = "1004";
auto newPersonNode = doc.NewElement("person");
newPersonNode->SetAttribute("id","1004");
newPersonNode->SetAttribute("id", id);
rootEl->InsertEndChild(newPersonNode);
doc.SaveFile(xmlEditResultPath.c_str());
xmlError = doc.LoadFile(xmlEditResultPath.c_str());
CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError);
rootEl = doc.RootElement();
auto personNode = rootEl->FirstChildElement("person");
while( nullptr != personNode )
{
if( 0 == strcmp(id,personNode->Attribute("id")))
{
break;
}
personNode = personNode->NextSiblingElement();
}
bool hasNewPersonNode = nullptr != personNode;
CHECK(hasNewPersonNode);
}
/**
* 删除节点
*/
SUBCASE("remove_node")
{
simpleXmlPath = getXmlPath("/data/simple.xml");
xmlEditResultPath = getXmlPath("/data/edit_remove_node_result.xml");
xmlError = doc.LoadFile(simpleXmlPath.c_str());
CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError);
const char* id = "1002";
auto rootEl = doc.RootElement();
auto personNode = rootEl->FirstChildElement("person");
while( nullptr != personNode )
{
if( 0 == strcmp(id,personNode->Attribute("id")))
{
break;
}
personNode = personNode->NextSiblingElement();
}
rootEl->DeleteChild(personNode);
doc.SaveFile(xmlEditResultPath.c_str());
xmlError = doc.LoadFile(xmlEditResultPath.c_str());
CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError);
rootEl = doc.RootElement();
personNode = rootEl->FirstChildElement("person");
while( nullptr != personNode )
{
if( 0 == strcmp(id,personNode->Attribute("id")))
{
break;
}
personNode = personNode->NextSiblingElement();
}
bool person1002NotExist = nullptr == personNode;
CHECK(person1002NotExist);
}
}
\ No newline at end of file
......@@ -28,7 +28,7 @@ TEST_CASE("tinyxml2_find_node")
auto personNode = rootEl->FirstChildElement("person");
while( nullptr != personNode )
{
if( strcmp(id,personNode->Attribute("id")))
if( 0 == strcmp(id,personNode->Attribute("id")))
{
break;
}
......@@ -38,5 +38,7 @@ TEST_CASE("tinyxml2_find_node")
bool find = nullptr != personNode;
CHECK(find);
bool idCheck = 0 == strcmp(id,personNode->Attribute("id"));
CHECK(idCheck);
}
}
\ No newline at end of file
#include <cstdio>
#include <cstring>
#include "doctest/doctest.h"
#include "tinyxml2.h"
#include "spdlog/spdlog.h"
#include "khl_xml_config/khl_xml_config.hpp"
TEST_CASE("tinyxml2_output")
{
std::string simpleXmlPath;
std::string xmlEditResultPath;
tinyxml2::XMLDocument doc;
tinyxml2::XMLError xmlError;
/**
* 添加节点
*/
SUBCASE("add_node")
{
simpleXmlPath = getXmlPath("/data/simple.xml");
xmlEditResultPath = getXmlPath("/data/edit_add_node_result.xml");
xmlError = doc.LoadFile(simpleXmlPath.c_str());
CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError);
doc.Print();
}
/**
* 删除节点
*/
SUBCASE("remove_node")
{
simpleXmlPath = getXmlPath("/data/simple.xml");
xmlEditResultPath = getXmlPath("/data/edit_remove_node_result.xml");
xmlError = doc.LoadFile(simpleXmlPath.c_str());
CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError);
tinyxml2::XMLPrinter printer;
doc.Print(&printer);
spdlog::info("xml content:\n {}",printer.CStr());
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册