MSXML4
//引入msxml4.dll
#import "C:/WINNT.0/system32/msxml4.dll"
//创建XMLDOMDocument指针
MSXML2::IXMLDOMDocumentPtr pXMLDoc
// 初始化COM接口
::CoInitialize(NULL)
// 通过智能指针创建一个解析器的实例
HRESULT hr
hr =pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30))
//加载文件
pXMLDoc->load("f://he.xml")
//在树中查找名为City的节点,"//"表示在任意一层查找
MSXML2::IXMLDOMElementPtr childNode
childNode = (MSXML2::IXMLDOMElementPtr)(pXMLDoc->selectSingleNode("//AUTHOR"))
//得到节点类型
MSXML2::DOMNodeType nodeType
childNode->get_nodeType(&nodeType)
//庆液宏节点名称埋桥
BSTR var
CString name
childNode->get_nodeName(&var)
name = (char*)(_bstr_t)var
//节点值
VARIANT varVal
childNode->get_nodeTypedValue(&varVal)
CString strValue = (char*)(_bstr_t)varVal
读属性:
//节点属性,放在链表中
MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL
MSXML2::IXMLDOMNodePtr pAttrItem
childNode = (MSXML2::IXMLDOMElementPtr)(pXMLDoc->selectSingleNode("//NUM"))
childNode->get_attributes(&pAttrs)
long nCount
pAttrs->get_length(&nCount)
for(int i = 0 i < nCount i++)
{
pAttrs->get_item(i,&pAttrItem)
//我们可以通过函数get_nodeName,get_nodeTypedValue得到属性名和属性值
//也可以直接得到
CString strAttrName = (char*)(_bstr_t)pAttrItem->nodeName
CString strAttrValue = (char*)(_bstr_t)pAttrItem->nodeTypedValue
}
另外
可以用MSXML DOM
创建一个文档对象的过程誉册。
HRESULT hr
IXMLDomDocument* pXMLDoc
IXMLDOMNode* pXDN
Hr=CoInitialize(NULL) //COM的初始化
hr=CoCreateInstance(CLSID_DOM Document,NULL,CLSCTX_INPPROC_SERVER,
IID_IXMLDOMDocument,(void**) &pXMLDoc)
hr=pXMLDoc->QueryInterface (IID_IXMLDOMNode,(void**)&pXDN)
使用文档中的createElement方法来创建一个节点装载和保存XML文件。通过load或者是loadXML方法可以从一个指定的URL来装载一个XML文档Save方法是用来把文档保存到一个指定的位置,用getElementsByTagName获得制定节点数据
二、
// 创建对象
CoInitialize(NULL)// 初始化COM
// 创建解析器实例
CComPtr<IXMLDOMDocument>spXMLDOM
BSTR bstrItemText
//long value
CString strbuf
HRESULT hr=spXMLDOM.CoCreateInstance (_uuidof(DOMDocument))
spXMLDOM->put_async(VARIANT_FALSE)
spXMLDOM->put_validateOnParse(VARIANT_FALSE)
spXMLDOM->put_resolveExternals(VARIANT_FALSE)
spXMLDOM->put_preserveWhiteSpace(VARIANT_TRUE)
_bstr_t path = _bstr_t(xmlPath)
// 装载XML文档。
hr=spXMLDOM->load(path)
// 1、读取里面全部文本
spXMLDOM->get_text(&bstrItemText)// new
strbuf = bstrItemText// new
// 2、读取一个节点
_bstr_t bstrSS("paragraph")
CComPtr<IXMLDOMNodeList>spParagraphs
spParagraphs=spXMLDOM->getElementsByTagName(bstrSS)//搜索"Paragrah"
//是否正确的得到了指向IDOMNodeList的指针。
hr = spParagraphs->get_length(&value)//得到所包含的NODE节点的个数
if(SUCCEEDED(hr))
...{
spParagraphs->reset()
for(int ii=0ii<valueii++)
...{
CComPtr<IXMLDOMNode>spParagraph
//得到具体的一个NODE节点
//hr = spParagraphs->get_item(ii,&spParagraph)
spParagraph = spParagraphs->nextNode()
if(SUCCEEDED(hr))
...{
spParagraph->get_text(&bstrItemText)//得到该节点相关的文本信息
strbuf += bstrItemText
strbuf += " "
}
}
}
下面是我写的生成XML文件的例子,主要是用递归来完成嵌套的XML生成。生成的格式为:
<Datas>
<Data>
<year>
</year>
<paper>
<name> </name>
<number></number>
<weight></weight>
<paper>
</paper>
.....
</paper>
</Data>
...
</Datas>
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std
const int MAX_YEAR = 6
const int MAX_NODENAME = 8
const int MAX_DEPTH = 1
const int MAX_ITEM = 15
const int MAX_TAGS = 50
enum NodeName
{
DATAS,
DATA,
YEAR,
PAPERS,
NUMBER,
WEIGHT,
TAG,
NAME
}
string NodeBegin[MAX_NODENAME] = {"<Datas>", "<Data>", "<year>", "<paper>", "<number>", "<weight>", "<tag>", "<name>"}
string NodeEnd[MAX_NODENAME] = {"</Datas>", "</Data>", "</year>", "</paper>", "<咐族碰/number>", "</weight>", "</tag>"衡谈, "</name>"}
string Years[MAX_YEAR] = {"2000","2001","2002","2003","2004","2005"}
//_T的穗孙意思是通知编译器,自行进行字符串的多字节/Unicode转换。
//而L表示,该字符串为Unicode版本。
ofstream outfile(L"首页.xml")
int Random(int i)
{
return (rand()%i+1)
}
void Recursive(int level, string parentName, string year)
{
if (level == 1)
{
for (int i = 0i <MAX_ITEMi++)
{
outfile<<NodeBegin[PAPERS]<<endl
outfile<<NodeBegin[NAME]<<parentName<<"-"<<i<<NodeEnd[NAME]<<endl
outfile<<NodeBegin[NUMBER]<<Random(10)<<NodeEnd[NUMBER]<<endl
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl
outfile<<NodeEnd[PAPERS]<<endl
}
for (int j =0j <MAX_TAGSj++)
{
outfile<<NodeBegin[TAG]<<endl
outfile<<NodeBegin[NAME]<<year<<":"<<parentName<<"-"<<j<<NodeEnd[NAME]<<endl
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl
outfile<<NodeEnd[TAG]<<endl
}
}
else
{
for (int i = 0i <MAX_ITEMi++)
{
char a[10]
string str
itoa(i, a, 10)
str = a
string curName = parentName+"-"+str
outfile<<NodeBegin[PAPERS]<<endl
outfile<<NodeBegin[NAME]<<curName<<NodeEnd[NAME]<<endl
outfile<<NodeBegin[NUMBER]<<Random(10)<<NodeEnd[NUMBER]<<endl
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl
Recursive(level-1, curName, year)
outfile<<NodeEnd[PAPERS]<<endl
}
for (int j =0j <MAX_TAGSj++)
{
outfile<<NodeBegin[TAG]<<endl
outfile<<NodeBegin[NAME]<<"Tag"<<year<<":"<<parentName<<"-"<<j<<NodeEnd[NAME]<<endl
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl
outfile<<NodeEnd[TAG]<<endl
}
}
}
void GenerateXML()
{
outfile<<NodeBegin[DATAS]<<endl
for (int i = 0i <MAX_YEARi++ )
{
outfile<<NodeBegin[DATA]<<endl
outfile<<NodeBegin[YEAR]<<Years[i]<<NodeEnd[YEAR]<<endl
Recursive(MAX_DEPTH, "学科主题",Years[i])
outfile<<NodeEnd[DATA]<<endl
}
outfile<<NodeEnd[DATAS]<<endl
}
void main()
{
locale::global(locale(""))
srand(time(NULL))
if (!outfile)
{
cout<<"can't open output file"<<endl
}
GenerateXML()
cout<<"Generate End."<<endl
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)