在线等~~急救~~MSXML操作

ywolf0214 2008-04-10 11:28:36
我有一个XML格式的文件,内容如下:
<?xml version="1.0" encoding="UTF-16" standalone="no" ?>
<result>

<interpretation confidence="84.00" grammar="grammar_demo">
<instance>线程</instance>
<input mode="speech">线程</input>
</interpretation>

<interpretation confidence="62.00" grammar="grammar_demo">
<instance>进程</instance>
<input mode="speech">进程</input>
</interpretation>

</result>
现需要将instance当中的值取出来,即取出“线程”“进程”。希望高手不吝赐教!为感!我用的是vc.net
另外,我也参考了网上的部分XML操作,取出的只是例如上面XML中的confidence,grammar以及这两个的值。也就是说取出的是尖括号内部元素属性值。现在我要取出的是两尖括号中间的值。
刚来论坛,没多少分,希望大家能够体谅一下,谢谢!
...全文
132 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ywolf0214 2008-04-14
  • 打赏
  • 举报
回复
谢谢!这样果然行,结贴了!呵呵!
ywolf0214 2008-04-11
  • 打赏
  • 举报
回复
问题部分解决了,可以如下:
XmlDocument^ doc = gcnew XmlDocument;
doc->Load( "C:\\re.xml" );
XmlNode^ book;
XmlNode^ root = doc->DocumentElement;
book = root->SelectSingleNode( "//result/interpretation/instance/text()");
只不过得到的只是第一个,不知道后面节点怎么得到,期望大家回答。另外在文件开始声明:
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
还是感谢HDT!~
ywolf0214 2008-04-11
  • 打赏
  • 举报
回复
hdt:
我编译了一下程序,也就是这3行代码
XmlDocument doc=new XmlDocument();
doc.Load("c:\\re.xml");
XmlNode xnserver = doc.SelectSingleNode("//result/interpretation/instance/text()");
出现了以下几个错误:
1>.\CGDDlg.cpp(629) : error C3149: 'System::Xml::XmlDocument' : cannot use this type here without a top-level '*'
1>.\CGDDlg.cpp(629) : error C3828: 'System::Xml::XmlDocument': placement arguments not allowed while creating instances of managed classes
1>.\CGDDlg.cpp(631) : error C3149: 'System::Xml::XmlNode' : cannot use this type here without a top-level '*'
1>.\CGDDlg.cpp(631) : error C3622: 'System::Xml::XmlNode': a class declared as '__abstract' cannot be instantiated
1> c:\windows\microsoft.net\framework\v2.0.50727\system.xml.dll : see declaration of 'System::Xml::XmlNode'
该怎么办呢?
tzqqkl 2008-04-11
  • 打赏
  • 举报
回复
libxml挺好用的
tzqqkl 2008-04-11
  • 打赏
  • 举报
回复
学习一下
真相重于对错 2008-04-11
  • 打赏
  • 举报
回复
XmlNodeList^ xnl = root->SelectNodes()
ywolf0214 2008-04-10
  • 打赏
  • 举报
回复
谢谢2楼的给予的答案,但是我还有疑问呵呵麻烦解答一下:我用的是MSXML,下面是我的部分代码:
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(hr))
{
MessageBox(L"Error!");
return 0;
}
load("c:\\re.xml");
MSXML2::IXMLDOMElementPtr childNode;
childNode = (MSXML2::IXMLDOMElementPtr)(pDoc->selectSingleNode("//instance"));

MSXML2::DOMNodeType nodeType;
childNode->get_nodeType(&nodeType);

MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
MSXML2::IXMLDOMNodeListPtr nodeList;
MSXML2::IXMLDOMNodePtr pAttrItem;
re.xml就是我那文件。接下来我该怎么做呢?那些地方需要更改?我是新手,见谅~~~
真相重于对错 2008-04-10
  • 打赏
  • 举报
回复
XmlDocument.SelectNodes("//result/interpretation/instance/text()");
scq2099yt 2008-04-10
  • 打赏
  • 举报
回复
UP
真相重于对错 2008-04-10
  • 打赏
  • 举报
回复
打开项目托管支持
using namespace System;
using namespace System::Xml;
ywolf0214 2008-04-10
  • 打赏
  • 举报
回复
用.net中的XmlDocument该怎么做呢?要包含什么头文件或库?
ywolf0214 2008-04-10
  • 打赏
  • 举报
回复
非常感谢!
真相重于对错 2008-04-10
  • 打赏
  • 举报
回复
msxml 组件 和 .net 下的xml类还是有些语法区别的,但是操作xpath 原理是一样的
.net 下有xmldocument , msxml 组件有IXMLDOMComment接口
同样都有SelectNodes
#include <stdio.h>
#import <msxml4.dll>
using namespace MSXML2;

void dump_com_error(_com_error &e);

int main(int argc, char* argv[])
{
CoInitialize(NULL);
try{
IXMLDOMDocument2Ptr pXMLDoc = NULL;
HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument40));

// Set parser property settings
pXMLDoc->async = VARIANT_FALSE;

// Load the sample XML file
hr = pXMLDoc->load("hello.xsl");

// If document does not load report the parse error
if(hr!=VARIANT_TRUE)
{
IXMLDOMParseErrorPtr pError;
pError = pXMLDoc->parseError;
_bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline())
+ _bstr_t("\n")+ _bstr_t(pError->Getreason());
MessageBox(NULL,parseError, "Parse Error",MB_OK);
return 0;
}
// Otherwise, build node list using SelectNodes
// and returns its length as console output
else
pXMLDoc->setProperty("SelectionLanguage", "XPath");
// Set the selection namespace URI if the nodes
// you wish to select later use a namespace prefix
pXMLDoc->setProperty("SelectionNamespaces",
"xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
IXMLDOMElementPtr pXMLDocElement = NULL;
pXMLDocElement = pXMLDoc->documentElement;
IXMLDOMNodeListPtr pXMLDomNodeList = NULL;
pXMLDomNodeList = pXMLDocElement->selectNodes("//xsl:template");
int count = 0;
count = pXMLDomNodeList->length;
printf("The number of <xsl:template> nodes is %i.\n", count);
}
catch(_com_error &e)
{
dump_com_error(e);
}
return 0;
}

void dump_com_error(_com_error &e)
{
printf("Error\n");
printf("\a\tCode = %08lx\n", e.Error());
printf("\a\tCode meaning = %s", e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}
Try It!
ywolf0214 2008-04-10
  • 打赏
  • 举报
回复
我顶!没人回答……
ywolf0214 2008-04-10
  • 打赏
  • 举报
回复
怎么不回答了呀,哎……2楼的XmlDocument在VC中哪个头文件里呀,我用
#import<System.XML.dll>
using namespace System.Xml;
但是编译时说 加载类型库/DLL 时出错。注册System.XML.dll时弹出没有找到输入点,无法注册。不解……

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