C++經過TinyXML類庫讀寫XML文件

DOM模型即文檔對象模型,是將整個文檔分紅多個元素(如書、章、節、段等),並利用樹型結構表示這些元素之間的順序關係以及嵌套包含關係。
ios


使用以前,須要先下載TinyXML類庫:http://download.csdn.net/detail/tennysonsky。也可在sourceforge上下載:http://sourceforge.net/projects/tinyxml/c++


而後解壓縮TinyXML後,將這六個文件添加到你的c++工程中,分別是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。測試


如本示例中,只有 main.cpp 纔是測試代碼:spa



編寫代碼時,只須要包含 tinyxml.h 頭文件便可,可是,編譯時卻須要把全部.cpp 文件都加上.net

示例代碼以下:指針

#include <stdio.h>
#include "tinyxml.h"
#include <iostream>
#include <cstring>
using namespace std;
/*
 TiXmlDocument:文檔類,它表明了整個xml文件
 TiXmlDeclaration:聲明類,它表示文件的聲明部分
 TiXmlComment:註釋類,它表示文件的註釋部分
 TiXmlElement:元素類,它是文件的主要部分,而且支持嵌套結構,通常使用這種結構來分類的存儲信息,它能夠包含屬性類和文本類
 TiXmlAttribute/TiXmlAttributeSet:元素屬性,它通常嵌套在元素中,用於記錄此元素的一些屬性
 TiXmlText:文本對象,它嵌套在某個元素內部
*/
//建立xml文件
int writeXmlFile()
{
 TiXmlDocument *writeDoc = new TiXmlDocument; //xml文檔指針
 
 //文檔格式聲明
 TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "UTF-8", "yes");
 writeDoc->LinkEndChild(decl); //寫入文檔
 int n = 3; //父節點個數
 TiXmlElement *RootElement = new TiXmlElement("Info");//根元素
 RootElement->SetAttribute("num", n); //屬性
 writeDoc->LinkEndChild(RootElement);
 
 for(int i=0; i<n; i++)//n個父節點
 {
  TiXmlElement *StuElement = new TiXmlElement("Stu");//Stu
  //設置屬性
  StuElement->SetAttribute("class","A");
  if(2 == i)
  {
    StuElement->SetAttribute("class","B");
  }
  
  StuElement->SetAttribute("id",i+1);
  StuElement->SetAttribute("flag", (i+1)*10);
  RootElement->LinkEndChild(StuElement);//父節點寫入文檔
 
  //姓名
  TiXmlElement *nameElement = new TiXmlElement("name");
  StuElement->LinkEndChild(nameElement);
  TiXmlText *nameContent = new TiXmlText("mike");
  nameElement->LinkEndChild(nameContent);
  
  //分數
  TiXmlElement *scoreElement = new TiXmlElement("score");
  StuElement->LinkEndChild(scoreElement);
  TiXmlText *scoreContent = new TiXmlText("88");
  scoreElement->LinkEndChild(scoreContent);
  
  //城市
  TiXmlElement *cityElement = new TiXmlElement("city");
  StuElement->LinkEndChild(cityElement);
  TiXmlText *cityContent = new TiXmlText("Shenzhen");
  cityElement->LinkEndChild(cityContent);
  
 }
 
 writeDoc->SaveFile("stu_info.xml");
 delete writeDoc;
 
 return 1;
}
//解析xml文件
int readXmlFile()
{
 TiXmlDocument mydoc("stu_info.xml");//xml文檔對象
 bool loadOk=mydoc.LoadFile();//加載文檔
 if(!loadOk)
 {
  cout<<"could not load the test file.Error:"<<mydoc.ErrorDesc()<<endl;
  exit(1);
 }
 TiXmlElement *RootElement=mydoc.RootElement(); //根元素, Info
 cout<< "[root name]" << RootElement->Value() <<"\n";
 
 TiXmlElement *pEle=RootElement;
 //遍歷該結點
 for(TiXmlElement *StuElement = pEle->FirstChildElement();//第一個子元素
  StuElement != NULL;
  StuElement = StuElement->NextSiblingElement())//下一個兄弟元素
 {
  // StuElement->Value() 節點名稱
  cout<< StuElement->Value() <<" ";
  TiXmlAttribute *pAttr=StuElement->FirstAttribute();//第一個屬性
  
  while( NULL != pAttr) //輸出全部屬性
  {
   cout<<pAttr->Name()<<":"<<pAttr->Value()<<" ";
   pAttr=pAttr->Next();
  }
  cout<<endl;
  
  //輸出子元素的值
  for(TiXmlElement *sonElement=StuElement->FirstChildElement();
  sonElement;
  sonElement=sonElement->NextSiblingElement())
  {
   cout<<sonElement->FirstChild()->Value()<<endl;
  }
 }
 
 return 1;
}
int main(int argc, char *argv[])
{
 
 writeXmlFile();
 printf("\nafter write\n");
 
 readXmlFile();
 return 0;
}

編譯運行結果以下:code



生成的xml文件內容以下:xml

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

<Info num="3">

    <Stu class="A" id="1" flag="10">

        <name>mike</name>

        <score>88</score>

        <city>Shenzhen</city>

    </Stu>

    <Stu class="A" id="2" flag="20">

        <name>mike</name>

        <score>88</score>

        <city>Shenzhen</city>

    </Stu>

    <Stu class="B" id="3" flag="30">

        <name>mike</name>

        <score>88</score>

        <city>Shenzhen</city>

    </Stu>

</Info>
相關文章
相關標籤/搜索