1.使用第3方库或windows SDK,更新某元素的属性值和元素值。
2.提供函数或方法.修改属性值和元素值。如:
1.实现函数 UpdateElementValue(const char* key,const char* value);
-- key的格式,更新toolbarwindow 属性name值时,可使用xpath格式,传入参数:toolbarwindow@name和值"goodbye"
-- 更新rect的属性值x时,key: "toolbarwindow:rect@x" value: "100"
2.先自己找资料再参考别人的,记录下实际耗时。
!.使用tinyxml。
<xml>
<selectwindow>It is a select object window</selectwindow>
<toolbarwindow name="toolbar" width="200">
It is a toolbar.
<rect x="4" y="5"/>
</toolbarwindow >
#include <string>
#include "tinyxml.h"
#include "tinystr.h"
#include "tool/string/string_util.h"
#include "tool/log/debug.h"
using namespace std;
int UpdateElementValue(TiXmlElement* root_element, const char* key, const char* value)
{
TiXmlElement* curr_element = root_element;
string str_key = key;
string xml_path;
string xml_property = "";
int addr = str_key.find_first_of('@');
if(addr != string::npos)
{
xml_path = str_key.substr(0, addr);
xml_property = str_key.substr(addr + 1);
}
else
{
xml_path = str_key.substr(0);
}
int i = 0;
int index = 0;
int size = xml_path.size();
for(; i < size; )
{
string str_note;
index = xml_path.find_first_of(':', i);
if (index == -1)
{
str_note = xml_path.substr(i);
}
else
{
str_note = xml_path.substr(i, index - i);
}
int j = 0;
for(; curr_element ; curr_element = curr_element->NextSiblingElement())
{
if(strcmp(curr_element->Value(), str_note.c_str()) == 0)
{
string dssddd = curr_element->Value();
j = 1;
break;
}
}
if(j == 0)
{
return -1;
}
i += str_note.length() + 1;
if(i < size)
{
curr_element = curr_element->FirstChildElement();
}
}
if(xml_property == "")
{
//文本修改
curr_element->ReplaceChild(curr_element->FirstChild(),TiXmlText(value));
}
else
{
//属性修改
curr_element->SetAttribute(xml_property.c_str(), value);
}
return 0;
}
int main(int argc, char **argv)
{
const char* path = QXGetRunDirectory(argv[0]);
string xml_file_path;
xml_file_path.append(path).append("uuu.xml");
//创建一个XML的文档对象。
TiXmlDocument* myDocument = new TiXmlDocument(xml_file_path.c_str());
myDocument->LoadFile();
//获得根元素,即Persons。
TiXmlElement* root_element = myDocument->RootElement();
UpdateElementValue(root_element->FirstChildElement(), "toolbarwindow@name", "goodbye");//修改属性
UpdateElementValue(root_element->FirstChildElement(), "toolbarwindow:rect@x", "100");//修改属性
UpdateElementValue(root_element->FirstChildElement(), "toolbarwindow", "It is a window.");//修改text
//保存到文件
myDocument->SaveFile(xml_file_path.c_str());
delete myDocument;
return 0;
}
#
# Generated Makefile - do not edit!
#
# Edit the Makefile in the project folder instead (../Makefile). Each target
# has a -pre and a -post target defined where you can add customized code.
#
# This makefile implements configuration specific macros and targets.
# Environment
MKDIR=mkdir
CP=cp
GREP=grep
NM=nm
CCADMIN=CCadmin
RANLIB=ranlib
CC=gcc.exe
CCC=g++.exe
CXX=g++.exe
FC=gfortran.exe
AS=as.exe
# Macros
CND_PLATFORM=MinGW-Windows
CND_CONF=Debug
CND_DISTDIR=dist
# Include project Makefile
include Makefile
# Object Directory
OBJECTDIR=build/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
${OBJECTDIR}/src/read_config/read_config_info.o \
${OBJECTDIR}/src/read_config/tinystr.o \
${OBJECTDIR}/src/read_config/tinyxml.o \
${OBJECTDIR}/src/read_config/tinyxmlerror.o \
${OBJECTDIR}/src/read_config/tinyxmlparser.o
# C Compiler Flags
CFLAGS=
# CC Compiler Flags
CCFLAGS=
CXXFLAGS=-g -Wall -MMD -MP -MF $@.d -m32 -D__QXWIN32__ -DBUILDING_READ_CONFIG_DLL -Iinclude -I/E/software/project/tool/win32/share/include
# Fortran Compiler Flags
FFLAGS=
# Assembler Flags
ASFLAGS=
# Link Libraries and Options
LDLIBSOPTIONS=-L/E/software/project/tool/win32/share/Debug -lQX_Tool
# Build Targets
.build-conf: ${BUILD_SUBPROJECTS}
"${MAKE}" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/read_config.exe
dist/Debug/MinGW-Windows/read_config.exe: ${OBJECTFILES}
${MKDIR} -p dist/Debug/MinGW-Windows
${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/read_config.exe ${OBJECTFILES} ${LDLIBSOPTIONS} -Wl,--out-implib,${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libread_config.dll.a
${OBJECTDIR}/src/read_config/read_config_info.o: nbproject/Makefile-${CND_CONF}.mk src/read_config/read_config_info.cpp
${MKDIR} -p ${OBJECTDIR}/src/read_config
${RM} $@.d
$(COMPILE.cc) -o ${OBJECTDIR}/src/read_config/read_config_info.o src/read_config/read_config_info.cpp
${OBJECTDIR}/src/read_config/tinystr.o: nbproject/Makefile-${CND_CONF}.mk src/read_config/tinystr.cpp
${MKDIR} -p ${OBJECTDIR}/src/tinystr
${RM} $@.d
$(COMPILE.cc) -o ${OBJECTDIR}/src/read_config/tinystr.o src/read_config/tinystr.cpp
${OBJECTDIR}/src/read_config/tinyxml.o: nbproject/Makefile-${CND_CONF}.mk src/read_config/tinyxml.cpp
${MKDIR} -p ${OBJECTDIR}/src/tinyxml
${RM} $@.d
$(COMPILE.cc) -o ${OBJECTDIR}/src/read_config/tinyxml.o src/read_config/tinyxml.cpp
${OBJECTDIR}/src/read_config/tinyxmlerror.o: nbproject/Makefile-${CND_CONF}.mk src/read_config/tinyxmlerror.cpp
${MKDIR} -p ${OBJECTDIR}/src/tinyxmlerror
${RM} $@.d
$(COMPILE.cc) -o ${OBJECTDIR}/src/read_config/tinyxmlerror.o src/read_config/tinyxmlerror.cpp
${OBJECTDIR}/src/read_config/tinyxmlparser.o: nbproject/Makefile-${CND_CONF}.mk src/read_config/tinyxmlparser.cpp
${MKDIR} -p ${OBJECTDIR}/src/tinyxmlparser
${RM} $@.d
$(COMPILE.cc) -o ${OBJECTDIR}/src/read_config/tinyxmlparser.o src/read_config/tinyxmlparser.cpp
# Subprojects
.build-subprojects:
# Clean Targets
.clean-conf: ${CLEAN_SUBPROJECTS}
${RM} -r build/Debug
${RM} dist/Debug/MinGW-Windows/read_config.exe
# Subprojects
.clean-subprojects:
# Enable dependency checking
.dep.inc: .depcheck-impl
include .dep.inc