怎么使用XMLDOMDocument生成一段xml代码?
发布网友
发布时间:2022-12-21 07:21
我来回答
共2个回答
热心网友
时间:2023-10-07 22:56
1.
BOOL CXmlApp::InitInstance()
{
AfxOleInit();
...
}
2.
#import <msxml4.dll>
3.
void CXmlDlg::OnButton1()
{
// TODO: Add your control notification handler code here
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot ;
//创建DOMDocument对象
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
}
//根节点的名称为china
pDoc->raw_createElement((_bstr_t)(char*)"china", &xmlRoot);
pDoc->raw_appendChild(xmlRoot, NULL);
MSXML2::IXMLDOMElementPtr childNode ;
pDoc->raw_createElement((_bstr_t)(char*)"City", &childNode);
childNode->Puttext("WuHan");//节点值
childNode->setAttribute("population","8,000,000");//属性名,属性值
childNode->setAttribute("area","10000");
xmlRoot->appendChild(childNode);
pDoc->raw_createElement((_bstr_t)(char*)"City", &childNode);
childNode->Puttext("ShangHai");
childNode->setAttribute("population","12,000,000");
childNode->setAttribute("area","12000");
xmlRoot->appendChild(childNode);
//保存到文件
//如果不存在就建立,存在就覆盖
pDoc->save("f:\he.xml");
}
生成的he.xml如下:
<china><City population="8,000,000" area="10000">WuHan</City><City population="12,000,000" area="12000">ShangHai</City></china>
---------------还有个例子
#include "stdafx.h"
#include "iostream.h"
#include "msxml.h"
#include "atlbase.h"
#import "msxml.dll" //引入类型库
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int Exit();
void LoadFromString();
void CreateXML();
//XML文本模板
_bstr_t XMLTemple=" -11 9 ";
int main()
{
cout << " XML编程--示范程序 " << endl;
CoInitialize(NULL); //初始化COM 环境
cout << "------生成新的XML文本 ------" << endl;
CreateXML();
cout << "------读取已有XML文本 ------" << endl;
LoadFromString();
return Exit();
}
void CreateXML()
{
MSXML::IXMLDOMDocumentPtr pDoc;
HRESULT hr =pDoc.CreateInstance(__uuidof(MSXML::DOMDocument));
if(!SUCCEEDED(hr))
{
cout << "无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!" << endl;
Exit();
}
MSXML::IXMLDOMElementPtr pDocElement=pDoc->createElement("China");
pDoc->appendChild(pDocElement);
cout << "生成树根:\n" << pDoc->xml << endl;
MSXML::IXMLDOMElementPtr pNewChildElement;
pNewChildElement=pDoc->createElement("Beijing");
pNewChildElement->Puttext("-11");
pDocElement->appendChild(pNewChildElement);
cout << "添加节点:\n" << pDoc->xml << endl;
pNewChildElement=pDoc->createElement("Shanghai");
pNewChildElement->Puttext("9");
pNewChildElement->setAttribute("Weather",_variant_t("Cloudy"));
pDocElement->appendChild(pNewChildElement);
cout << "再添加节点:\n" << pDoc->xml << endl;
pDocElement->removeChild (pNewChildElement);
cout << "把刚加入的节点删除:\n" << pDoc->xml << endl;
}
void LoadFromString()
{
MSXML::IXMLDOMDocumentPtr pDoc;
HRESULT hr =pDoc.CreateInstance(__uuidof(MSXML::DOMDocument));
if(!SUCCEEDED(hr))
{
cout << "无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!" << endl;
Exit();
}
pDoc->loadXML(XMLTemple);
cout << "读取结果:\n" << pDoc->xml << endl;
MSXML::IXMLDOMElementPtr pDocElement=pDoc->GetdocumentElement();
MSXML::IXMLDOMElementPtr pElement=pDocElement->selectSingleNode("Shanghai");
pDocElement->removeChild(pElement);
cout << "定位删除Shanghai节点:\n" << pDoc->xml << endl;
cout << "保存结果 Save .....(模拟而已)" << endl;
cout << "\n 好了,就这么简单" << endl;
}
int Exit()
{
getchar();
return 1;
}
热心网友
时间:2023-10-07 22:56
google
XMLDOMDocument xml 生成