zoukankan      html  css  js  c++  java
  • pugixml简单实用

    实现快递查询,调用快递100的API,未完成.

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <locale>
    #include <codecvt>
    #include <afxwin.h>
    #include <afxinet.h>
    #include <windows.h>
    #include "pugixml.hpp"
    #pragma comment(lib, "pugixmlsd.lib")
    
    void getXml(LPCTSTR url)
    { 
    	CFile file((TEXT("temp1.xml")), CFile::modeCreate|CFile::modeWrite);
    	CString content;
    	CString data;
    	DWORD dwStatusCode;
    	CInternetSession session(TEXT("HttpClient"));
    
    	CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
    	pfile -> QueryInfoStatusCode(dwStatusCode);
    	if(dwStatusCode == HTTP_STATUS_OK)
    	{
    		while (pfile -> ReadString(data))
    		{
    			content += data;
    		}
    	}
    	pfile -> Close();
    	delete pfile;
    	session.Close();
    	file.Write(content, content.GetLength());
    	file.Close();
    }
    
    void convertXml()
    {
    	auto LocUtf8 = std::locale(std::locale(""), new std::codecvt_utf8<wchar_t>);
    	std::wifstream wfin("temp1.xml");
    	std::wstring wstr, content;
    	wfin.imbue(LocUtf8);
    	while(getline(wfin, wstr))
    	{
    		content += wstr;
    	}
    	wfin.close();
    	system("del temp1.xml");
    	//std::wcout.imbue(std::locale(""));
    	//std::wcout << content << std::endl;
    
    	std::locale::global(std::locale("Chinese-simplified"));
    	std::wofstream wfout("temp.xml");
    	wfout << content;
    	wfout.close();
    }
    
    void readXml()
    {
    	pugi::xml_document doc;
    	doc.load_file("temp.xml");
    	//doc.save(std::cout);
    	pugi::xml_node data = doc.child("xml");
    	for(pugi::xml_node tool = data.first_child(); tool; tool = tool.next_sibling())  
    	{    
    
    		for(pugi::xml_node element = tool.first_child(); element; element = element.next_sibling())  
    		{  
    			std::cout << element.name() << " : " << element.text().get() << std::endl;  
    		}
    		std::cout << std::endl;
    	}
    }
    
    int main()
    {
    	LPCTSTR str = TEXT("http://api.kuaidi100.com/api?id=a78e61062aabe452&com=yuantong&nu=9100493541&show=1");
    	getXml(str);
    	convertXml();
    	readXml();
    	system("del temp.xml");
    	//system("pause");
    	return 0;
    }
    


    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    五大Java开源论坛
    mysql limit,offset 区别
    查询某个字段存在于哪几个表
    C++分享笔记:5X5单词字谜游戏设计
    Linux分享笔记:系统状态检测命令小结
    Linux分享笔记:查看帮助命令 & 常用系统工作命令
    数据结构(C语言)分享笔记:数据结构的逻辑层次、存储层次
    Linux分享笔记:shell终端的介绍
    Java开发学生管理系统
    JAVA使用JDBC连接,修改MySQL数据库(比较乱)
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834852.html
Copyright © 2011-2022 走看看