VC处理xml的方法

程序代码

 

  1. #import "msxml6.dll" named_guids raw_interfaces_only
  2. #include <msxml.h>
  3. using namespace MSXML2;
  4. using namespace std;
  5. int testXml(wstring strfilname)
  6. {
  7.     cout << "BEGIN" << endl;
  8.     HRESULT hr;
  9.     long cnt;
  10.     try
  11.     {
  12.         MSXML2::IXMLDOMDocumentPtr pDoc;
  13.         MSXML2::IXMLDOMNodeListPtr pNodeList;
  14.         MSXML2::IXMLDOMNodePtr pNode, pRoot;
  15.         MSXML2::IXMLDOMNamedNodeMapPtr pAttr;
  16.         hr = ::CoInitialize(NULL);
  17.         if (FAILED(hr))
  18.         {
  19.             throw L"failed to com init";
  20.         }
  21.         hr = CoCreateInstance(CLSID_DOMDocument60, NULL, CLSCTX_INPROC_SERVER, 
  22.             MSXML2::IID_IXMLDOMDocument, (void**)&pDoc);
  23.         if(FAILED(hr))
  24.             throw "failed to create DOM DOC";
  25.         VARIANT_BOOL bIsSuccessful;
  26.         BSTR bstr1 = SysAllocString(L"d://b.xml");
  27.         VARIANT v1;
  28.         v1.vt = VT_BSTR;
  29.         v1.bstrVal = bstr1;
  30.         ///// 载入 xml文件
  31.         pDoc->load(v1, &bIsSuccessful);
  32.         SysFreeString(bstr1);
  33.         ///// 获取根节点 ROOT_BASENAME 
  34.         //pRoot = pDoc->firstChild;
  35.         pDoc->get_firstChild(&pRoot);
  36.         ///// 输出跟节点名字
  37.         BSTR bstr;
  38.         pRoot->get_nodeName(&bstr);
  39.         cout << (char*)((_bstr_t)bstr) <<endl;
  40.         ///// 获取根节点属性 Attr_baseName_1 = "Attr_text_1" Attr_baseName_2 = "Attr_text_2"
  41.         pRoot->get_attributes(&pAttr);
  42.         ///// 取得属性个数
  43.         long length;
  44.         pAttr->get_length(&length);
  45.         cout << length << endl;
  46.         ///// 输出根节点属性
  47.         for(cnt = 0 ; pAttr->nextNode(&pNode), pNode ; ++cnt)
  48.         {
  49.             BSTR baseName;
  50.             pNode->get_baseName(&baseName);
  51.             BSTR text;
  52.             pNode->get_text(&text);
  53.             cout << (char*)((_bstr_t)baseName) << "  " << (char*)((_bstr_t)text) << endl;
  54.         }
  55.         cout << "Attr count " << cnt <<endl;
  56.         ///// 输出根节点所有儿子
  57.         pRoot->get_childNodes(&pNodeList);
  58.         pNodeList->get_length(&cnt);
  59.         cout << "LENGTH  " << cnt << endl;
  60.         for(cnt = 0; pNodeList->nextNode(&pNode), pNode; ++cnt)
  61.         {
  62.             BSTR text;
  63.             pNode->get_text(&text);
  64.             cout << (char*)((_bstr_t)text) << endl;
  65.         }
  66.         cout << "Node count " << cnt <<endl;
  67.     } 
  68.     catch (string str)
  69.     {
  70.         cout << "Exception : " << str << endl;
  71.     }
  72.     catch (...)
  73.     {
  74.         cout << "Exception Unkonwn/n";
  75.     }
  76.     ::CoUninitialize();
  77.     cout << "END" <<endl;
  78.     return 0;
  79. }

xml文件:

  1. <ROOT_BASENAME Attr_baseName_1 = "Attr_text_1" Attr_baseName_2 = "Attr_text_2">
  2. <FIRST_CHILD>
  3. text_1
  4. </FIRST_CHILD>
  5. <CHILD>
  6. text_2
  7. </CHILD>
  8. <LAST_CHILD>
  9. text_3
  10. </LAST_CHILD>
  11. </ROOT_BASENAME>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值