1. 程式人生 > >c++ 使用TinyXML 讀取xml資料

c++ 使用TinyXML 讀取xml資料

1  TinyXml 下載地址  點選開啟連結

2  下載完成後解壓縮 將tinystr.cpp ,tinystr.h,tinyxml.cpp,tinyxml.h,tinyxmlerror.cpp,tinyxmlparser.cpp 加入工程中

#include<iostream>
#include "tinystr.h"
#include "tinyxml.h"
#include<string>
#include<windows.h>
using namespace std;
#define  MAX_PATH1 100
/*獲取exe檔案路徑*/
string GetAppPath()
{
	 char spath[MAX_PATH1] = {0};
	 GetModuleFileName(NULL,spath,MAX_PATH);
	 string path(spath);
	 cout<<path<<endl;
     int m= path.find_last_of('\\');
	 path = path.substr(0,m);
	 cout<<path<<endl;
	 return path;
	  
	
}

/*建立一個xml資料*/

bool CreateXmlFile(string&  szFileName)
{
	 try
	 {
		  TiXmlDocument *myDocument = new TiXmlDocument();
		  TiXmlElement *RootElement = new TiXmlElement("Persons");
		  myDocument->LinkEndChild(RootElement);
		  TiXmlElement *PersonElement = new TiXmlElement("Person");
		  RootElement->LinkEndChild(PersonElement);
		  PersonElement->SetAttribute("ID",1);
		  TiXmlElement *PersonName  = new TiXmlElement("name");
		  TiXmlElement *PersonAge = new TiXmlElement("age");
		  PersonElement->LinkEndChild(PersonName);
		  PersonElement->LinkEndChild(PersonAge);

		  TiXmlText *NameContent = new TiXmlText("小家");
		  TiXmlText *AgeContent = new TiXmlText("23");
		  PersonName->LinkEndChild(NameContent);
		  PersonAge->LinkEndChild(AgeContent);
		  string spath = GetAppPath();
		  spath += "\\";
		  spath += szFileName;
		  myDocument->SaveFile(spath.c_str());


	 }
	 catch (string e)
	 {
	 	return false;
	 }

	 return true;
	


}


bool ReadXmlFile(string& szFileName)
{
	try
	{
		 string apppath = GetAppPath();
		 apppath += "\\";
		 apppath += szFileName;
		 TiXmlDocument *myDocument = new  TiXmlDocument(apppath.c_str());
		 myDocument->LoadFile();
		 TiXmlElement *RootElement = myDocument->RootElement();
		 cout<<RootElement->Value()<<endl;
		 TiXmlElement *FirstPerson = RootElement->FirstChildElement();
		 
		 TiXmlElement *NameElement = FirstPerson->FirstChildElement();
		 TiXmlElement *AgeElement = NameElement->NextSiblingElement();
		 TiXmlAttribute *personattri = FirstPerson->FirstAttribute();

		 cout<<NameElement->FirstChild()->Value()<<endl;
		 cout<<AgeElement->FirstChild()->Value()<<endl;
		 cout<<personattri->Value()<<endl;
	}
	catch(string e)
	{
		return false;

	}

	return true;



}


int main()
{
	string str = "123.txt";
  if(CreateXmlFile(str))
	  cout<<"ok"<<endl;
  else
	  cout<<"0"<<endl;

  if(ReadXmlFile(str))
	  cout<<"ok"<<endl;
  else
	  cout<<"0"<<endl;



  return 0;

}

相關推薦

c++ 使用TinyXML 讀取xml資料

1  TinyXml 下載地址  點選開啟連結 2  下載完成後解壓縮 將tinystr.cpp ,tinystr.h,tinyxml.cpp,tinyxml.h,tinyxmlerror.cpp,tinyxmlparser.cpp 加入工程中 3 #include&l

C#】解決進行反序列化時出錯:。InnerException 訊息是“反序列化物件 屬於型別 System.String 時出現錯誤。讀取 XML 資料時,超出最大字串內容長度配額 (8192)。

解決:.NET進行反序列化時出錯:。InnerException 訊息是“反序列化物件 屬於型別 System.String 時出現錯誤。讀取 XML 資料時,超出最大字串內容長度配額 (8192)

C#(.net)讀取excel資料 轉為datatable

#region 讀取Excel中的資料         /// <summary>          /// 讀取Excel中的資料 支援表頭(.xlsx)   不支援表頭(.xls) &

c++:boost讀取xml

