应该可以设置id,根据id获取值
不好意思啦!借用zhangmiger代码一下。
XmlDocument
doc
=
new
XmlDocument();
docLoad("//XMLFile1xml");
XmlNodeList
nodes
=
docSelectNodes("/Dimap_Document/IMAGING_DATE/vertex");
foreach
(XmlNode
node
in
nodes)
{
if(nodeAttributes["id"]value=="v2"){
ConsoleWriteLine(nodeInnerText);
}
}
ConsoleRead();
XMLFile1xml文件的内容:
<xml
version="10"
encoding="utf-8"
>
<Dimap_Document>
<IMAGING_DATE>
<vertex
id="v1">
a
</vertex>
<vertex
id="v2">
b
</vertex>
<vertex
id="v3">
c
</vertex>
</IMAGING_DATE>
</Dimap_Document>
下面是我写的生成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 = 0; i < MAX_ITEM; i++)
{
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 =0; j < MAX_TAGS; j++)
{
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 = 0; i < MAX_ITEM; i++)
{
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 =0; j < MAX_TAGS; j++)
{
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 = 0; i < MAX_YEAR; i++ )
{
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;
}
ML在Win32程序方面应该没有在Web方面应用得多,很多Win32程序也只是用XML来存存配置信息而已,而且没有足够的好处的话还不如用ini。VC++里 *** 作XML有两个库可以用:MSXML和XmlLite。MSXML又细分了两种接口:DOM和SAX2。XP没自带有XmlLite,只自带有2x、3x版的MSXML,不支持SAX2(需要MSXML 40以上),所以优先使用DOM。
DOM是以COM形式提供的,VC++里调用DOM可以分3种方法:
1、MFC里用CComPtr调用
2、SDK里直接调用DOM接口
3、SDK里用智能指针调用
这3种方法本质上是一样的,区别只不过在于需要编码的多少而已,用CComPtr可以极大的简化代码,下面是几个例子。
例子stocksxml:
<xml version="10" encoding="utf-8">
<root>
<node1>text1</node1>
<node2>
<childnode1 attrib1="value1" attrib2="value2"/>
<childnode2 attrib1="value1" attrib2="value2">childtext1</childnode2>
</node2>
</root>
这个例子应该包含了XML最常见的特征了吧?
MFC
MFC里可以直接使用DOM,不需要手动添加额外的头文件,只需要在CWinApp::InitInstance()里调用CoInitialize(NULL)初始化COM,在CWinApp::ExitInstance里调用CoUninitialize()释放COM就行了。
//读取XML
CComPtr<IXMLDOMDocument> spDoc; //DOM
spDocCoCreateInstance(CLSID_DOMDocument);
VARIANT_BOOL vb;
spDoc->load(CComVariant(OLESTR("stocksxml")), &vb); //加载XML文件
CComPtr<IXMLDOMElement> spRootEle;
spDoc->get_documentElement(&spRootEle); //根节点
CComPtr<IXMLDOMNodeList> spNodeList;
spRootEle->get_childNodes(&spNodeList); //子节点列表
long nLen;
spNodeList->get_length(&nLen); //子节点数
for (long i = 0; i != nLen; ++i) //遍历子节点
{
CComPtr<IXMLDOMNode> spNode;
spNodeList->get_item(i, &spNode);
ProcessNode(spNode); //节点处理函数
}
//写入XML
CComPtr<IXMLDOMNode> spNode;
spRootEle->selectSingleNode(OLESTR("/root/node1"), &spNode);
spNode->put_text(OLESTR("newText")); //写入text
spRootEle->selectSingleNode(OLESTR("/root/node2/childnode1/@attrib1"), &spNode);
spNode->put_nodeValue(CComVariant(OLESTR("newValue"))); //写入value
CComPtr<IXMLDOMNode> spNewNode;
spDoc->createNode(CComVariant(NODE_ELEMENT), OLESTR("childnode3"), OLESTR(""), &spNewNode); //创建新节点
spRootEle->selectSingleNode(OLESTR("/root/node2"), &spNode);
spNode->appendChild(spNewNode, &spNewNode); //将新节点加为node2的子节点
spNewNode->put_text(OLESTR("childtext2")); //写入新节点text
CComQIPtr<IXMLDOMElement> spEle = spNewNode; //注意这里使用CComQIPtr
spEle->setAttribute(OLESTR("attrib1"), CComVariant(OLESTR("value1")));//给新节点添加属性
spDoc->save(CComVariant(OLESTR("stocksxml")));
//节点处理函数
void ProcessNode(CComPtr<IXMLDOMNode>& spNode)
{
CComBSTR bsNodeName;
spNode->get_nodeName(&bsNodeName); //节点名
AfxMessageBox(COLE2CT(bsNodeName));
CComVariant varVal;
spNode->get_nodeValue(&varVal); //节点值
AfxMessageBox(COLE2CT(varValbstrVal));
DOMNodeType eNodeType;
spNode->get_nodeType(&eNodeType);
if (eNodeType == NODE_ELEMENT) //只有NODE_ELEMENT类型才能包含有属性和子节点
{
//递归遍历节点属性
CComPtr<IXMLDOMNamedNodeMap> spNameNodeMap;
spNode->get_attributes(&spNameNodeMap);
long nLength;
spNameNodeMap->get_length(&nLength);
for (long i = 0; i != nLength; ++i)
{
CComPtr<IXMLDOMNode> spNodeAttrib; //注意属性也是一个IXMLDOMNode
spNameNodeMap->get_item(i, &spNodeAttrib);
ProcessNode(spNodeAttrib);
}
//递归遍历子节点
CComPtr<IXMLDOMNodeList> spNodeList;
spNode->get_childNodes(&spNodeList);
spNodeList->get_length(&nLength);
for (long i = 0; i != nLength; ++i)
{
CComPtr<IXMLDOMNode> spChildNode;
spNodeList->get_item(i, &spChildNode);
ProcessNode(spChildNode);
}
}
}
对于<tag>text</tag>这样的节点,get_nodeValue会得到空,要得到"text"的话可以遍历子节点(只有一个子节点,它的nodeName为"#text",nodeType为NODE_TEXT,nodeValue就是"text");也可以用get_text直接得到"text",但是对于这样的节点<tag>text<childtag>childtext</childtag></tag>,get_text会同时得到"text"和"childtext",不过这样的节点应该是不允许的。
DOM里使用的字符串(BSTR)都是OLESTR类型,默认情况下OLESTR是Unicode字符,MFC里可以用COLE2CT把LPCOLESTR转换为LPCTSTR。
对于自己定义的XML,大多数时候不需要遍历,可以通过调用selectNodes、selectSingleNode指定XPath直接读取某个节点或属性:
CComPtr<IXMLDOMDocument> spDoc; //DOM
spDocCoCreateInstance(CLSID_DOMDocument);
VARIANT_BOOL vb;
spDoc->load(CComVariant(OLESTR("stocksxml")), &vb); //加载XML文件
CComPtr<IXMLDOMElement> spRootEle;
spDoc->get_documentElement(&spRootEle); //根节点
CComPtr<IXMLDOMNodeList> spNodeList;
CComPtr<IXMLDOMNode> spNode;
spRootEle->selectNodes(OLESTR("/root/node2/"), &spNodeList); //得到node2下的所有子节点
spRootEle->selectSingleNode(OLESTR("/root/node2/childnode1/@attrib1"), &spNode); //得到childnode1的attrib1属性
XPath的语法可以参考XML文档或MSDN。
SDK
SDK中也可以使用智能指针,和MFC没太大区别,同样很方便,直接给代码:
#include <iostream>
#include <tcharh>
#import <msxml3dll>
//节点处理函数
void ProcessNode(MSXML2::IXMLDOMNodePtr spNode)
{
std::cout << "nodeName: " << spNode->nodeName;
if (spNode->nodeType == NODE_ATTRIBUTE || spNode->nodeType == NODE_TEXT)
std::cout << "\tnodeValue: " << _bstr_t(spNode->nodeValue);
std::cout << std::endl;
if (spNode->nodeType == NODE_ELEMENT)
{
MSXML2::IXMLDOMNamedNodeMapPtr spNameNodeMap = spNode->attributes;
for (long i = 0; i != spNameNodeMap->length; ++i) //遍历节点属性
ProcessNode(spNameNodeMap->item);
MSXML2::IXMLDOMNodeListPtr spNodeList = spNode->childNodes;
for (long i = 0; i != spNodeList->length; ++i) //遍历子节点
ProcessNode(spNodeList->item);
}
}
int _tmain(int argc, _TCHAR argv[])
{
CoInitialize(NULL);
//读取XML
MSXML2::IXMLDOMDocumentPtr spXMLDoc;
spXMLDocCreateInstance(__uuidof(MSXML2::DOMDocument30));
spXMLDoc->load(L"stocksxml");
MSXML2::IXMLDOMElementPtr spRoot = spXMLDoc->documentElement; //根节点
MSXML2::IXMLDOMNodeListPtr spNodeList = spRoot->childNodes;
for (long i = 0; i != spNodeList->length; ++i) //遍历子节点
ProcessNode(spNodeList->item);
//写入XML
spRoot->selectSingleNode(L"/root/node1")->text = L"newText";
spRoot->selectSingleNode(L"/root/node2/childnode1/@attrib1")->nodeValue = L"newValue";
MSXML2::IXMLDOMNodePtr spNewNode = spRoot->selectSingleNode(L"/root/node2")->appendChild(
spXMLDoc->createNode(_variant_t(NODE_ELEMENT), L"childnode3", L"")
); //给node2创建新子节点childnode3
spNewNode->text = L"childtext2";
MSXML2::IXMLDOMElementPtr spEle = spNewNode;
spEle->setAttribute(L"attrib1", _variant_t(L"value1")); //添加新属性
spXMLDoc->save(_variant_t(L"stocksxml"));
spNewNodeRelease();
spEleRelease();
spNodeListRelease();
spRootRelease();
spXMLDocRelease();
CoUninitialize();
system("pause");
return 0;
}
以上就是关于c# 如何让读取xml文件重复节点内的值全部的内容,包括:c# 如何让读取xml文件重复节点内的值、求 C++读写取XML 文件方法!网上复制的让开、如何在VC中使用XML等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)