linux下tinyxml开发入门

来源:互联网 发布:mac发出滋滋的声音 编辑:程序博客网 时间:2024/05/17 02:20

从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

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

[java] view plaincopy
  1. [dev@localhost tinyxml]$ make  
  2. g++ -c -Wall -Wno-format -g -DDEBUG    tinyxml.cpp -o tinyxml.o  
  3. g++ -c -Wall -Wno-format -g -DDEBUG    tinyxmlparser.cpp -o tinyxmlparser.o  
  4. g++ -c -Wall -Wno-format -g -DDEBUG    xmltest.cpp -o xmltest.o  
  5. g++ -c -Wall -Wno-format -g -DDEBUG    tinyxmlerror.cpp -o tinyxmlerror.o  
  6. g++ -c -Wall -Wno-format -g -DDEBUG    tinystr.cpp -o tinystr.o  
  7. tinystr.cpp:38: warning: aggregate has a partly bracketed initializer  
  8. g++ -o xmltest -g tinyxml.o tinyxmlparser.o xmltest.o tinyxmlerror.o tinystr.o  

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

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

 

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

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

[java] view plaincopy
  1. #############################################  
  2. #### for tinyxml  
  3. #############################################  
  4. export TINYXML_ROOT=$HOME/tinyxml  

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

 

[c-sharp] view plaincopy
  1. #****************************************************************************  
  2. #  
  3. # Makefile for TinyXml test.  
  4. # Lee Thomason  
  5. # www.grinninglizard.com  
  6. #  
  7. # This is a GNU make (gmake) makefile  
  8. #****************************************************************************  
  9.  
  10. # DEBUG can be set to YES to include debugging info, or NO otherwise  
  11. DEBUG          := YES  
  12.  
  13. # PROFILE can be set to YES to include profiling info, or NO otherwise  
  14. PROFILE        := NO  
  15.  
  16. # TINYXML_USE_STL can be used to turn on STL support. NO, then STL  
  17. # will not be used. YES will include the STL files.  
  18. TINYXML_USE_STL := NO  
  19.  
  20. #****************************************************************************  
  21.   
  22. CC     := gcc  
  23. CXX    := g++  
  24. LD     := g++  
  25. AR     := ar rc  
  26. RANLIB := ranlib  
  27.   
  28. DEBUG_CFLAGS     := -Wall -Wno-format -g -DDEBUG  
  29. RELEASE_CFLAGS   := -Wall -Wno-unknown-pragmas -Wno-format -O3  
  30.   
  31. LIBS             :=  
  32.   
  33. DEBUG_CXXFLAGS   := ${DEBUG_CFLAGS}   
  34. RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}  
  35.   
  36. DEBUG_LDFLAGS    := -g  
  37. RELEASE_LDFLAGS  :=  
  38.   
  39. ifeq (YES, ${DEBUG})  
  40.    CFLAGS       := ${DEBUG_CFLAGS}  
  41.    CXXFLAGS     := ${DEBUG_CXXFLAGS}  
  42.    LDFLAGS      := ${DEBUG_LDFLAGS}  
  43. else  
  44.    CFLAGS       := ${RELEASE_CFLAGS}  
  45.    CXXFLAGS     := ${RELEASE_CXXFLAGS}  
  46.    LDFLAGS      := ${RELEASE_LDFLAGS}  
  47. endif  
  48.   
  49. ifeq (YES, ${PROFILE})  
  50.    CFLAGS   := ${CFLAGS} -pg -O3  
  51.    CXXFLAGS := ${CXXFLAGS} -pg -O3  
  52.    LDFLAGS  := ${LDFLAGS} -pg  
  53. endif  
  54.  
  55. #****************************************************************************  
  56. # Preprocessor directives  
  57. #****************************************************************************  
  58.   
  59. ifeq (YES, ${TINYXML_USE_STL})  
  60.   DEFS := -DTIXML_USE_STL  
  61. else  
  62.   DEFS :=  
  63. endif  
  64.  
  65. #****************************************************************************  
  66. # Include paths  
  67. #****************************************************************************  
  68.  
  69. #INCS := -I/usr/include/g++-2 -I/usr/local/include  
  70. INCS :=  
  71.  
  72.  
  73. #****************************************************************************  
  74. # Makefile code common to all platforms  
  75. #****************************************************************************  
  76.   
  77. CFLAGS   := ${CFLAGS}   ${DEFS}  
  78. CXXFLAGS := ${CXXFLAGS} ${DEFS}  
  79.  
  80. #****************************************************************************  
  81. # Targets of the build  
  82. #****************************************************************************  
  83.   
  84. OUTPUT := xmltest   
  85. LIB := libtinyxml.so  
  86.   
  87. all: ${OUTPUT} ${LIB}  
  88.  
  89.  
  90. #****************************************************************************  
  91. # Source files  
  92. #****************************************************************************  
  93.   
  94. SRCS := tinyxml.cpp tinyxmlparser.cpp xmltest.cpp tinyxmlerror.cpp tinystr.cpp  
  95.  
  96. # Add on the sources for libraries  
  97. SRCS := ${SRCS}  
  98.   
  99. OBJS := $(addsuffix .o,$(basename ${SRCS}))  
  100. LIBOBJS := tinyxml.o tinyxmlparser.o tinyxmlerror.o tinystr.o  
  101.  
  102. #****************************************************************************  
  103. # Output  
  104. #****************************************************************************  
  105.   
  106. ${OUTPUT}: ${OBJS}  
  107.         ${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}  
  108.   
  109. ${LIB}: ${LIBOBJS}  
  110.         ar -r $@ ${LIBOBJS}  
  111.  
  112.  
  113. #****************************************************************************  
  114. # common rules  
  115. #****************************************************************************  
  116.  
  117. # Rules for compiling source files to object files  
  118. %.o : %.cpp  
  119.         ${CXX} -c ${CXXFLAGS} ${INCS} $< -o $@  
  120.   
  121. %.o : %.c  
  122.         ${CC} -c ${CFLAGS} ${INCS} $< -o $@  
  123.   
  124. dist:  
  125.         bash makedistlinux  
  126.   
  127. clean:  
  128.         -rm -f core ${OBJS} ${OUTPUT} ${LIB} ${TEST}  
  129.   
  130. depend:  
  131.         #makedepend ${INCS} ${SRCS}  
  132.   
  133. tinyxml.o: tinyxml.h tinystr.h  
  134. tinyxmlparser.o: tinyxml.h tinystr.h  
  135. xmltest.o: tinyxml.h tinystr.h  
  136. 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,内容如下:

