转:linux下tinyxml开发入门

转自下面这篇文章:

http://blog.csdn.net/chenxiaohua/article/details/4272171

http://ncu.dl.sourceforge.net/sourceforge/tinyxml/tinyxml_2_4_0.tar.gz下载tinyxml,可以根据自己的需要,选择不同的版本。

将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

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

添加环境变量TINYXML_ROOT

把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

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

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

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

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

<Persons>
        <Person ID="1">
            <name>周星星</name>
            <age>20</age>
        </Person>
        <Person ID="2">
            <name>白晶晶</name>
            <age>18</age>
        </Person>
    </Persons>
在tinyxml下创建,也添加了一个测试程序tinyxml_test.cpp,内容如下:

#include "tinyxml.h"
#include "tinystr.h"

#include <iostream>

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 

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

Persons
Person
周星星
20
1 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值