本文目录一览:
c语言如何调用xml的接口函数
/***************
?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"
如何用C语言实现对xml文件的加密
把xml就当做一个普通的二进制文件
然后随便找一个加密算法
按照字节读入xml文件
然后按照加密算法进行加密转换,输出到目标文件就可以了
简单的
可以用异或方式。
怎么用C语言写一个简单的XML文件
用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"