mfc 增删改 xml 节点
2009-09-23 10:55
225 查看
1、对xml的特定节点的属性的读取
MSXML::IXMLDOMNodePtr psNode;
MSXML::IXMLDOMNamedNodeMapPtr methodpAttrs = NULL;
MSXML::IXMLDOMNodePtr pmethodAttrItem;
try
{
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance (MSXML::CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,MSXML::IID_IXMLDOMDocument,(void**)&pDoc)))
{
CComVariant vFile(path);//path为要读取的xml的路径
pDoc->load(vFile);
MSXML::IXMLDOMNodeListPtr pNodeList = NULL;
pNodeList = pDoc->selectNodes((_bstr_t)"xxxxxxxxxx"); //xxxxxxxxx处为要查找的节点
int num=pNodeList->Getlength();
for(int i=0;i<num;i++)
{
psNode=pNodeList->Getitem(i);
BSTR bstr=psNode->Gettext(); //取得psNode的节点值
。。。//进行相应的操作
psNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
。。。//得到属性值后进行相应的操作
}
}
}
}catch(...)
{ }
2、节点的删除
//首先找到相关节点
MSXML::IXMLDOMDocument* pDoc=NULL;
MSXML::IXMLDOMNodePtr pNode;
MSXML::IXMLDOMNodePtr psNode;
MSXML::IXMLDOMNodePtr proNode;
MSXML::IXMLDOMNodePtr pattNode;
MSXML::IXMLDOMNodePtr pmethNode;
MSXML::IXMLDOMNodePtr pparmNode;
MSXML::IXMLDOMElementPtr pchildNode=NULL;
MSXML::IXMLDOMNodePtr psonNode;
MSXML::IXMLDOMElementPtr pparamNode;
MSXML::IXMLDOMElementPtr psoneNode=NULL;
MSXML::IXMLDOMNode *pIDOMNode=NULL;
MSXML::IXMLDOMNamedNodeMapPtr methodpAttrs = NULL;
MSXML::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
MSXML::IXMLDOMNodePtr pmethodAttrItem;
MSXML::IXMLDOMNodePtr pAttrItem;
MSXML::IXMLDOMNodeListPtr pList = NULL;
MSXML::IXMLDOMNodeListPtr pparaList = NULL;
MSXML::IXMLDOMNodeListPtr pattackList = NULL;
MSXML::IXMLDOMNodeListPtr pmethodList = NULL;
MSXML::IXMLDOMNodeListPtr pparList = NULL;
MSXML::IXMLDOMNamedNodeMapPtr otherpAttrs = NULL;
long nCount;
int k,m,l;
int i;
CString fin;
CString strAttrName ;
CString strAttrValue ;
try
{
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(MSXML::CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,MSXML::IID_IXMLDOMDocument,(void**)&pDoc)))
{
CComVariant vFile(path);
pDoc->load(vFile);
MSXML::IXMLDOMNodeListPtr pNodeList = NULL;
pNodeList = pDoc->selectNodes((_bstr_t)"NESSUS/nessus");
int num=pNodeList->Getlength();
for(i=0;i<num;i++)
{
psNode=pNodeList->Getitem(i);
psNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
if(strgetName=="info"&&strgetValue==name) //get the right nessus
{
k=1;
break;
}
}//end for
if(k==1)
break;
}//end for
pparaList=pDoc->selectNodes((_bstr_t)"NESSUS/nessus/attacks");
proNode=pparaList->Getitem(i); //get attacks
proNode->get_childNodes(&pattackList);
int nAttack=pattackList->Getlength();
for(i=0;i<nAttack;i++)
{
psNode=pattackList->Getitem(i);
methodpAttrs=NULL;
psNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
if(strgetName=="info"&&strgetValue==method) //get the right attack
{
m=1;
break;
}
}//end for
if(m==1)
break;
}//end for
pattackList=NULL;
psNode->get_childNodes(&pattackList);
int nmethods=pattackList->Getlength();
for(i=0;i<nmethods;i++)
{
psonNode=pattackList->Getitem(i);
methodpAttrs=NULL;
psonNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
if(strgetName=="name"&&strgetValue==attack) //get the right attack
{
l=1;
break;
}
}//end for
if(l==1)
break;
}//end for
//delete
pattackList=NULL;
psonNode->get_childNodes(&pattackList);
num=pattackList->Getlength();
//printf("%d",num);
//methodpAttrs=NULL;
int index=0;
for(int i=0;i<num;i++) //delete
{
pattNode=pattackList->Getitem(index);
MSXML::IXMLDOMNamedNodeMapPtr methodpAttr = NULL;
pattNode->get_attributes(&methodpAttr);
methodpAttr->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttr->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
if(strgetName=="name"&&strgetValue=="php/kbholespath") //get the right attack
{
psonNode->removeChild(pattNode);
pDoc->save(vFile);
}
else
index++;
}//end for
}
}
}
}catch(...)
{
}
3、节点的建立
MSXML::IXMLDOMDocument* pDoc=NULL;
MSXML::IXMLDOMElementPtr pNode;
MSXML::IXMLDOMElementPtr psNode;
MSXML::IXMLDOMElementPtr proNode;
MSXML::IXMLDOMElementPtr pattNode;
MSXML::IXMLDOMElementPtr pmethNode;
MSXML::IXMLDOMElementPtr pparmNode;
MSXML::IXMLDOMElementPtr psonNode;
try
{
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(MSXML::CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,MSXML::IID_IXMLDOMDocument,(void**)&pDoc)))
{
CComVariant vFile(path);
pDoc->load(vFile);
MSXML::IXMLDOMElementPtr pxmlRoot;
pxmlRoot=pDoc->documentElement;
int length=arr.GetSize();
pDoc->raw_createElement((_bstr_t)"nessus",&pNode); //建立nessus节点
pNode->setAttribute((_bstr_t)"id",(_bstr_t)arr.GetAt(0));
pNode->setAttribute((_bstr_t)"info",(_bstr_t)arr.GetAt(1));
pNode->setAttribute((_bstr_t)"vulnerable",(_bstr_t)"true");
pNode->setAttribute((_bstr_t)"hasinformation",(_bstr_t)"true");
pxmlRoot->appendChild(pNode);
pDoc->raw_createElement((_bstr_t)"name",&psNode);//建立name节点
CString strname=arr.GetAt(1)+".nasl";
psNode->Puttext((_bstr_t)strname);
pNode->appendChild(psNode);
pDoc->raw_createElement((_bstr_t)"discription",&proNode); //建立discription节点
proNode->Puttext((_bstr_t)arr.GetAt(2));
pNode->appendChild(proNode);
pDoc->raw_createElement((_bstr_t)"required",&pattNode);//建立required节点
pNode->appendChild(pattNode);
if(length>3)
{
pDoc->raw_createElement((_bstr_t)"script",&pmethNode);
int j=length-3;
CString len;
len.Format("%d",j);
pmethNode->setAttribute((_bstr_t)"num",(_bstr_t)len);
pattNode->appendChild(pmethNode);
for(int i=3;i<length;i++)
{
pDoc->raw_createElement((_bstr_t)"value",&pparmNode);
pparmNode->Puttext((_bstr_t)arr.GetAt(i));
pmethNode->appendChild(pparmNode);
}
}
pDoc->raw_createElement((_bstr_t)"attacks",&psonNode);
pNode->appendChild(psonNode);
pDoc->save(vFile);
}
}
}catch(...)
{
}
MSXML::IXMLDOMNodePtr psNode;
MSXML::IXMLDOMNamedNodeMapPtr methodpAttrs = NULL;
MSXML::IXMLDOMNodePtr pmethodAttrItem;
try
{
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance (MSXML::CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,MSXML::IID_IXMLDOMDocument,(void**)&pDoc)))
{
CComVariant vFile(path);//path为要读取的xml的路径
pDoc->load(vFile);
MSXML::IXMLDOMNodeListPtr pNodeList = NULL;
pNodeList = pDoc->selectNodes((_bstr_t)"xxxxxxxxxx"); //xxxxxxxxx处为要查找的节点
int num=pNodeList->Getlength();
for(int i=0;i<num;i++)
{
psNode=pNodeList->Getitem(i);
BSTR bstr=psNode->Gettext(); //取得psNode的节点值
。。。//进行相应的操作
psNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
。。。//得到属性值后进行相应的操作
}
}
}
}catch(...)
{ }
2、节点的删除
//首先找到相关节点
MSXML::IXMLDOMDocument* pDoc=NULL;
MSXML::IXMLDOMNodePtr pNode;
MSXML::IXMLDOMNodePtr psNode;
MSXML::IXMLDOMNodePtr proNode;
MSXML::IXMLDOMNodePtr pattNode;
MSXML::IXMLDOMNodePtr pmethNode;
MSXML::IXMLDOMNodePtr pparmNode;
MSXML::IXMLDOMElementPtr pchildNode=NULL;
MSXML::IXMLDOMNodePtr psonNode;
MSXML::IXMLDOMElementPtr pparamNode;
MSXML::IXMLDOMElementPtr psoneNode=NULL;
MSXML::IXMLDOMNode *pIDOMNode=NULL;
MSXML::IXMLDOMNamedNodeMapPtr methodpAttrs = NULL;
MSXML::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
MSXML::IXMLDOMNodePtr pmethodAttrItem;
MSXML::IXMLDOMNodePtr pAttrItem;
MSXML::IXMLDOMNodeListPtr pList = NULL;
MSXML::IXMLDOMNodeListPtr pparaList = NULL;
MSXML::IXMLDOMNodeListPtr pattackList = NULL;
MSXML::IXMLDOMNodeListPtr pmethodList = NULL;
MSXML::IXMLDOMNodeListPtr pparList = NULL;
MSXML::IXMLDOMNamedNodeMapPtr otherpAttrs = NULL;
long nCount;
int k,m,l;
int i;
CString fin;
CString strAttrName ;
CString strAttrValue ;
try
{
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(MSXML::CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,MSXML::IID_IXMLDOMDocument,(void**)&pDoc)))
{
CComVariant vFile(path);
pDoc->load(vFile);
MSXML::IXMLDOMNodeListPtr pNodeList = NULL;
pNodeList = pDoc->selectNodes((_bstr_t)"NESSUS/nessus");
int num=pNodeList->Getlength();
for(i=0;i<num;i++)
{
psNode=pNodeList->Getitem(i);
psNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
if(strgetName=="info"&&strgetValue==name) //get the right nessus
{
k=1;
break;
}
}//end for
if(k==1)
break;
}//end for
pparaList=pDoc->selectNodes((_bstr_t)"NESSUS/nessus/attacks");
proNode=pparaList->Getitem(i); //get attacks
proNode->get_childNodes(&pattackList);
int nAttack=pattackList->Getlength();
for(i=0;i<nAttack;i++)
{
psNode=pattackList->Getitem(i);
methodpAttrs=NULL;
psNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
if(strgetName=="info"&&strgetValue==method) //get the right attack
{
m=1;
break;
}
}//end for
if(m==1)
break;
}//end for
pattackList=NULL;
psNode->get_childNodes(&pattackList);
int nmethods=pattackList->Getlength();
for(i=0;i<nmethods;i++)
{
psonNode=pattackList->Getitem(i);
methodpAttrs=NULL;
psonNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
if(strgetName=="name"&&strgetValue==attack) //get the right attack
{
l=1;
break;
}
}//end for
if(l==1)
break;
}//end for
//delete
pattackList=NULL;
psonNode->get_childNodes(&pattackList);
num=pattackList->Getlength();
//printf("%d",num);
//methodpAttrs=NULL;
int index=0;
for(int i=0;i<num;i++) //delete
{
pattNode=pattackList->Getitem(index);
MSXML::IXMLDOMNamedNodeMapPtr methodpAttr = NULL;
pattNode->get_attributes(&methodpAttr);
methodpAttr->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttr->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
if(strgetName=="name"&&strgetValue=="php/kbholespath") //get the right attack
{
psonNode->removeChild(pattNode);
pDoc->save(vFile);
}
else
index++;
}//end for
}
}
}
}catch(...)
{
}
3、节点的建立
MSXML::IXMLDOMDocument* pDoc=NULL;
MSXML::IXMLDOMElementPtr pNode;
MSXML::IXMLDOMElementPtr psNode;
MSXML::IXMLDOMElementPtr proNode;
MSXML::IXMLDOMElementPtr pattNode;
MSXML::IXMLDOMElementPtr pmethNode;
MSXML::IXMLDOMElementPtr pparmNode;
MSXML::IXMLDOMElementPtr psonNode;
try
{
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(MSXML::CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,MSXML::IID_IXMLDOMDocument,(void**)&pDoc)))
{
CComVariant vFile(path);
pDoc->load(vFile);
MSXML::IXMLDOMElementPtr pxmlRoot;
pxmlRoot=pDoc->documentElement;
int length=arr.GetSize();
pDoc->raw_createElement((_bstr_t)"nessus",&pNode); //建立nessus节点
pNode->setAttribute((_bstr_t)"id",(_bstr_t)arr.GetAt(0));
pNode->setAttribute((_bstr_t)"info",(_bstr_t)arr.GetAt(1));
pNode->setAttribute((_bstr_t)"vulnerable",(_bstr_t)"true");
pNode->setAttribute((_bstr_t)"hasinformation",(_bstr_t)"true");
pxmlRoot->appendChild(pNode);
pDoc->raw_createElement((_bstr_t)"name",&psNode);//建立name节点
CString strname=arr.GetAt(1)+".nasl";
psNode->Puttext((_bstr_t)strname);
pNode->appendChild(psNode);
pDoc->raw_createElement((_bstr_t)"discription",&proNode); //建立discription节点
proNode->Puttext((_bstr_t)arr.GetAt(2));
pNode->appendChild(proNode);
pDoc->raw_createElement((_bstr_t)"required",&pattNode);//建立required节点
pNode->appendChild(pattNode);
if(length>3)
{
pDoc->raw_createElement((_bstr_t)"script",&pmethNode);
int j=length-3;
CString len;
len.Format("%d",j);
pmethNode->setAttribute((_bstr_t)"num",(_bstr_t)len);
pattNode->appendChild(pmethNode);
for(int i=3;i<length;i++)
{
pDoc->raw_createElement((_bstr_t)"value",&pparmNode);
pparmNode->Puttext((_bstr_t)arr.GetAt(i));
pmethNode->appendChild(pparmNode);
}
}
pDoc->raw_createElement((_bstr_t)"attacks",&psonNode);
pNode->appendChild(psonNode);
pDoc->save(vFile);
}
}
}catch(...)
{
}
相关文章推荐
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- oracle中对于xmlType数据节点的增删改
- xml节点 增删改
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- xml节点 增删改
- MFC下XML遍历同名节点(Node)
- c#对xml多属性节点的增删改操作
- MFC下XML遍历同名节点(Node)
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- 如何才能将修改(对元素节点增删改)后的XML文件parse回去呢(使结果保存下来)?
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- 递规法遍历xml树的属性和节点值
- MFC读写XML文件
- XML节点元素命名规则
- 无法解密受保护的XML节点“DTS:Password”
- xml节点删除修改