linux内核读取xml文件,linux下用tinyxml读写配置文件 [zz]

将tinyxml_2_4_0.tar.gz上传到主机,然后解压执行如下命令:

tar -xzvf tinyxml_2_4_0.tar.gz

成功之后,会在当前目录出现一个tinyxml目录,进入该目录cd tinyxml,然后进行编译,顺序执行如下命令:

cd tinyxml

make

在屏幕上会打印如下输出:

[dev@localhost

tinyxml]$ make

g++ -c -Wall -Wno-format -g -DDEBUG tinyxml.cpp -o

tinyxml.o

g++ -c -Wall -Wno-format -g -DDEBUG

tinyxmlparser.cpp -o tinyxmlparser.o

g++ -c -Wall -Wno-format -g -DDEBUG xmltest.cpp -o

xmltest.o

g++ -c -Wall -Wno-format -g -DDEBUG

tinyxmlerror.cpp -o tinyxmlerror.o

g++ -c -Wall -Wno-format -g -DDEBUG tinystr.cpp -o

tinystr.o

tinystr.cpp:38: warning: aggregate has a partly

bracketed initializer

g++ -o xmltest -g tinyxml.o tinyxmlparser.o xmltest.o

tinyxmlerror.o tinystr.o

[dev@localhost tinyxml]$ make

g++ -c -Wall -Wno-format -g -DDEBUG tinyxml.cpp -o tinyxml.o

g++ -c -Wall -Wno-format -g -DDEBUG tinyxmlparser.cpp -o tinyxmlparser.o

g++ -c -Wall -Wno-format -g -DDEBUG xmltest.cpp -o xmltest.o

g++ -c -Wall -Wno-format -g -DDEBUG tinyxmlerror.cpp -o tinyxmlerror.o

g++ -c -Wall -Wno-format -g -DDEBUG tinystr.cpp -o tinystr.o

tinystr.cpp:38: warning: aggregate has a partly bracketed initializer

g++ -o xmltest -g tinyxml.o tinyxmlparser.o xmltest.o tinyxmlerror.o tinystr.o

没有出现错误,表示编译完成,这时可以执行tinyxml自带的测试程序xmltest。

我直行xmltest之后,打印出一堆乱码,后来就没有管。自己写程序测试了。

为了使用tinyxml开发,使用方便,做了一些配置。

添加环境变量TINYXML_ROOT,编辑.bash_profile,添加如下内容:

#############################################

#### for

tinyxml

#############################################

export TINYXML_ROOT=$HOME/tinyxml

#############################################

#### for tinyxml

#############################################

export TINYXML_ROOT=$HOME/tinyxml

把tinyxml包编译打包成一个连接库,方便开发,这就要修改tinyxml目录下的Makefile。

#****************************************************************************

#

# Makefile for TinyXml test.

# Lee

Thomason

# www.grinninglizard.com

#

# This is a GNU make (gmake) makefile

#****************************************************************************

# DEBUG

can be set to YES to include debugging info, or NO

otherwise

DEBUG := YES

# PROFILE can be set to YES to include profiling info, or

NO otherwise

PROFILE := NO

#

TINYXML_USE_STL can be used to turn on STL support. NO, then

STL

# will not be used. YES will include the STL

files.

TINYXML_USE_STL := NO

#****************************************************************************

CC := gcc

CXX := g++

LD := g++

AR := ar rc

RANLIB := ranlib

DEBUG_CFLAGS := -Wall -Wno-format -g -DDEBUG

RELEASE_CFLAGS := -Wall -Wno-unknown-pragmas

-Wno-format -O3

LIBS :=

DEBUG_CXXFLAGS := ${DEBUG_CFLAGS}

RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}

DEBUG_LDFLAGS := -g

RELEASE_LDFLAGS :=

ifeq (YES, ${DEBUG})

CFLAGS := ${DEBUG_CFLAGS}

CXXFLAGS := ${DEBUG_CXXFLAGS}

LDFLAGS := ${DEBUG_LDFLAGS}

else

CFLAGS := ${RELEASE_CFLAGS}

CXXFLAGS := ${RELEASE_CXXFLAGS}

LDFLAGS := ${RELEASE_LDFLAGS}

endif

ifeq (YES, ${PROFILE})

CFLAGS := ${CFLAGS} -pg -O3

