drcarter의 DevLog

1. MSXML 4.0 SDK 설치.. (.msi)

2. stdafx.h 하단부에 밑의 두줄 추가..

..

..

#import <msxml4.dll>
using namespace MSXML2;
//{{AFX_INSERT_LOCATION}}

..

3. 원하는 곳에서..

 

-- local 의 xml


void XXXXX::XmlTest()
{

 IXMLDOMDocument2Ptr pDoc = NULL;    // XML Document 선언
 IXMLDOMNamedNodeMapPtr pNodeMap = NULL;   // for attribute
 IXMLDOMNodeListPtr pNodeList = NULL;   // 찾은 노드들
 IXMLDOMNodePtr pNode = NULL;     // 찾은 노드들 하나씩(뒤에 설명)
 
 CString nodeName = "";
 CString attrName = "";

 CString attrVal = "";

 long lCount=0, lTotal = 0;      // 노드들에 대한 갯수 관련
 long lTotalAttr=0, lCountAttr = 0;    // attribute에 대한 갯구 관련

 pDoc.CreateInstance(__uuidof(DOMDocument));  // 인스턴스 생성
 pDoc->load((_variant_t)"C:\\test.xml");    
 pNodeList = pDoc->selectNodes(L"//tNode");  // tNode 노드들 모두 찾기~
 
 if(pNodeList)
 {
  lTotal = pNodeList->Getlength();   // 검색된 노드의 총 갯수

  while(lCount < lTotal)      // 루프를 돌리자~
  {
   pNode = pNodeList->Getitem(lCount++); // lCount 번째 노드를 선택
   pNodeMap = pNode->Getattributes();  // 해당 노드의 attribute 목록 선택
   lCountAttr = 0;
   lTotalAttr = pNodeMap->Getlength();  // Total of Attribute


   while(lCountAttr < lTotalAttr)
   {

    nodeName = (LPCTSTR)pNode->GetbaseName(); // 현재 노드 이름
    // 해당 노드의 attribute name
    attrName = (LPCTSTR)pNodeMap->Getitem(lCountAttr)->GetnodeName();
    // 해당 attribute value - %s(X), %S(O)) 대소문자 구분해서 쓸 것!!
    attrVal.Format("%S", (LPCTSTR)pNodeMap->Getitem(lCountAttr)->GetnodeValue().bstrVal);
   

    // 원하는대로 attrVal 을 가져다 쓰자!!    
   

    lCountAttr++;
   }
   
   
  } // lCount < l Total

  // return S_OK;
 }

 }


-- web xml

 

stdafx.h 하단부에 밑의 두줄 추가..


#import "msxml3.dll" named_guids raw_interfaces_only
using namespace MSXML2;

 

위의 방식으로 웹에서의 xml 을 접근할려고 했더니만 오류가 나서 한참을 뒤지다가

찾아낸 자료를 많이 뜯어고쳐서 필요한 것만 가져오는 함수로 재수정..

 

void XXXXXX::GetHttpXML()
{
 IXMLDOMDocumentPtr pDoc = NULL;
 IXMLDOMElementPtr pElemRoot = NULL;
 IXMLDOMNamedNodeMapPtr pNodeMap = NULL;
 IXMLDOMNodePtr pNode1 = NULL;
    IXMLDOMNodePtr pNode2 = NULL;
 IXMLDOMNodeListPtr pNodeList = NULL;
    VARIANT_BOOL f;
 HRESULT hr;
 _variant_t tVal;
 IXMLDOMNode* node = NULL;

 long lCount=0, lTotal = 0;      // 노드들에 대한 갯수 관련
 long lTotalAttr=0, lCountAttr = 0;    // attribute에 대한 갯구 관련

 CString real_attrVal = "";
 u_short *nodeName = NULL;
 BSTR *attrName = NULL;
 _variant_t varText;


 hr = pDoc.CreateInstance(__uuidof(DOMDocument));

    pDoc->put_async(VARIANT_FALSE);
    pDoc->put_preserveWhiteSpace(VARIANT_TRUE);
   
    hr = pDoc->load(_variant_t(L"http://192.168.2.254/test.xml"), &f);
   
    pDoc->get_documentElement(&pElemRoot);
    pElemRoot->getElementsByTagName(CComBSTR(L"test"), &pNodeList);
   
 if(pNodeList)
 {  
  pNodeList->get_length(&lTotal);    // 검색된 노드의 총 갯수

  while(lCount < lTotal)      // 루프를 돌리자~
  {
   CString varTextEx = "";

   pNodeList->get_item(lCount++, &pNode1); // lCount 번째 노드를 선택
   pNode1->get_attributes(&pNodeMap);  // 해당 노드의 attribute 목록 선택
   lCountAttr = 0;
   
   // attribute 0, 1, 2 중 1번째 값을 가져온다.
   pNodeMap->get_item(1, &node);
   node->get_nodeValue(&varText);
   
   varTextEx.Format("%S", varText.bstrVal);        
   
   // attribute 0, 1, 2 중 2번째 값을 가져온다.
   pNodeMap->get_item(2, &node);
   node->get_nodeValue(&varText);
   
   CString varTextEx2 = "";
   varTextEx2.Format("%S", varText.bstrVal);

 
  }
 
 }
}

[출처] [MFC, C++] XML|작성자 리우