[xhtml] view plaincopy
  1. <Persons>  
  2.         <Person ID="1">  
  3.             <name>周星星</name>  
  4.             <age>20</age>  
  5.         </Person>  
  6.         <Person ID="2">  
  7.             <name>白晶晶</name>  
  8.             <age>18</age>  
  9.         </Person>  
  10.     </Persons>  

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

[cpp] view plaincopy
  1. #include "tinyxml.h"  
  2. #include "tinystr.h"  
  3.   
  4. #include <iostream>  
  5.   
  6. using namespace std;  
  7.   
  8. int main()  
  9. {  
  10.     //创建一个XML的文档对象。  
  11.     TiXmlDocument *myDocument = new TiXmlDocument("test.xml");  
  12.     myDocument->LoadFile();  
  13.       
  14.     //获得根元素,即Persons。  
  15.     TiXmlElement *RootElement = myDocument->RootElement();  
  16.   
  17.     //输出根元素名称,即输出Persons。  
  18.     cout << RootElement->Value() << endl;  
  19.       
  20.     //获得第一个Person节点。  
  21.     TiXmlElement *FirstPerson = RootElement->FirstChildElement();  
  22.     //输出接点名Person  
  23.   
  24.     cout << FirstPerson->Value() << endl;  
  25.     //获得第一个Person的name节点和age节点和ID属性。  
  26.     TiXmlElement *NameElement = FirstPerson->FirstChildElement();  
  27.     TiXmlElement *AgeElement = NameElement->NextSiblingElement();  
  28.     TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();  
  29.       
  30.     //输出第一个Person的name内容,即周星星;age内容,即20;ID属性,即1。  
  31.     cout << NameElement->FirstChild()->Value() << endl;  
  32.     cout << AgeElement->FirstChild()->Value() << endl;  
  33.     cout << IDAttribute->Value() << endl;  
  34.   
  35.         return 0;  
  36. }  

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