CXXFLAGS := ${CXXFLAGS} -pg -O3

LDFLAGS := ${LDFLAGS} -pg

endif

#****************************************************************************

#

Preprocessor directives

#****************************************************************************

ifeq (YES, ${TINYXML_USE_STL})

DEFS := -DTIXML_USE_STL

else

DEFS :=

endif

#****************************************************************************

#

Include paths

#****************************************************************************

#INCS := -I/usr/include/g++-2

-I/usr/local/include

INCS :=

#****************************************************************************

#

Makefile code common to all platforms

#****************************************************************************

CFLAGS := ${CFLAGS} ${DEFS}

CXXFLAGS := ${CXXFLAGS} ${DEFS}

#****************************************************************************

# Targets of the build

#****************************************************************************

OUTPUT := xmltest

LIB := libtinyxml.so

all: ${OUTPUT} ${LIB}

#****************************************************************************

# Source files

#****************************************************************************

SRCS := tinyxml.cpp tinyxmlparser.cpp xmltest.cpp

tinyxmlerror.cpp tinystr.cpp

# Add on

the sources for libraries

SRCS := ${SRCS}

OBJS := $(addsuffix .o,$(basename

${SRCS}))

LIBOBJS := tinyxml.o tinyxmlparser.o tinyxmlerror.o

tinystr.o

#****************************************************************************

# Output

#****************************************************************************

${OUTPUT}: ${OBJS}

${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS}

${EXTRA_LIBS}

${LIB}: ${LIBOBJS}

ar -r $@ ${LIBOBJS}

#****************************************************************************

# common

rules

#****************************************************************************

# Rules for compiling source files to object

files

%.o : %.cpp

${CXX} -c ${CXXFLAGS} ${INCS}

$< -o $@

%.o : %.c

${CC} -c ${CFLAGS} ${INCS} $< -o

$@

dist:

bash makedistlinux

clean:

-rm -f core ${OBJS} ${OUTPUT} ${LIB} ${TEST}

depend:

#makedepend ${INCS} ${SRCS}

tinyxml.o: tinyxml.h tinystr.h

tinyxmlparser.o: tinyxml.h tinystr.h

xmltest.o: tinyxml.h tinystr.h

tinyxmlerror.o: tinyxml.h tinystr.h

#****************************************************************************

#

# Makefile for TinyXml test.

# Lee Thomason

# www.grinninglizard.com

#

# This is a GNU make (gmake) makefile

#****************************************************************************

# DEBUG can be set to YES to include debugging info, or NO otherwise

DEBUG := YES

# PROFILE can be set to YES to include profiling info, or NO otherwise

PROFILE := NO

# TINYXML_USE_STL can be used to turn on STL support. NO, then STL

# will not be used. YES will include the STL files.

TINYXML_USE_STL := NO

#****************************************************************************

CC := gcc

CXX := g++

LD := g++

AR := ar rc

RANLIB := ranlib

DEBUG_CFLAGS := -Wall -Wno-format -g -DDEBUG

RELEASE_CFLAGS := -Wall -Wno-unknown-pragmas -Wno-format -O3

LIBS :=

DEBUG_CXXFLAGS := ${DEBUG_CFLAGS}

RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}

DEBUG_LDFLAGS := -g

RELEASE_LDFLAGS :=

ifeq (YES, ${DEBUG})

CFLAGS := ${DEBUG_CFLAGS}

CXXFLAGS := ${DEBUG_CXXFLAGS}

LDFLAGS := ${DEBUG_LDFLAGS}

else

CFLAGS := ${RELEASE_CFLAGS}

CXXFLAGS := ${RELEASE_CXXFLAGS}

LDFLAGS := ${RELEASE_LDFLAGS}

endif

ifeq (YES, ${PROFILE})

CFLAGS := ${CFLAGS} -pg -O3

CXXFLAGS := ${CXXFLAGS} -pg -O3

LDFLAGS := ${LDFLAGS} -pg

endif

#****************************************************************************

# Preprocessor directives

#****************************************************************************

ifeq (YES, ${TINYXML_USE_STL})

DEFS := -DTIXML_USE_STL

else

DEFS :=

endif

#****************************************************************************

# Include paths

#****************************************************************************

#INCS := -I/usr/include/g++-2 -I/usr/local/include