int   load(const std::string &strFilePath, const std::string &strFileName){ m_mapParam.clear(); string strConfFullPath; if (kbdSuccess !=

C# NPOI讀取Excel資料

using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Wi

C語言讀取指定資料夾下的所有檔案(各種資訊)

前言: 由於在做老師佈置的作業過程中需要用到讀取資料夾下所有檔案的功能,由於之前還沒有用 C 語言試過,後面還是通過各種百度之後才解決掉。在這裡做個記錄,以後查閱方便。 分析: 1、標頭檔案: 要實現我們瀏覽某資料夾下各種檔案資訊的功能,我們首先得引

用php讀取xml資料

今天工作上碰到一個問題 由於我們的專案資料太少 所以需要從web search那邊借調資料,他們只給我們提供了一個xml的介面。因此,我們需要把xml的資料轉化成html呈現給大家。由於專案是基於php的,所以就摒棄了用js來讀取xml選擇了繼續使用php。不過,我以前從來沒有做過此類的嘗試 所以找

TinyXML讀取XML檔案內容 [大三四八九月實習]

1 XML檔案的節點與元素 為什麼要探討這個問題呢,因為在TinyXML類的成員函式中,有指向下一個結點的成員函式(NextSibling)有指向下一個元素的成員函式(NextSiblingElement),元素在XML檔案基本結構中基本已經形成概念,現在又冒出一個結點。還

Ajax讀取XML資料

最早的Ajax推出的時候實際上只給出了兩類資料 文字資料:最初只是虛妄簡單的返回一些處理標記(後來功能得到了擴充) XML資料:是為了攜帶更多的資料返回, 假設有如下的XML資料 <?xml version="1.

Jquery Ajax 讀取XML 資料

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="JqueryAjax_Default" %> <!DOCTYPE html PUBLIC

C# 中讀取資料庫資料的問題

程式首先是要判斷一天資訊是否存在於資料庫中,然後若存在則將資料展示在頁面的ListView中。 用來兩種方法: 1.用Adapter private static string strConnect = "Provider = Microsoft.Jet.OLEDB.4

Js跨域名讀取xml資料

 <%p = "http://222.186.24.eee:812/liveNow.xml"Response.BinaryWrite ZQcnGet(p)Response.Flush Function ZQcnGet(url) Set Retrieval = Creat

C#解析讀取XML檔案,並且對XML檔案進行操作

如果不懂XML文件結構,請點選移步這裡 在綜合搜尋了網上的文章之後發現,有很多文章的講解博主看的一知半解,並且有些方法已經找不到了,所以就有了自己整理一下學習成果,同時通過部落格共享成果 目前還在學習中,先佔坑,後續補上 經過昨天的實驗和測試,補上C#XML的資訊讀取操

Windows Tinyxml讀取xml檔案小結

TinyXML是一個開源的解析XML的解析庫,能夠用於C++,能夠在Windows或Linux中編譯。這個解析庫的模型通過解析XML檔案,然後在記憶體中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。DOM模型即文件物件模型,是將整個文件分成多個元素(如書、章、節、段等

InnerException 訊息是“反序列化物件 屬於型別 *** 時出現錯誤。讀取 XML 資料時,超出最大字串內容長度配額 (8192)。(注意細節)

WEB站點在呼叫我們WCF服務的時候,只要傳入的引數過長,就報如下錯誤: 格式化程式嘗試對訊息反序列化時引發異常: 嘗試對引數 http://tempuri.org/ 進行反序列化時出錯: formDataXml。InnerException 訊息是“反序列化物件 屬於型別

C#/ASP.NET Xml多級資料讀取

<Data> <Project> <Item Id="51351132-59a7-4c0b-909d-51b89b1c3159" IsDefault="1" Path="D:\TDDOWNLOAD\Source\Report1.App" /> &l

C#讀取XML檔案資料和把資料儲存至xml的方法

原文在百度知道中,來源於多個網友。 新浪微博:http://blog.sina.com.cn/s/blog_ad7fd0f4010180md.html (一) 儲存 var xml =XElement.Load(@"路徑");xml.Element("節點名字").AddA

C# 讀取 XML文件

doc des img 分享 child add 類型 blog pri public static List<XmlData> GetXmlData( string path,string name) { List<

C#讀取xml文件靜態類

col tex oca spa void n) bin log element internal static class XmlUtil { internal static XmlDocument doc; interna

淺談JS中的!=、== 、!==、===的用法和區別 JS中Null與Undefined的區別 讀取XML文件 獲取路徑的方式 C#中Cookie,Session,Application的用法與區別? c#反射 抽象工廠

main 收集 data- 時間設置 oba ase pdo 簡單工廠模式 1.0 var num = 1; var str = ‘1‘; var test = 1; test == num //true 相同類型 相同值 te