[c-sharp] view plaincopy
  1. #****************************************************************************  
  2. #  
  3. # Makefile for TinyXml test.  
  4. # Lee Thomason  
  5. # www.grinninglizard.com  
  6. #  
  7. # This is a GNU make (gmake) makefile  
  8. #****************************************************************************  
  9.  
  10. # DEBUG can be set to YES to include debugging info, or NO otherwise  
  11. DEBUG          := YES  
  12.  
  13. # PROFILE can be set to YES to include profiling info, or NO otherwise  
  14. PROFILE        := NO  
  15.  
  16. # TINYXML_USE_STL can be used to turn on STL support. NO, then STL  
  17. # will not be used. YES will include the STL files.  
  18. TINYXML_USE_STL := NO  
  19.  
  20. #****************************************************************************  
  21.   
  22. CC     := gcc  
  23. CXX    := g++  
  24. LD     := g++  
  25. AR     := ar rc  
  26. RANLIB := ranlib  
  27.   
  28. DEBUG_CFLAGS     := -Wall -Wno-format -g -DDEBUG  
  29. RELEASE_CFLAGS   := -Wall -Wno-unknown-pragmas -Wno-format -O3  
  30.   
  31. LIBS             :=  
  32.   
  33. DEBUG_CXXFLAGS   := ${DEBUG_CFLAGS}   
  34. RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}  
  35.   
  36. DEBUG_LDFLAGS    := -g  
  37. RELEASE_LDFLAGS  :=  
  38.   
  39. ifeq (YES, ${DEBUG})  
  40.    CFLAGS       := ${DEBUG_CFLAGS}  
  41.    CXXFLAGS     := ${DEBUG_CXXFLAGS}  
  42.    LDFLAGS      := ${DEBUG_LDFLAGS}  
  43. else  
  44.    CFLAGS       := ${RELEASE_CFLAGS}  
  45.    CXXFLAGS     := ${RELEASE_CXXFLAGS}  
  46.    LDFLAGS      := ${RELEASE_LDFLAGS}  
  47. endif  
  48.   
  49. ifeq (YES, ${PROFILE})  
  50.    CFLAGS   := ${CFLAGS} -pg -O3  
  51.    CXXFLAGS := ${CXXFLAGS} -pg -O3  
  52.    LDFLAGS  := ${LDFLAGS} -pg  
  53. endif  
  54.  
  55. #****************************************************************************  
  56. # Preprocessor directives  
  57. #****************************************************************************  
  58.   
  59. ifeq (YES, ${TINYXML_USE_STL})  
  60.   DEFS := -DTIXML_USE_STL  
  61. else  
  62.   DEFS :=  
  63. endif  
  64.  
  65. #****************************************************************************  
  66. # Include paths  
  67. #****************************************************************************  
  68.  
  69. #INCS := -I/usr/include/g++-2 -I/usr/local/include  
  70. INCS :=  
  71.  
  72.  
  73. #****************************************************************************  
  74. # Makefile code common to all platforms  
  75. #****************************************************************************  
  76.   
  77. CFLAGS   := ${CFLAGS}   ${DEFS}  
  78. CXXFLAGS := ${CXXFLAGS} ${DEFS}  
  79.  
  80. #****************************************************************************  
  81. # Targets of the build  
  82. #****************************************************************************  
  83.   
  84. OUTPUT := xmltest   
  85. LIB := libtinyxml.so  
  86. TEST:= tinyxml_test  
  87.   
  88. all: ${OUTPUT} ${LIB} ${TEST}  
  89.  
  90.  
  91. #****************************************************************************  
  92. # Source files  
  93. #****************************************************************************  
  94.   
  95. SRCS := tinyxml.cpp tinyxmlparser.cpp xmltest.cpp tinyxmlerror.cpp tinystr.cpp  
  96.  
  97. # Add on the sources for libraries  
  98. SRCS := ${SRCS}  
  99.   
  100. OBJS := $(addsuffix .o,$(basename ${SRCS}))  
  101. LIBOBJS := tinyxml.o tinyxmlparser.o tinyxmlerror.o tinystr.o  
  102.  
  103. #****************************************************************************  
  104. # Output  
  105. #****************************************************************************  
  106.   
  107. ${OUTPUT}: ${OBJS}  
  108.         ${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}  
  109.   
  110. ${LIB}: ${LIBOBJS}  
  111.         ar -r $@ ${LIBOBJS}  
  112.   
  113. ${TEST}: tinyxml_test.o  
  114.         ${LD} -o $@ ${LDFLAGS}  tinyxml_test.o -L${TINYXML_ROOT}  -ltinyxml  
  115.  
  116. #****************************************************************************  
  117. # common rules  
  118. #****************************************************************************  
  119.  
  120. # Rules for compiling source files to object files  
  121. %.o : %.cpp  
  122.         ${CXX} -c ${CXXFLAGS} ${INCS} $< -o $@  
  123.   
  124. %.o : %.c  
  125.         ${CC} -c ${CFLAGS} ${INCS} $< -o $@  
  126.   
  127. dist:  
  128.         bash makedistlinux  
  129.   
  130. clean:  
  131.         -rm -f core ${OBJS} ${OUTPUT} ${LIB} ${TEST}  
  132.   
  133. depend:  
  134.         #makedepend ${INCS} ${SRCS}  
  135.   
  136. tinyxml.o: tinyxml.h tinystr.h  
  137. tinyxmlparser.o: tinyxml.h tinystr.h  
  138. xmltest.o: tinyxml.h tinystr.h  
  139. tinyxmlerror.o: tinyxml.h tinystr.h   

 

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

[c-sharp] view plaincopy
  1. Persons  
  2. Person  
  3. 周星星  
  4. 20  
  5. 1   

0 0
原创粉丝点击