例1:node
先看一下,xml的結構數組
生成代碼app
CGeometry Parser; Parser.SetVersionEncoding(_T("version=\"1.0\" encoding=\"GBK\"")); CPSRAttribute aa; aa.m_strName = _T("PSRURI名稱"); aa.m_strValve = _T("6932B3BDE50FB1E0E0430A8EA810B1E00000值"); CResource a1; a1.push_back(aa); a1.push_back(aa); CResourceArr arr1; arr1.m_strType = _T("PSRType類型"); arr1.push_back(a1); arr1.push_back(a1); Parser.m_Attribute.m_strType = _T("PSRURIType類型"); Parser.m_Attribute.push_back(arr1); Parser.m_Attribute.push_back(arr1); CString strXml = _T("C:\\Users\\Administrator\\Desktop\\123.xml"); Parser.SaveXMLFile(CComVar(strXml));
FJGisXMLData.h函數
#pragma once #include "PrjStruct.h" class __declspec(dllexport) CPSRAttribute : public CHHElement { public: CPSRAttribute(){}; ~CPSRAttribute(){}; public: CString m_strName; CString m_strValve; CString m_strAlias; protected: static LPCTSTR node_Root; static LPCTSTR glb_strName; static LPCTSTR glb_strValve; static LPCTSTR glb_strAlias; protected: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; public: virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); virtual bool SaveNodeXML(OUT _HHAppend MSXML::IXMLDOMNodePtr pNode)const; static LPCTSTR GetSelfRoot(){return node_Root;}; }; class __declspec(dllexport) CLineSegment : public CHHElement { public: CLineSegment(){}; ~CLineSegment(){}; public: CString m_strPosList; protected: static LPCTSTR node_Root; static LPCTSTR glb_strPosList; protected: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; public: virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); virtual bool SaveNodeXML(OUT _HHAppend MSXML::IXMLDOMNodePtr pNode)const; static LPCTSTR GetSelfRoot(){return node_Root;}; }; class __declspec(dllexport) CResource : public CArrElementBase<CPSRAttribute> { public: CResource(){}; ~CResource(){}; public: CLineSegment m_Line; protected: static LPCTSTR node_Root; protected: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; public: virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); virtual bool SaveNodeXML(OUT _HHAppend MSXML::IXMLDOMNodePtr pNode)const; static LPCTSTR GetSelfRoot(){return node_Root;}; }; class __declspec(dllexport) CResourceArr : public CArrElementBase<CResource> { public: CResourceArr(){}; ~CResourceArr(){}; public: CString m_strType; CResource m_Resource; protected: static LPCTSTR node_Root; static LPCTSTR glb_strType; protected: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; public: virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); virtual bool SaveNodeXML(OUT _HHAppend MSXML::IXMLDOMNodePtr pNode)const; static LPCTSTR GetSelfRoot(){return node_Root;}; }; class __declspec(dllexport) CAttribute : public CArrElementBase<CResourceArr> { public: CAttribute(){} ~CAttribute(){} public: CString m_strType; CResourceArr m_ResourceArr; protected: static LPCTSTR node_Root;//節點 static LPCTSTR glb_strType; static LPCTSTR glb_ResourceArr; public: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); virtual bool SaveNodeXML(OUT _HHAppend MSXML::IXMLDOMNodePtr pNode)const; static LPCTSTR GetSelfRoot(){return node_Root;}; }; class __declspec(dllexport) CTile : public CHHElement { public: CTile(){} ~CTile(){} public: CString m_strToken; CString m_strCacheName; CString m_strUrl; CString m_strScale; CString m_strExtent; CString m_strLevel; CString m_strStartRow; CString m_strEndRow; CString m_strStartCol; CString m_strEndCol; CString m_strYAxisDirection; protected: static LPCTSTR node_Root;//節點 static LPCTSTR glb_strToken; static LPCTSTR glb_strCacheName; static LPCTSTR glb_strUrl; static LPCTSTR glb_strScale; static LPCTSTR glb_strExtent; static LPCTSTR glb_strLevel; static LPCTSTR glb_strStartRow; static LPCTSTR glb_strEndRow; static LPCTSTR glb_strStartCol; static LPCTSTR glb_strEndCol; static LPCTSTR glb_strYAxisDirection; public: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); virtual bool SaveNodeXML(OUT _HHAppend MSXML::IXMLDOMNodePtr pNode)const; static LPCTSTR GetSelfRoot(){return node_Root;}; }; class __declspec(dllexport) CGeometry : public CHHElement { public: CGeometry(){} ~CGeometry(){} public: CTile m_Tile; CAttribute m_Attribute; protected: static LPCTSTR node_Root;//節點 public: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); virtual bool SaveNodeXML(OUT _HHAppend MSXML::IXMLDOMNodePtr pNode)const; static LPCTSTR GetSelfRoot(){return node_Root;}; };
FJGisXMLData.cpp測試
#include "StdAfx.h" #include "FJGisXMLData.h" #include "XMLExt.h" ////////////////////////////////CGeometry///////////////////////////////// LPCTSTR CGeometry::node_Root = _T("MyGeometryQueryOutput" ); bool CGeometry::LoadNodeXML(MSXML::IXMLDOMNodePtr pNode) { try { CHHXMLNodePtr pNodePtr(pNode); if ( pNodePtr.SetToSubPtr(CTile::GetSelfRoot(),true) ) m_Tile.LoadNodeXML(pNodePtr.GetNodePtr()); if ( pNodePtr.SetToBrotherPtr(CAttribute::GetSelfRoot(),true) ) m_Attribute.LoadNodeXML(pNodePtr.GetNodePtr()); } catch(...) { HHAssert(false); return false; } return true; } bool CGeometry::SaveNodeXML(OUT _HHAppend MSXML::IXMLDOMNodePtr pNode) const { try { // 保存點 CHHXMLNodePtr pNodePtr(pNode); if ( pNodePtr.SetToSubPtr(CTile::GetSelfRoot(), true) ) m_Tile.SaveNodeXML(pNodePtr.GetNodePtr()); if ( pNodePtr.SetToBrotherPtr(CAttribute::GetSelfRoot(), true) ) m_Attribute.SaveNodeXML(pNodePtr.GetNodePtr()); } catch(...) { HHAssert(false); return false; } return true; } ////////////////////////////////////CTile//////////////////////////////// LPCTSTR CTile::node_Root = _T("Tile" ); LPCTSTR CTile::glb_strToken = _T("token" ); LPCTSTR CTile::glb_strCacheName = _T("tileCacheName" ); LPCTSTR CTile::glb_strUrl = _T("tileUrl" ); LPCTSTR CTile::glb_strScale = _T("scale" ); LPCTSTR CTile::glb_strExtent = _T("extent" ); LPCTSTR CTile::glb_strLevel = _T("level" ); LPCTSTR CTile::glb_strStartRow = _T("tileStartRow" ); LPCTSTR CTile::glb_strEndRow = _T("tileEndRow" ); LPCTSTR CTile::glb_strStartCol = _T("tileStartCol" ); LPCTSTR CTile::glb_strEndCol = _T("tileEndCol" ); LPCTSTR CTile::glb_strYAxisDirection = _T("tileYaxesDirection" ); bool CTile::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.GetNodeVar(glb_strToken, m_strToken); pNodePtr.GetNodeVar(glb_strCacheName, m_strCacheName); pNodePtr.GetNodeVar(glb_strUrl, m_strUrl); pNodePtr.GetNodeVar(glb_strLevel, m_strLevel); pNodePtr.GetNodeVar(glb_strStartRow, m_strStartRow); pNodePtr.GetNodeVar(glb_strEndRow, m_strEndRow); pNodePtr.GetNodeVar(glb_strStartCol, m_strStartCol); pNodePtr.GetNodeVar(glb_strEndCol, m_strEndCol); pNodePtr.GetNodeVar(glb_strYAxisDirection, m_strYAxisDirection); pNodePtr.GetNodeVar(glb_strScale, m_strScale); pNodePtr.GetNodeVar(glb_strExtent, m_strExtent); } catch(...) { HHAssert(false); return false; } return true; } bool CTile::SaveNodeXML( OUT _HHAppend MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetNodeVar(glb_strToken, m_strToken); pNodePtr.SetNodeVar(glb_strCacheName, m_strCacheName); pNodePtr.SetNodeVar(glb_strUrl, m_strUrl); pNodePtr.SetNodeVar(glb_strLevel, m_strLevel); pNodePtr.SetNodeVar(glb_strStartRow, m_strStartRow); pNodePtr.SetNodeVar(glb_strEndRow, m_strEndRow); pNodePtr.SetNodeVar(glb_strStartCol, m_strStartCol); pNodePtr.SetNodeVar(glb_strEndCol, m_strEndCol); pNodePtr.SetNodeVar(glb_strYAxisDirection, m_strYAxisDirection); pNodePtr.SetNodeVar(glb_strScale, m_strScale); pNodePtr.SetNodeVar(glb_strExtent, m_strExtent); } catch(...) { HHAssert(false); return false; } return true; } ////////////////////////////////////CAttribute//////////////////////////////// LPCTSTR CAttribute::node_Root = _T("GetAttributeByURIOutput" ); LPCTSTR CAttribute::glb_strType = _T("PSRURIType" ); bool CAttribute::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.GetNodeVar(glb_strType,m_strType); HHVerify2(__super::LoadNodeXML(pNodePtr.GetNodePtr()), return false;); } catch(...) { HHAssert(false); return false; } return true; } bool CAttribute::SaveNodeXML( OUT _HHAppend MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetNodeVar(glb_strType,m_strType); HHVerify2(__super::SaveNodeXML(pNodePtr.GetNodePtr()), return false;); } catch(...) { HHAssert(false); return false; } return true; } ////////////////////////////////////CLineSegment//////////////////////////////// LPCTSTR CLineSegment::node_Root = _T("LineStringSegment" ); LPCTSTR CLineSegment::glb_strPosList = _T("posList" ); bool CLineSegment::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.GetNodeVar(glb_strPosList, m_strPosList); } catch(...) { HHAssert(false); return false; } return true; } bool CLineSegment::SaveNodeXML( OUT _HHAppend MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetNodeVar(glb_strPosList, m_strPosList); } catch(...) { HHAssert(false); return false; } return true; } ////////////////////////////////////CPSRAttribute//////////////////////////////// LPCTSTR CPSRAttribute::node_Root = _T("PSRAttribute" ); LPCTSTR CPSRAttribute::glb_strName = _T("Name" ); LPCTSTR CPSRAttribute::glb_strValve = _T("Value" ); LPCTSTR CPSRAttribute::glb_strAlias = _T("Alias" ); bool CPSRAttribute::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.GetNodeVar(glb_strName, m_strName); pNodePtr.GetNodeVar(glb_strValve, m_strValve); pNodePtr.GetNodeVar(glb_strAlias, m_strAlias); } catch(...) { HHAssert(false); return false; } return true; } bool CPSRAttribute::SaveNodeXML( OUT _HHAppend MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetNodeVar(glb_strName, m_strName); pNodePtr.SetNodeVar(glb_strValve, m_strValve); pNodePtr.SetNodeVar(glb_strAlias, m_strAlias); } catch(...) { HHAssert(false); return false; } return true; } ////////////////////////////////////CResource//////////////////////////////// LPCTSTR CResource::node_Root = _T("PowerSystemResource" ); bool CResource::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { try { CHHXMLNodePtr pNodePtr(pNode); HHVerify2(__super::LoadNodeXML(pNodePtr.GetNodePtr()), return false;); if ( pNodePtr.SetToSubPtr(CLineSegment::GetSelfRoot(),true) ) m_Line.LoadNodeXML(pNodePtr.GetNodePtr()); } catch(...) { HHAssert(false); return false; } return true; } bool CResource::SaveNodeXML( OUT _HHAppend MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); HHVerify2(__super::SaveNodeXML(pNodePtr.GetNodePtr()), return false;); if ( pNodePtr.SetToSubPtr(CLineSegment::GetSelfRoot(),true) ) m_Line.SaveNodeXML(pNodePtr.GetNodePtr()); } catch(...) { HHAssert(false); return false; } return true; } ////////////////////////////////////CResourceArr//////////////////////////////// LPCTSTR CResourceArr::node_Root = _T("PowerSystemResourceArray"); LPCTSTR CResourceArr::glb_strType = _T("PSRType"); bool CResourceArr::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.GetNodeVar(glb_strType,m_strType); HHVerify2(__super::LoadNodeXML(pNodePtr.GetNodePtr()), return false;); } catch(...) { HHAssert(false); return false; } return true; } bool CResourceArr::SaveNodeXML( OUT _HHAppend MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetNodeVar(glb_strType,m_strType); HHVerify2(__super::SaveNodeXML(pNodePtr.GetNodePtr()), return false;); } catch(...) { HHAssert(false); return false; } return true; }
例2:ui
xml的結構this
具體如何填充數據看博客:https://my.oschina.net/u/2930533/blog/1573584編碼
.h.net
#pragma once #include <vector> #include <msxml2.h> #import <MSXML.DLL> named_guids class CIniFileN; ///////////////////////////////////////////////////////////////////////////////////////////// // class CXMLElement 保存、合成XML格式的基類 ///////////////////////////////////////////////////////////////////////////////////////////// interface __declspec(dllexport) CXMLElement { public: CXMLElement():m_strverencoding(_T("version=\'1.0\'")){} virtual ~CXMLElement(){} public: // 讀取XML格式的字符串,給成員賦值 virtual bool LoadXML(IN LPCTSTR lpXMLStr)=0; virtual bool LoadXML(MSXML::IXMLDOMNodePtr pNode)=0; // 將成員值,保存爲XML格式 virtual bool SaveXML( CString* strXML)const=0; virtual bool SaveXML( MSXML::IXMLDOMNodePtr pNode)const=0; // 函數自測試 virtual bool SelfTest(){return false;}; // 取得根節點名 LPCTSTR GetRootNodeIn()const{return GetRootNode();} public: // 保存到XML文件 virtual bool SaveXMLFile(LPCTSTR lpFileName)const; // 讀取XML文件 virtual bool LoadXMLFile(LPCTSTR lpFileName); // [須要GetRootNode()節點] virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode)=0; // 將成員值,保存爲XML格式 virtual bool SaveNodeXML( MSXML::IXMLDOMNodePtr pNode)const=0; // 取得根節點名 virtual LPCTSTR GetRootNode()const=0; // 設置xml文件頭部的version='1.0' encoding='GB2312',默認爲 version='1.0',不指定編碼方式 void SetVersionEncoding(LPCTSTR lpverencod); private: CString m_strverencoding; }; ///////////////////////////////////////////////////////////////////////////////////////////// // struct CHHElement: public CXMLElement 對基類的次級封裝,目的統一完成某幾個函數 ///////////////////////////////////////////////////////////////////////////////////////////// struct __declspec(dllexport) CHHElement: public CXMLElement { public: CHHElement(); ~CHHElement(); public: // 讀取XML格式的字符串,給成員賦值 virtual bool LoadXML(IN LPCTSTR lpXMLStr); virtual bool LoadXML(MSXML::IXMLDOMNodePtr pNode){return LoadNodeXML(pNode);} // 將成員值,保存爲XML格式 virtual bool SaveXML(CString* strXML)const; virtual bool SaveXML(MSXML::IXMLDOMNodePtr pNode)const{return SaveNodeXML(pNode);} }; ///////////////////////////////////////////////////////////////////////////////////////////// // struct CArrBase 數組封裝 ///////////////////////////////////////////////////////////////////////////////////////////// template<class T> struct __declspec(dllexport) CArrBase: public std::vector<T> { public: // 查找序號 template<class _Pr> int Find(_Pr op )const { std::vector<T>::const_iterator mIter = std::find_if(begin(), end(), op); if ( mIter == end() ) return -1; return mIter-begin(); } template<class _Pr> std::vector<int> FindAll(_Pr op )const { std::vector<int> arrRet; for (int i=0; i<(int)size(); ++i) { if ( op(at(i))) { arrRet.push_back(i); } } return arrRet; } // 排序 template<class _Pr> __inline void Sort(_Pr op) { std::sort(begin(), end(), op); } __inline void Sort(){std::sort(begin(), end());} // 清除指定序號記錄 __inline virtual void RemoveAt(int nIndex) { if ( nIndex>=0 && nIndex<(int)size() ) { erase(begin()+nIndex); } } }; ///////////////////////////////////////////////////////////////////////////////////////////// // struct CArrElementTest: protected CArrBase<CXMLElement*> 元素自測試數組 ///////////////////////////////////////////////////////////////////////////////////////////// struct __declspec(dllexport) CArrElementTest: protected CArrBase<CXMLElement*> { public: CArrElementTest(); virtual ~CArrElementTest(); public: // 添加一個測試實體 template<class T> void Add() { T* pItem = new T; push_back(pItem); } // 清除所有 virtual void clear(); // 進行自測試 virtual void TestAll(); }; ///////////////////////////////////////////////////////////////////////////////////////////// // struct CArrElementBase: public CHHElement, public CArrBase<T> 元素數組操做封裝 ///////////////////////////////////////////////////////////////////////////////////////////// template<class T> struct __declspec(dllexport) CArrElementBase: public CHHElement, public CArrBase<T> { typedef const CArrBase<T>* _CArrBasePtr; public: // 重載等號/不等號 bool operator == (const CArrElementBase&p1 )const { return *((_CArrBasePtr)this)==*((_CArrBasePtr)(&p1));} bool operator != (const CArrElementBase&p1 )const { return !(*this==p1); } protected: // 讀取XML格式的字符串,給成員賦值 virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode) { try { // 解析取每一個元素 T tmp; MSXML::IXMLDOMNodeListPtr pNodeList = pNode->selectNodes(tmp.GetRootNodeIn()); const int nCount = pNodeList->Getlength(); if ( nCount < 1 ) return true; // 循環建立每一個元素信息 for (int i=0; i<nCount; ++i) { // 取得節點信息 T mInfo; if(!mInfo.LoadXML(pNodeList->Getitem(i))) continue; push_back(mInfo); } } catch(...) { return false; } return true; } // 將成員值,保存爲XML格式 virtual bool SaveNodeXML( MSXML::IXMLDOMNodePtr pNode)const { const int nCount = size(); for (int i=0; i<nCount; ++i) { // 一條信息 const T& mItem = at(i); try { // 建立一個節點 MSXML::IXMLDOMNodePtr pNew = pNode->GetownerDocument()->createElement(mItem.GetRootNodeIn()); if(!pNode->appendChild(pNew)) continue; // 保存節點信息 if(!mItem.SaveXML(pNew)) return false; } catch(...) { continue; } } return true; } }; //樁位信息 class __declspec(dllexport) CPointInfo : public CHHElement { public: CPointInfo(){} ~CPointInfo(){} public: CString m_strCoord; CString m_strName; protected: static LPCTSTR node_Point;//節點 static LPCTSTR glb_strCoord; static LPCTSTR glb_strName; public: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); virtual bool SaveNodeXML( MSXML::IXMLDOMNodePtr pNode)const; public: static LPCTSTR GetSelfRoot(); }; class __declspec(dllexport) CPlacemark : public CHHElement { public: CPlacemark(){} ~CPlacemark(){} public: CPointInfo m_Point; CString m_strName; protected: static LPCTSTR node_Placemark;//節點 static LPCTSTR glb_strName; public: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); virtual bool SaveNodeXML( MSXML::IXMLDOMNodePtr pNode)const; public: static LPCTSTR GetSelfRoot(); }; class __declspec(dllexport) CHHDocument : CHHElement { public: CHHDocument(); virtual~CHHDocument(); public: CPlacemark m_Placemark; protected: static LPCTSTR node_Document;//節點 protected: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; public: //讀取XML格式的字符串 virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); //保存成xml格式 virtual bool SaveNodeXML(MSXML::IXMLDOMNodePtr pNode)const; public: static LPCTSTR GetSelfRoot(); }; class __declspec(dllexport) CKmlXmlns : public CHHElement { public: CKmlXmlns(); virtual~CKmlXmlns(){} public: CString m_strXmlns; CHHDocument m_Document; protected: static LPCTSTR node_Kml;//節點 static LPCTSTR glb_strXmlns; protected: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; public: //讀取XML格式的字符串 virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); //保存成xml格式 virtual bool SaveNodeXML(MSXML::IXMLDOMNodePtr pNode)const; public: static LPCTSTR GetSelfRoot(); }; //座標轉換參數 class __declspec(dllexport) CCoordTrans : public CHHElement { public: CCoordTrans(); virtual~CCoordTrans(){} public: CString m_strName; CString m_strCur; CString m_strTar; double m_dDX; double m_dDY; double m_dDZ; double m_dRX; double m_dRY; double m_dRZ; double m_dK; protected: static LPCTSTR node_root;//節點 static LPCTSTR glb_strName; static LPCTSTR glb_strCur; static LPCTSTR glb_strTar; static LPCTSTR glb_strDX; static LPCTSTR glb_strDY; static LPCTSTR glb_strDZ; static LPCTSTR glb_strRX; static LPCTSTR glb_strRY; static LPCTSTR glb_strRZ; static LPCTSTR glb_strK; protected: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; public: //讀取XML格式的字符串 virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); //保存成xml格式 virtual bool SaveNodeXML(MSXML::IXMLDOMNodePtr pNode)const; public: static LPCTSTR GetSelfRoot(); }; struct __declspec(dllexport)CCoordTransArr: public CArrElementBase<CCoordTrans> { public: CCoordTransArr(); virtual ~CCoordTransArr(); protected: static LPCTSTR node_root;//!< 根節點 protected: // 取得根節點名 virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; // [須要node_root節點] virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); // 將成員值,保存爲XML格式 virtual bool SaveNodeXML(MSXML::IXMLDOMNodePtr pNode)const; public: // 取得根結點 static LPCTSTR GetSelfRoot(); }; //多段線圖層 class __declspec(dllexport) CPolyLineLayer : public CHHElement { public: CPolyLineLayer(); virtual~CPolyLineLayer(){} public: CString m_strLayerName; protected: static LPCTSTR node_root;//節點 static LPCTSTR glb_strLayerName; protected: virtual LPCTSTR GetRootNode()const{return GetSelfRoot();}; public: //讀取XML格式的字符串 virtual bool LoadNodeXML(MSXML::IXMLDOMNodePtr pNode); //保存成xml格式 virtual bool SaveNodeXML(MSXML::IXMLDOMNodePtr pNode)const; public: static LPCTSTR GetSelfRoot(); };
.cppcode
#include "StdAfx.h" #include "PrjStruct.h" #include "XMLExt.h" #import <MSXML.DLL> named_guids ////////////////////////////////////////////////////////////////////////// //! @brief //! @param LPCTSTR lpFileName //! @exception //! @return bool //! @sa // ----------------------------------------------------------------------- // 版本: 1.0 ////////////////////////////////////////////////////////////////////////// bool CXMLElement::SaveXMLFile( LPCTSTR lpFileName ) const { try { // 建立根節點 MSXML::IXMLDOMDocumentPtr pXmlDoc; if(pXmlDoc.CreateInstance(MSXML::CLSID_DOMDocument) != S_OK) return false; MSXML::IXMLDOMProcessingInstructionPtr pi = pXmlDoc->createProcessingInstruction(_T("xml"), (LPCTSTR)m_strverencoding); _variant_t vNullVal; vNullVal.vt = VT_NULL; pXmlDoc->insertBefore(pi, vNullVal); MSXML::IXMLDOMNodePtr pRoot = pXmlDoc->createElement(GetRootNode()); if(!pXmlDoc->appendChild(pRoot)) return false; // 向節點中寫信息 if(!SaveNodeXML(pRoot)) return false; // 取得XML信息 pXmlDoc->save(lpFileName); } catch(...) { return false; } return true; } ////////////////////////////////////////////////////////////////////////// //! @brief 讀取XML文件 //! @param LPCTSTR lpFileName XML文件名稱 //! @exception //! @return bool //! @sa // ----------------------------------------------------------------------- // 版本: 1.0 ////////////////////////////////////////////////////////////////////////// bool CXMLElement::LoadXMLFile( LPCTSTR lpFileName ) { try { // 建立根節點 MSXML::IXMLDOMDocumentPtr pXmlDoc; if(pXmlDoc.CreateInstance(MSXML::CLSID_DOMDocument) != S_OK) return false; // 取得XML信息 if(!pXmlDoc->load(lpFileName)) return false; return LoadNodeXML(pXmlDoc->GetdocumentElement()); } catch(...) { return false; } return true; } void CXMLElement::SetVersionEncoding(LPCTSTR lpverencod) { if (lpverencod != NULL && lpverencod[0] != _T('\0')) m_strverencoding = lpverencod; } ///////////////////////////////////////////////////////////////////////////////////////////// // struct CHHElement: public CXMLElement 對基類的次級封裝,目的統一完成某幾個函數 ///////////////////////////////////////////////////////////////////////////////////////////// CHHElement::CHHElement() { } CHHElement::~CHHElement() { } // 讀取XML格式的字符串,給成員賦值 bool CHHElement::LoadXML(IN LPCTSTR lpXMLStr) { try { // 載入字符串 MSXML::IXMLDOMDocumentPtr pXmlDoc; if(pXmlDoc.CreateInstance(MSXML::CLSID_DOMDocument) != S_OK) return false; if(!pXmlDoc->loadXML(lpXMLStr)); return false; if(!LoadNodeXML(pXmlDoc->GetdocumentElement())) return false; } catch(...) { return false; } return true; } // 將成員值,保存爲XML格式 bool CHHElement::SaveXML(CString* strXML)const { try { // 建立根節點 MSXML::IXMLDOMDocumentPtr pXmlDoc; if(pXmlDoc.CreateInstance(MSXML::CLSID_DOMDocument) != S_OK ) return false; MSXML::IXMLDOMNodePtr pRoot = pXmlDoc->createElement(GetRootNode()); if(!pXmlDoc->appendChild(pRoot)) return false; // 向節點中寫信息 if(!SaveNodeXML(pRoot)) return false; // 取得XML信息 *strXML = (LPCTSTR)pXmlDoc->Getxml(); } catch(...) { return false; } return true; } ///////////////////////////////////////////////////////////////////////////////////////////// // struct CArrElementTest: protected CArrBase<CXMLElement*> 元素自測試數組 ///////////////////////////////////////////////////////////////////////////////////////////// CArrElementTest::CArrElementTest() { } CArrElementTest::~CArrElementTest() { clear(); } void CArrElementTest::clear() { const int nCount = size(); for (int i=0; i<nCount; ++i) { delete at(i); } clear(); } // 進行自測試 void CArrElementTest::TestAll() { const int nCount = size(); for (int i=0; i<nCount; ++i) { if(!at(i)->SelfTest()) return; } } //////////////////////////////樁位信息///////////////////////// LPCTSTR CHHDocument::node_Document = _T("Document"); CHHDocument::CHHDocument() { } CHHDocument::~CHHDocument() { } LPCTSTR CHHDocument::GetSelfRoot() { return node_Document; } bool CHHDocument::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { return true; } bool CHHDocument::SaveNodeXML(MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetToSubPtr(CPlacemark::GetSelfRoot(), true); return m_Placemark.SaveNodeXML(pNodePtr.GetNodePtr()); } catch(...) { return false; } return true; } LPCTSTR CPointInfo::node_Point = _T("LineString" ); LPCTSTR CPointInfo::glb_strCoord = _T("coordinates" ); LPCTSTR CPointInfo::glb_strName = _T("zhuangs" ); LPCTSTR CPointInfo::GetSelfRoot() { return node_Point; } bool CPointInfo::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { try { CHHXMLNodePtr pNodePtr(pNode); } catch(...) { return false; } return true; } bool CPointInfo::SaveNodeXML(MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetNodeVar(glb_strName,m_strName); pNodePtr.SetNodeVar(glb_strCoord,m_strCoord); } catch(...) { return false; } return true; } LPCTSTR CKmlXmlns::node_Kml = _T("kml" ); LPCTSTR CKmlXmlns::glb_strXmlns = _T("xmlns"); CKmlXmlns::CKmlXmlns() { m_strXmlns = _T("http://www.opengis.net/kml/2.2"); } bool CKmlXmlns::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { return true; } bool CKmlXmlns::SaveNodeXML( MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetAttribute(glb_strXmlns, m_strXmlns); pNodePtr.SetToSubPtr(CHHDocument::GetSelfRoot(), true); return m_Document.SaveNodeXML(pNodePtr.GetNodePtr()); } catch(...) { return false; } return true; } LPCTSTR CKmlXmlns::GetSelfRoot() { return node_Kml; } LPCTSTR CPlacemark::node_Placemark = _T("Placemark"); LPCTSTR CPlacemark::glb_strName = _T("name"); bool CPlacemark::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { return true; } bool CPlacemark::SaveNodeXML( MSXML::IXMLDOMNodePtr pNode ) const { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetNodeVar(glb_strName,m_strName ); pNodePtr.SetToSubPtr(CPointInfo::GetSelfRoot(), true); return m_Point.SaveNodeXML(pNodePtr.GetNodePtr()); } catch(...) { return false; } return true; } LPCTSTR CPlacemark::GetSelfRoot() { return node_Placemark; } ////////////////////////////////////////座標轉換參數///////////////// LPCTSTR CCoordTransArr::node_root = _T("CoordTranss");// 根節點 CCoordTransArr::CCoordTransArr() { } CCoordTransArr::~CCoordTransArr() { } // 讀取XML格式的字符串,給成員賦值 bool CCoordTransArr::LoadNodeXML(MSXML::IXMLDOMNodePtr pNode) { try { if ( !__super::LoadNodeXML(pNode) ) return false; } catch(...) { return false; } return true; } // 將成員值,保存爲XML格式 bool CCoordTransArr::SaveNodeXML( MSXML::IXMLDOMNodePtr pNode)const { try { if ( !__super::SaveNodeXML(pNode) ) return false; } catch(...) { return false; } return true; } LPCTSTR CCoordTransArr::GetSelfRoot() { return node_root; } LPCTSTR CCoordTrans::node_root = _T("CoordTrans"); LPCTSTR CCoordTrans::glb_strName = _T("Name" ); LPCTSTR CCoordTrans::glb_strCur = _T("CurCoord" ); LPCTSTR CCoordTrans::glb_strTar = _T("TarCoord" ); LPCTSTR CCoordTrans::glb_strDX = _T("DX" ); LPCTSTR CCoordTrans::glb_strDY = _T("DY" ); LPCTSTR CCoordTrans::glb_strDZ = _T("DZ" ); LPCTSTR CCoordTrans::glb_strRX = _T("RX" ); LPCTSTR CCoordTrans::glb_strRY = _T("RY" ); LPCTSTR CCoordTrans::glb_strRZ = _T("RZ" ); LPCTSTR CCoordTrans::glb_strK = _T("K" ); CCoordTrans::CCoordTrans() { } bool CCoordTrans::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.GetAttribute(glb_strName,m_strName); pNodePtr.GetAttribute(glb_strCur,m_strCur); pNodePtr.GetAttribute(glb_strTar,m_strTar); pNodePtr.GetAttribute(glb_strDX,m_dDX); pNodePtr.GetAttribute(glb_strDY,m_dDY); pNodePtr.GetAttribute(glb_strDZ,m_dDZ); pNodePtr.GetAttribute(glb_strRX,m_dRX); pNodePtr.GetAttribute(glb_strRY,m_dRY); pNodePtr.GetAttribute(glb_strRZ,m_dRZ); pNodePtr.GetAttribute(glb_strK,m_dK); } catch(...) { return false; } return true; } bool CCoordTrans::SaveNodeXML( MSXML::IXMLDOMNodePtr pNode ) const { try { CString strTemp; CHHXMLNodePtr pNodePtr(pNode); pNodePtr.SetAttribute(glb_strName,m_strName); pNodePtr.SetAttribute(glb_strCur,m_strCur); pNodePtr.SetAttribute(glb_strTar,m_strTar); strTemp.Format(_T("%.20f"),m_dDX); pNodePtr.SetAttribute(glb_strDX,strTemp); strTemp.Format(_T("%.20f"),m_dDY); pNodePtr.SetAttribute(glb_strDY,strTemp); strTemp.Format(_T("%.20f"),m_dDZ); pNodePtr.SetAttribute(glb_strDZ,strTemp); strTemp.Format(_T("%.20f"),m_dRX); pNodePtr.SetAttribute(glb_strRX,strTemp); strTemp.Format(_T("%.20f"),m_dRY); pNodePtr.SetAttribute(glb_strRY,strTemp); strTemp.Format(_T("%.20f"),m_dRZ); pNodePtr.SetAttribute(glb_strRZ,strTemp); strTemp.Format(_T("%.20f"),m_dK); pNodePtr.SetAttribute(glb_strK,strTemp); } catch(...) { return false; } return true; } LPCTSTR CCoordTrans::GetSelfRoot() { return node_root; } /////////////////////////////////////多段線圖層///////////////////////////// LPCTSTR CPolyLineLayer::node_root = _T("Layer"); LPCTSTR CPolyLineLayer::glb_strLayerName = _T("Name" ); CPolyLineLayer::CPolyLineLayer() { } bool CPolyLineLayer::LoadNodeXML( MSXML::IXMLDOMNodePtr pNode ) { try { CHHXMLNodePtr pNodePtr(pNode); pNodePtr.GetNodeVar(glb_strLayerName,m_strLayerName); } catch(...) { return false; } return true; } bool CPolyLineLayer::SaveNodeXML( MSXML::IXMLDOMNodePtr pNode ) const { return true; } LPCTSTR CPolyLineLayer::GetSelfRoot() { return node_root; }