INCS :=

#****************************************************************************

# Makefile code common to all platforms

#****************************************************************************

CFLAGS := ${CFLAGS} ${DEFS}

CXXFLAGS := ${CXXFLAGS} ${DEFS}

#****************************************************************************

# Targets of the build

#****************************************************************************

OUTPUT := xmltest

LIB := libtinyxml.so

all: ${OUTPUT} ${LIB}

#****************************************************************************

# Source files

#****************************************************************************

SRCS := tinyxml.cpp tinyxmlparser.cpp xmltest.cpp tinyxmlerror.cpp tinystr.cpp

# Add on the sources for libraries

SRCS := ${SRCS}

OBJS := $(addsuffix .o,$(basename ${SRCS}))

LIBOBJS := tinyxml.o tinyxmlparser.o tinyxmlerror.o tinystr.o

#****************************************************************************

# Output

#****************************************************************************

${OUTPUT}: ${OBJS}

${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}

${LIB}: ${LIBOBJS}

ar -r $@ ${LIBOBJS}

#****************************************************************************

# common rules

#****************************************************************************

# Rules for compiling source files to object files

%.o : %.cpp

${CXX} -c ${CXXFLAGS} ${INCS} $< -o $@

%.o : %.c

${CC} -c ${CFLAGS} ${INCS} $< -o $@

dist:

bash makedistlinux

clean:

-rm -f core ${OBJS} ${OUTPUT} ${LIB} ${TEST}

depend:

#makedepend ${INCS} ${SRCS}

tinyxml.o: tinyxml.h tinystr.h

tinyxmlparser.o: tinyxml.h tinystr.h

xmltest.o: tinyxml.h tinystr.h

tinyxmlerror.o: tinyxml.h tinystr.h

在tinyxml目录下重新执行make,会看到多执行了一行命令:

ar -r libtinyxml.so tinyxml.o tinyxmlparser.o tinyxmlerror.o

tinystr.o

生成了一个包libtinyxml.so,有了这个包,使用tinyxml开发的时候,在连接命令中加入这个包的连接,就可以正确地生成目标程序。

现在来写一个小程序测试一下,在tinyxml目录创建一个测试的xml文件,文件名为test.xml,内容如下:

周星星

20

白晶晶

18

周星星

20

白晶晶

18

在tinyxml下创建,也添加了一个测试程序tinyxml_test.cpp,内容如下:

#include "tinyxml.h"

#include

"tinystr.h"

#include

using namespace

std;

int

main()

{

//创建一个XML的文档对象。

TiXmlDocument *myDocument = new

TiXmlDocument("test.xml");

myDocument->LoadFile();

//获得根元素,即Persons。

TiXmlElement *RootElement =

myDocument->RootElement();

//输出根元素名称,即输出Persons。

cout <<

RootElement->Value()

<< endl;

//获得第一个Person节点。

TiXmlElement *FirstPerson =

RootElement->FirstChildElement();

//输出接点名Person

cout <<

FirstPerson->Value()

<< endl;

//获得第一个Person的name节点和age节点和ID属性。

TiXmlElement *NameElement =

FirstPerson->FirstChildElement();

TiXmlElement *AgeElement =

NameElement->NextSiblingElement();

TiXmlAttribute *IDAttribute =

FirstPerson->FirstAttribute();

//输出第一个Person的name内容,即周星星;age内容,即20;ID属性,即1。

cout <<

NameElement->FirstChild()->Value()

<< endl;

cout <<

AgeElement->FirstChild()->Value()

<< endl;

cout <<

IDAttribute->Value()

<< endl;

return 0;

}

#include "tinyxml.h"

#include "tinystr.h"

#include

using namespace std;

int main()

{

//创建一个XML的文档对象。

TiXmlDocument *myDocument = new TiXmlDocument("test.xml");

myDocument->LoadFile();

//获得根元素,即Persons。

TiXmlElement *RootElement = myDocument->RootElement();

//输出根元素名称,即输出Persons。

cout << RootElement->Value() << endl;

//获得第一个Person节点。

TiXmlElement *FirstPerson = RootElement->FirstChildElement();

//输出接点名Person

cout << FirstPerson->Value() << endl;

//获得第一个Person的name节点和age节点和ID属性。

TiXmlElement *NameElement = FirstPerson->FirstChildElement();

TiXmlElement *AgeElement = NameElement->NextSiblingElement();

TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();

//输出第一个Person的name内容,即周星星;age内容,即20;ID属性,即1。

cout << NameElement->FirstChild()->Value() << endl;

cout << AgeElement->FirstChild()->Value() << endl;

cout << IDAttribute->Value() << endl;

return 0;

}

