04-28 06:01
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[C/C++] MFC / XML Parsing Using Boost 본문

Programming/C&C++

[C/C++] MFC / XML Parsing Using Boost

cinema4dr12 2014. 9. 1. 16:17


1. 헤더파일 추가

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>


2. 네임스페이스 지정

using namespace boost;
using namespace boost::property_tree;


3. 전역함수 추가

const ptree& empty_ptree(){
    static ptree t;
    return t;
}


4. 변수 선언

ptree ptr;
CString myXMLFileName = _T("");
CString myXMLFilePath = _T("");
CString strAttr = _T("");
CString strAttrValue = _T("");
CString strTagName = _T("");


5. xml 파일명 지정

myXMLFileName = _T("PropertySettings.config");


6. XML 파일 경로 지정

myXMLFilePath = _T("D:\\Project\\Alpius\\branches\\alpius_xml_1.0.1\\Alpius\\Settings\\Configuration\\") + myXMLFileName;


7. XML 파일 스트림 읽기: read_xml 함수를 실행하여 XML 정보를 ptr에 저장함

read_xml( ConvertUnicode2String( myXMLFilePath ), ptr );


8. Attribute를 읽을 XML 태그 정의

const ptree & formats = ptr.get_child("alpius900.stage-setup.mode-config.dlg-hardware.group-im.group-motorLimit", empty_ptree());


9. BOOST_FOREACH MACRO를 실행하여 해당 XML 태그의 attribute를 읽는다. First는 attribute를 Second는 해당 attribute의 value이다.

BOOST_FOREACH(const ptree::value_type& f, formats){
	string at = f.first + ATTR_SET;
	strTagName = _T("");
	strTagName = f.first.c_str();
	AfxMessageBox(strTagName);

	const ptree& attributes = f.second.get_child("<xmlattr>", empty_ptree());

	BOOST_FOREACH(const ptree::value_type &v, attributes){
		strAttr = _T("First : ");
		strAttr += v.first.data();
		strAttrValue = v.second.data().c_str();
		AfxMessageBox(strAttr);
		AfxMessageBox(_T("Second : ") + strAttrValue);
	}
}



전체코드

ptree ptr;
CString myXMLFileName = _T("");
CString myXMLFilePath = _T("");
CString strAttr = _T("");
CString strAttrValue = _T("");
CString strTagName = _T("");

myXMLFileName = _T("PropertySettings.config");
myXMLFilePath = _T("D:\\Project\\Alpius\\branches\\alpius_xml_1.0.1\\Alpius\\Settings\\Configuration\\") + myXMLFileName;
read_xml( ConvertUnicode2String( myXMLFilePath ), ptr );

const ptree& formats = ptr.get_child("alpius900.stage-treatment.step-compare.dlg-main.group-operation", empty_ptree());

BOOST_FOREACH(const ptree::value_type& f, formats){
	string at = f.first + ATTR_SET;
	strTagName = _T("");
	strTagName = f.first.c_str();
	AfxMessageBox(strTagName);

	const ptree& attributes = f.second.get_child("", empty_ptree());

	BOOST_FOREACH(const ptree::value_type &v, attributes){
		strAttr = _T("First : ");
		strAttr += v.first.data();
		strAttrValue = v.second.data().c_str();
		AfxMessageBox(strAttr);
		AfxMessageBox(_T("Second : ") + strAttrValue);
	}
}


Comments