简单处理IP XML数据

前端之家收集整理的这篇文章主要介绍了简单处理IP XML数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
///* 编译环境: visual c++ */ 
//#include <stdio.h> 
//#include <winsock2.h> 
//#pragma comment(lib,"ws2_32.lib") 
//
//int doit(int,char **) 
//{ 
//	char host_name[255]; 
//	//获取本地主机名称 
//	if (gethostname(host_name,sizeof(host_name)) == SOCKET_ERROR) { 
//		printf("Error %d when getting local host name.\n",WSAGetLastError()); 
//		return 1; 
//	} 
//	printf("Host name is: %s\n",host_name); 
//
//	//从主机名数据库中得到对应的“主机” 
//	struct hostent *phe = gethostbyname(host_name); 
//	if (phe == 0) { 
//		printf("Yow! Bad host lookup."); 
//		return 1; 
//	} 
//
//	//循环得出本地机器所有IP地址 
//	for (int i = 0; phe->h_addr_list[i] != 0; ++i) { 
//		struct in_addr addr; 
//		memcpy(&addr,phe->h_addr_list[i],sizeof(struct in_addr)); 
//		printf("Address %d : %s\n",i,inet_ntoa(addr)); 
//	} 
//
//	return 0; 
//} 
//
//int main(int argc,char *argv[]) 
//{ 
//	WSAData wsaData; 
//	if (WSAStartup(MAKEWORD(1,1),&wsaData) != 0) { 
//		return 255; 
//	} 
//
//	int retval = doit(argc,argv); 
//
//	WSACleanup(); 
//	return retval; 
//}



#include <iostream>
#include <string>
#include <cstdlib>
#include <afxinet.h>
#include "tinyxml.h"
#pragma comment(lib,"tinyxml.lib")
#pragma comment(lib,"tinyxmlSTL.lib")
using namespace std;

std::string getIp()
{
	CString url = "http://ip.dnsexit.com/";
	CString content;
	CString data;
	DWORD dwStatusCode;
	CInternetSession session("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();
	return std::string(content);
}

void getXml(string url)
{
	CFile file(("temp.xml"),CFile::modeCreate|CFile::modeWrite);
	CString content;
	CString data;
	DWORD dwStatusCode;
	CInternetSession session("HttpClient");

	CHttpFile* pfile = (CHttpFile *)session.OpenURL(url.c_str());
	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 readXml()
{
	TiXmlDocument doc("temp.xml");
	doc.LoadFile();
	TiXmlElement* root = doc.RootElement();
	TiXmlNode* item = root->FirstChild("product");
	TiXmlNode* child = item->FirstChild();
	string ip = child->ToElement()->GetText();
	if (!ip.empty()) 
	{
		cout << "ip = " << ip << endl;
	} 

	child = item->IterateChildren(child);
	string addr = child->ToElement()->GetText();
	if (!addr.empty())
	{
		cout << "address = " << addr << endl;
	} 
}

int main()
{
	string str = "http://www.yodao.com/smartresult-xml/search.s?type=ip&q=";
	str+= getIp();
	getXml(str);
	readXml();
	system("del temp.xml");
	system("pause");
	return 0;
}

tinyxml的编译参数要严格与所写程序匹配,比如runtime library Use of MFC,Character Set,否则就会爆出这样不知所云的错误

1>libcmt.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(wsetloca.obj) : error LNK2005: __configthreadlocale already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(crt0dat.obj) : error LNK2005: __amsg_exit already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(crt0dat.obj) : error LNK2005: __cexit already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(crt0dat.obj) : error LNK2005: __exit already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(crt0dat.obj) : error LNK2005: __initterm_e already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(crt0dat.obj) : error LNK2005: _exit already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(mlock.obj) : error LNK2005: __lock already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(mlock.obj) : error LNK2005: __unlock already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(winapisupp.obj) : error LNK2005: ___crtSetUnhandledExceptionFilter already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(winapisupp.obj) : error LNK2005: ___crtTerminateProcess already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(winapisupp.obj) : error LNK2005: ___crtUnhandledException already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(winxfltr.obj) : error LNK2005: __XcptFilter already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(hooks.obj) : error LNK2005: "void __cdecl terminate(void)" (?terminate@@YAXXZ) already defined in msvcrtd.lib(MSVCR110D.dll) 1>libcmt.lib(crt0init.obj) : error LNK2005: ___xi_a already defined in msvcrtd.lib(cinitexe.obj) 1>libcmt.lib(crt0init.obj) : error LNK2005: ___xi_z already defined in msvcrtd.lib(cinitexe.obj) 1>libcmt.lib(crt0init.obj) : error LNK2005: ___xc_a already defined in msvcrtd.lib(cinitexe.obj) 1>libcmt.lib(crt0init.obj) : error LNK2005: ___xc_z already defined in msvcrtd.lib(cinitexe.obj) 1>libcmt.lib(errmode.obj) : error LNK2005: ___set_app_type already defined in msvcrtd.lib(MSVCR110D.dll) 1>LINK : warning LNK4098: defaultlib 'msvcrtd.lib' conflicts with use of other libs; use /NODEFAULTLIB:library 1>msvcrtd.lib(cinitexe.obj) : warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library 1>D:\VS2010\OnlyForFun\OnlyForFun\Debug\ReadXml.exe : fatal error LNK1169: one or more multiply defined symbols found ========== Build: 0 succeeded,1 Failed,0 up-to-date,0 skipped ==========

猜你在找的XML相关文章