在tinyxml目录下修改Makefile,修改之后的内容如下:

#****************************************************************************

#

# Makefile for TinyXml test.

# Lee

Thomason

# www.grinninglizard.com

#

# This is a GNU make (gmake) makefile

#****************************************************************************

# DEBUG

can be set to YES to include debugging info, or NO

otherwise

DEBUG := YES

# PROFILE can be set to YES to include profiling info, or

NO otherwise

PROFILE := NO

#

TINYXML_USE_STL can be used to turn on STL support. NO, then

STL

# will not be used. YES will include the STL

files.

TINYXML_USE_STL := NO

#****************************************************************************

CC := gcc

CXX := g++

LD := g++

AR := ar rc

RANLIB := ranlib

DEBUG_CFLAGS := -Wall -Wno-format -g -DDEBUG

RELEASE_CFLAGS := -Wall -Wno-unknown-pragmas

-Wno-format -O3

LIBS :=

DEBUG_CXXFLAGS := ${DEBUG_CFLAGS}

RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}

DEBUG_LDFLAGS := -g

RELEASE_LDFLAGS :=

ifeq (YES, ${DEBUG})

CFLAGS := ${DEBUG_CFLAGS}

CXXFLAGS := ${DEBUG_CXXFLAGS}

LDFLAGS := ${DEBUG_LDFLAGS}

else

CFLAGS := ${RELEASE_CFLAGS}

CXXFLAGS := ${RELEASE_CXXFLAGS}

LDFLAGS := ${RELEASE_LDFLAGS}

endif

ifeq (YES, ${PROFILE})

CFLAGS := ${CFLAGS} -pg -O3

CXXFLAGS := ${CXXFLAGS} -pg -O3

LDFLAGS := ${LDFLAGS} -pg

endif

#****************************************************************************

#

Preprocessor directives

#****************************************************************************

ifeq (YES, ${TINYXML_USE_STL})

DEFS := -DTIXML_USE_STL

else

DEFS :=

endif

#****************************************************************************

#

Include paths

#****************************************************************************

#INCS := -I/usr/include/g++-2

-I/usr/local/include

INCS :=

#****************************************************************************

#

Makefile code common to all platforms

#****************************************************************************

CFLAGS := ${CFLAGS} ${DEFS}

CXXFLAGS := ${CXXFLAGS} ${DEFS}

#****************************************************************************

# Targets of the build

#****************************************************************************

OUTPUT := xmltest

LIB := libtinyxml.so

TEST:= tinyxml_test

all: ${OUTPUT} ${LIB} ${TEST}

#****************************************************************************

# Source

files

#****************************************************************************

SRCS := tinyxml.cpp tinyxmlparser.cpp

xmltest.cpp tinyxmlerror.cpp tinystr.cpp

# Add on the sources for libraries

SRCS := ${SRCS}

OBJS := $(addsuffix .o,$(basename ${SRCS}))

LIBOBJS := tinyxml.o tinyxmlparser.o

tinyxmlerror.o tinystr.o

#****************************************************************************

#

Output

#****************************************************************************

${OUTPUT}: ${OBJS}

${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS}

${EXTRA_LIBS}

${LIB}: ${LIBOBJS}

ar -r $@ ${LIBOBJS}

${TEST}: tinyxml_test.o

${LD} -o $@ ${LDFLAGS} tinyxml_test.o -L${TINYXML_ROOT}

-ltinyxml

#****************************************************************************

# common rules

#****************************************************************************

# Rules

for compiling source files to object files

%.o : %.cpp

${CXX} -c ${CXXFLAGS} ${INCS} $< -o

$@

%.o : %.c

${CC} -c ${CFLAGS} ${INCS} $<

-o $@

dist:

bash makedistlinux

clean:

-rm -f core ${OBJS} ${OUTPUT} ${LIB}

${TEST}

depend:

#makedepend ${INCS} ${SRCS}

tinyxml.o: tinyxml.h tinystr.h

