有 一Students.xml 内容如下:
<Class name="计算机软件班"> <Students> <student name="张三" studentNo="13031001" sex="男" age="22"> <phone>88208888</phone> <address>西安市太白南路二号</address> </student> <student name="李四" studentNo="13031002" sex="男" age="20"> <phone>88206666</phone> <address>西安市光华路</address> </student> </Students> </Class>
用tiny xml解析器对其进行解析,tiny xml解析器的代码可以网上下载到,包含文件4个cpp文件和2个h文件如下:
tinystr.cpp,tinyxml.cpp,tinyxmlerror.cpp,tinyxmlparser.cpp,tinyxml.h,tinystr.h
解析代码xmltest.cpp如下:
#include <iostream> #include <string> #include "tinyxml.h" using namespace std; using std::string; int main() { /* * 加载需要解析的xml文件 * */ TiXmlDocument* myDocument = new TiXmlDocument(); myDocument->LoadFile("Students.xml"); /* *获取根元素这里对应的是:Class name * */ TiXmlElement* rootElement = myDocument->RootElement(); /* * 获取根的属性值:计算机软件班 * */ TiXmlAttribute* attributeOfroot = rootElement->FirstAttribute(); cout << rootElement->Value() << " " << attributeOfroot->Name() << ":" << attributeOfroot->Value() << endl; /* * 获取子元素 * */ TiXmlElement* studentsElement = rootElement->FirstChildElement(); /* * 获取子元素中的第一个子元素 * */ TiXmlElement* studentElement = studentsElement->FirstChildElement(); /* * 遍历子元素 * */ while ( studentElement ) { TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute(); //获得student的name属性 while ( attributeOfStudent ) { std::cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value() << std::endl; attributeOfStudent = attributeOfStudent->Next(); } TiXmlElement* phoneElement = studentElement->FirstChildElement();//获得student的phone元素 std::cout << "phone" << " : " << phoneElement->GetText() << std::endl; TiXmlElement* addressElement = phoneElement->NextSiblingElement(); std::cout << "address" << " : " << addressElement->GetText() << std::endl; studentElement = studentElement->NextSiblingElement(); } return 0; }
Makfile 文件如下: #**************************************************************************** # # 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 := NO # 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 all: ${OUTPUT} #**************************************************************************** # 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})) #**************************************************************************** # Output #**************************************************************************** ${OUTPUT}: ${OBJS} ${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS} #**************************************************************************** # 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} 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