以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML基础 』  (http://bbs.xml.org.cn/list.asp?boardid=1)
----  这两天学习xml成果,希望能给帮助你解决问题  vc操作xml  (http://bbs.xml.org.cn/dispbbs.asp?boardid=1&rootid=&id=6692)


--  作者:wdllove
--  发布时间:4/13/2004 4:31:00 PM

--  这两天学习xml成果,希望能给帮助你解决问题  vc操作xml

void QueryXml()
{
   IXMLDOMDocumentPtr pXMLDom;
   HRESULT hr;

   CoInitialize(NULL);

   hr = pXMLDom.CreateInstance(__uuidof(DOMDocument40));
   if (FAILED(hr))
   {
      printf("Failed to instantiate DOMDocument40 class\n");
      return ;
   }
   //load
   pXMLDom->async = VARIANT_FALSE;
   if ( pXMLDom->load("c:\\he.xml") != VARIANT_TRUE)
   {
      printf("Failed load xml data from file.\n%s\n",
         (LPCSTR)pXMLDom->parseError->Getreason());
      return ;
   }

   IXMLDOMElementPtr  pNode = (IXMLDOMElementPtr)pXMLDom->selectSingleNode("//china/*");
   if (pNode == NULL)
   {
      printf("Invalid node fetched.\n%s\n",
         (LPCSTR)pXMLDom->parseError->Getreason());
   }
   else
   {
      /*printf("Result from selectSingleNode:\nNode, <%s>:\n\t%s\n\n",
         (LPCSTR)pNode->nodeName, (LPCSTR)pNode->xml);
   */
   printf("解释XML文件\n");
   }
   
   IXMLDOMNodeListPtr pnl = pXMLDom->selectNodes("//china/*");
   printf("Results from selectNodes:\n");
   for (int i=0; i<pnl->length; i++)
   {
     pNode = pnl->item[i];
  
  char cityName[400];memset(cityName,0,400);
  VARIANT varVal;    
  pNode->get_nodeTypedValue(&varVal); 
  strcpy(cityName,(char*)(_bstr_t)varVal);
  printf(cityName);
  printf(":详细情况\n");

  IXMLDOMNamedNodeMapPtr pAttrs = NULL;     
  IXMLDOMNodePtr pAttrItem;    
  pNode->get_attributes(&pAttrs);    
  long nCount ;    
  pAttrs->get_length(&nCount);    
  for(int i = 0 ; i < nCount ; i++)     
    {                      
   pAttrs->get_item(i,&pAttrItem); 

   char tmpName[400];memset(tmpName,0,400);
   strcpy(tmpName,(char*)(_bstr_t)pAttrItem->nodeName);
     printf(tmpName);
   printf(":");

   char tmpValue[400];
   memset(tmpValue,0,400);
   strcpy(tmpValue,(char*)(_bstr_t)pAttrItem->nodeTypedValue );
     printf(tmpValue);
   printf("\n");
  }  

  /*
      printf("Node (%d), <%s>:\n\t%s\n",
         i, (LPCSTR)pNode->nodeName, (LPCSTR)pnl->item[i]->xml);
   */
   }

   pXMLDom.Release();
   pNode.Release();
   pnl.Release();
   CoUninitialize();

}

void NewXml()
{
 CoInitialize(NULL);
 IXMLDOMDocumentPtr pDoc;
 IXMLDOMElementPtr xmlRoot;
 //创建DOMDOCUMENT对象
 HRESULT hr=pDoc.CreateInstance(_uuidof(DOMDocument40));
 if(!SUCCEEDED(hr))  
 {                  
  printf("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
  return ;  
 }
 pDoc->raw_createElement((_bstr_t)(char*)"country", &xmlRoot);
 pDoc->raw_appendChild(xmlRoot, NULL);
 
 IXMLDOMElementPtr  childChina;
 
 pDoc->raw_createElement((_bstr_t)(char*)"china", &childChina);

 IXMLDOMElementPtr  childNode ;

 pDoc->raw_createElement((_bstr_t)(char*)"City", &childNode);
 childNode->Puttext("沈阳");//节点值 
 childNode->setAttribute("population","8,000,000");//属性名,属性值 
 childNode->setAttribute("area","10000"); 
 childChina->appendChild(childNode);
  
 pDoc->raw_createElement((_bstr_t)(char*)"City", &childNode);
 childNode->Puttext("长春");//节点值 
 childNode->setAttribute("population","8,000,000");//属性名,属性值 
 childNode->setAttribute("area","10000"); 
 childChina->appendChild(childNode);
 xmlRoot->appendChild(childChina);

 pDoc->save("c:\\he.xml");
 
 pDoc.Release();
 xmlRoot.Release();
 childChina.Release();
 childNode.Release();

 CoUninitialize();
}

还在完善中,自己也很东西不会,也希望得到你们的帮助


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
42.969ms