如何用vc创建和读取xml文件??
如何用vc创建和读取xml文件??
能给一个简单的例子吗?? 问题点数:60、回复次数:7 Top
1 楼 edaynet(忆天) 回复于 2005-04-28 05:19:36 得分 60
void CXmlDlg::OnCreate()
{
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot ;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
}
pDoc->raw_createElement((_bstr_t)(char*)"china", &xmlRoot);
//MSXML2::IXMLDOMNodePtr childnode = xmlnode;
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");
}
void CXmlDlg::OnGet()
{
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
}
pDoc->load("f:\\he.xml");
MSXML2::IXMLDOMElementPtr childNode ;
childNode =
(MSXML2::IXMLDOMElementPtr)(pDoc->selectSingleNode("//City"));
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->get_attributes(&pAttrs);
long nCount ;
pAttrs->get_length(&nCount);
for(int i = 0 ; i < nCount ; i++)
pAttrs->get_item(i,&pAttrItem);
}
Top
2 楼 juststone(灰虫轨迹) 回复于 2005-04-28 06:42:11 得分 0
CMarkup http://www.firstobject.com/
写
CMarkup xml;
xml.AddElem( "ORDER" );
xml.AddChildElem( "ITEM" );
xml.IntoElem();
xml.AddChildElem( "SN", "132487A-J" );
xml.AddChildElem( "NAME", "crank casing" );
xml.AddChildElem( "QTY", "1" );
CString csXML = xml.GetDoc();
<ORDER>
<ITEM>
<SN>132487A-J</SN>
<NAME>crank casing</NAME>
<QTY>1</QTY>
</ITEM>
</ORDER>
条件是XML不能太大,在50M以内最好
tinyxml 好像也不错,不过没用过
http://sourceforge.net/projects/tinyxml/Top
3 楼 cwcsdn(nic) 回复于 2005-04-28 13:47:16 得分 0
to edaynet 你跟我自己找的一样!
这个例子是创建一个文件,我是想做一个像日志一样,每天增加一条记录,保存的这个文件不断增大!Top
4 楼 edaynet(忆天) 回复于 2005-04-28 16:30:33 得分 0
你的问题目是:
如何用vc创建和读取xml文件??
现在又说“我是想做一个像日志一样,每天增加一条记录,保存的这个文件不断增大”。
你搞什么,不写清楚。 凸^_^凸Top
5 楼 rushinger(阮祥哥) 回复于 2005-04-28 16:33:52 得分 0
搞什么,害得人家瞎忙老半天Top
6 楼 xuzheng318(忧郁王子) 回复于 2005-04-28 16:34:40 得分 0
xml操作简单得例子
Dim makexml As New XmlDocument()
Dim newattr As XmlAttribute
makexml.LoadXml("<T><qiang><frametype><left>ok</left></frametype></qiang></T>")
Dim makexmlnode As XmlNode = makexml.SelectSingleNode("//qiang/frametype/left")
Dim newnode As XmlNode
newnode = makexml.CreateElement("data")
makexmlnode.AppendChild(newnode)
newattr = makexml.CreateAttribute("type")
newattr.Value = "login"
newnode.Attributes.Append(newattr)
Response.Write(Server.HtmlEncode(makexml.InnerXml))
Response.End()
Dim x As String
x = "<Treenode/>"
Dim d As New XmlDocument()
d.LoadXml((x))
Dim n As XmlAttribute
n = d.CreateAttribute("id")
n.Value = "abc"
d.DocumentElement.Attributes.Append(n)
n = d.CreateAttribute("text")
n.Value = "abc"
d.DocumentElement.Attributes.Append(n)
Response.Write(Server.HtmlEncode(d.OuterXml))