tinyxmlparser.o: tinyxml.h

tinystr.h

xmltest.o: tinyxml.h tinystr.h

tinyxmlerror.o: tinyxml.h

tinystr.h

#****************************************************************************

#

# Makefile for TinyXml test.

# Lee Thomason

# www.grinninglizard.com

#

# This is a GNU make (gmake) makefile

#****************************************************************************

# DEBUG can be set to YES to include debugging info, or NO otherwise

DEBUG := YES

# PROFILE can be set to YES to include profiling info, or NO otherwise

PROFILE := NO

# TINYXML_USE_STL can be used to turn on STL support. NO, then STL

# will not be used. YES will include the STL files.

TINYXML_USE_STL := NO

#****************************************************************************

CC := gcc

CXX := g++

LD := g++

AR := ar rc

RANLIB := ranlib

DEBUG_CFLAGS := -Wall -Wno-format -g -DDEBUG

RELEASE_CFLAGS := -Wall -Wno-unknown-pragmas -Wno-format -O3

LIBS :=

DEBUG_CXXFLAGS := ${DEBUG_CFLAGS}

RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}

DEBUG_LDFLAGS := -g

RELEASE_LDFLAGS :=

ifeq (YES, ${DEBUG})

CFLAGS := ${DEBUG_CFLAGS}

CXXFLAGS := ${DEBUG_CXXFLAGS}

LDFLAGS := ${DEBUG_LDFLAGS}

else

CFLAGS := ${RELEASE_CFLAGS}

CXXFLAGS := ${RELEASE_CXXFLAGS}

LDFLAGS := ${RELEASE_LDFLAGS}

endif

ifeq (YES, ${PROFILE})

CFLAGS := ${CFLAGS} -pg -O3

CXXFLAGS := ${CXXFLAGS} -pg -O3

LDFLAGS := ${LDFLAGS} -pg

endif

#****************************************************************************

# Preprocessor directives

#****************************************************************************

ifeq (YES, ${TINYXML_USE_STL})

DEFS := -DTIXML_USE_STL

else

DEFS :=

endif

#****************************************************************************

# Include paths

#****************************************************************************

#INCS := -I/usr/include/g++-2 -I/usr/local/include

INCS :=

#****************************************************************************

# Makefile code common to all platforms

#****************************************************************************

CFLAGS := ${CFLAGS} ${DEFS}

CXXFLAGS := ${CXXFLAGS} ${DEFS}

#****************************************************************************

# Targets of the build

#****************************************************************************

OUTPUT := xmltest

LIB := libtinyxml.so

TEST:= tinyxml_test

all: ${OUTPUT} ${LIB} ${TEST}

#****************************************************************************

# Source files

#****************************************************************************

SRCS := tinyxml.cpp tinyxmlparser.cpp xmltest.cpp tinyxmlerror.cpp tinystr.cpp

# Add on the sources for libraries

SRCS := ${SRCS}

OBJS := $(addsuffix .o,$(basename ${SRCS}))

LIBOBJS := tinyxml.o tinyxmlparser.o tinyxmlerror.o tinystr.o

#****************************************************************************

# Output

#****************************************************************************

${OUTPUT}: ${OBJS}

${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}

${LIB}: ${LIBOBJS}

ar -r $@ ${LIBOBJS}

${TEST}: tinyxml_test.o

${LD} -o $@ ${LDFLAGS} tinyxml_test.o -L${TINYXML_ROOT} -ltinyxml

#****************************************************************************

# common rules

#****************************************************************************

# Rules for compiling source files to object files

%.o : %.cpp

${CXX} -c ${CXXFLAGS} ${INCS} $< -o $@

%.o : %.c

${CC} -c ${CFLAGS} ${INCS} $< -o $@

dist:

bash makedistlinux

clean:

-rm -f core ${OBJS} ${OUTPUT} ${LIB} ${TEST}

depend:

#makedepend ${INCS} ${SRCS}

tinyxml.o: tinyxml.h tinystr.h

tinyxmlparser.o: tinyxml.h tinystr.h

xmltest.o: tinyxml.h tinystr.h

tinyxmlerror.o: tinyxml.h tinystr.h

然后执行make,这时,可以看到多了一个tinyxml_test 生成,执行tinyxml_test,得到如下输出结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值