十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
用VC吧,下面有一个例子,你参照下:
创新互联公司是一家集网站建设,青山企业网站建设,青山品牌网站建设,网站定制,青山网站建设报价,网络营销,网络优化,青山网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
void CreateXml()
{
CoInitialize(NULL);
// 创建文档
MSXML2::IXMLDOMDocument2Ptr pXMLDoc = NULL;
//创建DOMDocument对象
HRESULT hr = pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
if (!SUCCEEDED(hr))
{
return;
}
// Create the Processing Instruction
MSXML2::IXMLDOMProcessingInstructionPtr pXMLProc = NULL;
pXMLProc = pXMLDoc-createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
_variant_t vNullVal;
vNullVal.vt = VT_NULL;
pXMLDoc-insertBefore(pXMLProc, vNullVal);
// 创建根结点
_variant_t varNodeType((short)MSXML2::NODE_ELEMENT);
MSXML2::IXMLDOMNodePtr pXMLNodeRoot= NULL;
pXMLNodeRoot = pXMLDoc-createNode(varNodeType, _T("Cases"), _T(""));
// 添加根结点
pXMLDoc-appendChild(pXMLNodeRoot);
// 创建并添加下级结点
MSXML2::IXMLDOMNodePtr pXMLNodeNode= NULL;
pXMLNodeNode = pXMLNodeRoot-appendChild(pXMLDoc-createElement(_T("Case")));
// 创建下级元素结点
MSXML2::IXMLDOMElementPtr pXMLEle = NULL;
pXMLEle = pXMLDoc-createElement(_T("CopyFile"));
// 创建并设置下级结点属性
MSXML2::IXMLDOMAttributePtr pXMLAttr = NULL;
pXMLAttr = pXMLDoc-createAttribute(_T("src"));
pXMLAttr-nodeTypedValue = "C:\\test.txt";
pXMLEle-attributes-setNamedItem(pXMLAttr);
pXMLAttr = pXMLDoc-createAttribute(_T("dest"));
pXMLAttr-nodeTypedValue = "D:\\Test.txt";
pXMLEle-attributes-setNamedItem(pXMLAttr);
// 添加元素结点
pXMLNodeNode-appendChild( pXMLEle);
MSXML2::IXMLDOMElementPtr pXMLEle1 = NULL;
pXMLEle1 = pXMLDoc-createElement(_T("DelFile"));
pXMLEle1-appendChild(pXMLDoc-createTextNode("C:\\test.txt"));
// 添加元素结点
pXMLNodeNode-appendChild( pXMLEle1);
// 保存文档
pXMLDoc-save(_T("d:\\Test.xml"));
}
效果如下:
?xml version="1.0" encoding="UTF-8" ?
Cases
Case
CopyFile src="C:\test.txt" dest="D:\Test.txt" /
DelFileC:\test.txt/DelFile
/Case
/Cases
为了能够让MFC认识MSXML2,我们需要引入相应的dll,代码如下;
#import "msxml4.dll"
/* 前段时间恰好做过类似的东西,代码可以给你参考下。
* Xml配置见最后
*/
typedef struct SrcFileFmt
{
int ColID;
char ColCode[64]; /* 字段英文名称 */
char ColName[128]; /* 字段中文名称*/
char ColType[20]; /* 字段类型(包含长度) */
char ColComment[128]; /* 字段描述 */
}SrcFileFmt;
int main(int argc, char **argv)
{
SrcFileFmt SrcFileFmt[128];
int iNum = -1;
if ( 2 argc )
{
printf("Usage: %s SrcXmlFile\n", argv[0]);
return -1;
}
iNum = parseSourceCfg(SrcCfgFile, SrcFileFmt);
if (iNum == -1)
{
return -1;
}
return 0;
}
/* 调用此函数后,xml文件的内容会被存储到结构体数组SrcFileFmt srcfilefmt[]中
* 此函数依赖于libxml2-2.9.2.tar.xz
*/
int parseSourceCfg(char *FileName, SrcFileFmt srcfilefmt[])
{ /* 解析源文件xml,FileName 为源xml文件名 */
xmlDocPtr doc;
xmlNodePtr cur, root;
char sFileName[64] = {'\0'};
int cnt = 0;
if (FileName == NULL)
{
return -1;
}
sprintf(sFileName, "%s.xml", FileName);
doc = xmlParseFile(sFileName);
if (doc == NULL)
{
return -1;
}
root = xmlDocGetRootElement(doc);
if (root == NULL) {
xmlFreeDoc(doc);
return(-1);
}
if (xmlStrcmp(root-name, (const xmlChar *) "SrcRoot"))
{
xmlFreeDoc(doc);
return -1;
}
cur = root-xmlChildrenNode;
while (cur != NULL)
{
if ((!xmlStrcmp(cur-name, (const xmlChar *)"Column")))
{
xmlChar *key;
xmlNodePtr cur_sub = cur;
cur_sub = cur_sub-xmlChildrenNode;
while (cur_sub != NULL)
{
if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)"ColID"))) {
key = xmlNodeListGetString(doc, cur_sub-xmlChildrenNode, 1);
killblank((char*)key);
srcfilefmt[cnt].ColID = atoi((char*)key);
xmlFree(key);
}
if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)"ColCode"))) {
key = xmlNodeListGetString(doc, cur_sub-xmlChildrenNode, 1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColCode, (char*)key);
xmlFree(key);
}
else if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)"ColName"))) {
key = xmlNodeListGetString(doc, cur_sub-xmlChildrenNode, 1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColName, (char*)key);
xmlFree(key);
}
else if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)"ColType"))) {
key = xmlNodeListGetString(doc, cur_sub-xmlChildrenNode, 1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColType, (char*)key);
xmlFree(key);
}
else if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)"ColComment"))) {
key = xmlNodeListGetString(doc, cur_sub-xmlChildrenNode, 1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColComment, (char*)key);
xmlFree(key);
}
cur_sub = cur_sub-next;
}
cnt++;
}
cur = cur-next;
}
xmlFreeDoc(doc);
return cnt;
}
SrcRoot
Column
ColID1/ColID
ColCodekmh/ColCode
ColName字段1/ColName
ColTypeVARCHAR(11)/ColType
/Column
Column
ColID2/ColID
ColCodedfkmh/ColCode
ColName字段2/ColName
ColTypeVARCHAR(11)/ColType
/Column
Column
ColID3/ColID
ColCodehbh/ColCode
ColName字段3/ColName
ColTypeINTEGER(10)/ColType
/Column
/SrcRoot
/***************
?xml version="1.0" encoding="utf-8"?
Cases
case
No001/No
CopyFile src="C:\test.txt" dest="D:\test.txt"/CopyFile
/case
case
No002/No
DelFileC:\test.txt/DelFile
/case
/Cases
*******************/
// 我们用MFC来读取上述xml,代码如下:
void ReadXml(CString strXmlPath)
{
MSXML2::IXMLDOMDocumentPtr pDoc;
::CoInitialize(NULL);
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if (!SUCCEEDED(hr))
{
MessageBox(_T("创建DOMDocument对象失败。\n请检查运行环境"), _T("错误"), MB_ICONERROR);
return;
}
// 读取xml
pDoc-put_async(VARIANT_FALSE);
VARIANT_BOOL bhr = pDoc-load((_variant_t)strXmlPath);
if (bhr != VARIANT_TRUE) {
MessageBox(_T("无法正确读取xml文件"), _T("错误"), MB_ICONERROR);
return;
}
// 根节点取得
MSXML2::IXMLDOMElementPtr root = pDoc-documentElement;
// 取得根节点的名字
_variant_t strRootName = root-nodeName;
_bstr_t wstrRootName(strRootName.bstrVal);
MSXML2::IXMLDOMNodeListPtr nodeList = root-GetchildNodes();//cases
// 解析cases的子节点
ReadCases(nodeList);
}
void ReadCases(MSXML2::IXMLDOMNodeListPtr nodeList)
{
int ilength = nodeList-Getlength();
for (int nodeCount = 0; nodeCount ilength; nodeCount++) {
MSXML2::IXMLDOMNodePtr nodePtr = nodeList-nextNode();
_variant_t strNodeName = nodePtr-GetnodeName();
_variant_t strNodeValue = nodePtr-GetnodeValue();
// 读取case节点下的子节点
ReadCase(nodePtr-GetchildNodes());
}
}
void ReadCase(MSXML2::IXMLDOMNodeListPtr nodeList)
{
CString strLogInfo;
strLogInfo.Empty();
CString strNo; // case编号
CString strSrcFile; // 源文件
CString strDestFile; // 目标文件
for (int nodeCount = 0; nodeCount nodeList-Getlength(); nodeCount++)
{
MSXML2::IXMLDOMNodePtr nodePtr = nodeList-nextNode();
_variant_t strCaseNodeName = nodePtr-GetnodeName();
_variant_t strCaseNodeValue = nodePtr-Gettext();
BSTR bStrTemp = strCaseNodeName.bstrVal;
CString strTemp = CString(bStrTemp);
SysFreeString(bStrTemp);
CString strNodeName = strTemp;
// 节点的值,如何取得?
if (0 == strNodeName.CompareNoCase(_T("NO")))
{
strNo = (BSTR)strCaseNodeValue.pbstrVal;
// 取得的值可以打印出来
printf(strNo);
}
// 节点有属性值,该怎么处理?
else if (0 == strNodeName.CompareNoCase(_T("CopyFile")))
{
strSrcFile.Empty();
strDestFile.Empty();
// 取得节点的属性值
MSXML2::IXMLDOMNamedNodeMapPtr pDOMAttrList= nodePtr-Getattributes();
for (int j = 0; j pDOMAttrList-Getlength(); j++)
{
MSXML2::IXMLDOMNodePtr pDOMAttr= pDOMAttrList-Getitem(j);
// 取得源文件路径
if (CompareNoCase((char*)pDOMAttr-GetnodeName(), _T("src")))
{
strSrcFile = pDOMAttr-GetnodeTypedValue();
// 取得目标文件路径
} else if (CompareNoCase((char*)pDOMAttr-GetnodeName(), _T("dest")))
{
strDestFile =pDOMAttr-GetnodeTypedValue();
}
CopyFile(strSrcFile, strDestFile, FALSE);
}
else if (0 == strNodeName.CompareNoCase(_T("DelFile")))
{
strDestFile.Empty();
strDestFile = CString((BSTR)strCaseNodeValue.pbstrVal);
DeleteFile(strDestFile);
}
}
}
// 为了能够让MFC认识MSXML2,我们需要引入相应的dll,代码如下;
#import "msxml4.dll"
把所有的数据当做一个字符串
收到数据后先strstr(buffer,"?xml version=\"1.0\" encoding=\"UTF-8\"?");
如果返回的是NULL则表示没有这段 退出
buffer是你收到的数据起始地址