VC 读取xml文件--load函数返-1的异常

shmiloveyou 2013-12-25 03:32:43
在MFC中读取xml配置文件时,代码中“注释1”处的返回值一直为-1。不知道是什么原因,请大家帮忙解决下这个问题。请大家针对这个问题帮助我,建议我使用别的库函数(比如:Boost的property_ptree)来操作xml文件的大牛尽量帮我解决这个问题,谢谢。
异常的语句:
HRESULT loadrs = pDoc->load("G:\\XML读写\\OperateXML\\test.xml");
//注释1:一直有异常,loadrs的值为-1
if( S_OK != loadrs )
MessageBox("加载XML错误");

xml文件的内容如下:

<China>
<City population="7000" area="2000">shanghai</City>
<City population="39999" area="3322">beijing</City>
</China>


VC中读取操作代码:

#import "C:\\WINDOWS\\system32\\msxml3.dll"
using namespace MSXML2;

void COperateXMLDlg::OnBtnGetXML()
{
m_list.DeleteAllItems(); //m_list是ListControl控件绑定的一个变量
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(hr))
{
MessageBox("Error!");
return;
}

HRESULT loadrs = pDoc->load("G:\\XML读写\\OperateXML\\test.xml");
//注释1:一直有异常,loadrs的值为-1,但是其后的代码没有问题,数据读取也正常
if( S_OK != loadrs )
MessageBox("加载XML错误");
MSXML2::IXMLDOMElementPtr childNode;
childNode = (MSXML2::IXMLDOMElementPtr)(pDoc->selectSingleNode("//City"));
//查询字串中的“//”表示所有行查找

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

MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
MSXML2::IXMLDOMNodeListPtr nodeList;
MSXML2::IXMLDOMNodePtr pAttrItem;
childNode->get_attributes(&pAttrs);
childNode->get_childNodes(&nodeList);
long nCount, iCount;
pAttrs->get_length(&nCount); //获取节点属性个数
nodeList->get_length(&iCount); //获取节点个数
for(int i = 0; i < iCount; i++) //根据需要可以添加数据到ListControl中
{
for(int j = 0; j < nCount; j++)
{
pAttrs->get_item(j, &pAttrItem);
CString strAttrName = (char*)(_bstr_t)pAttrItem->nodeName;
CString strAttrValue = (char*)(_bstr_t)pAttrItem->nodeTypedValue;
m_list.InsertItem(i, strAttrName);
m_list.SetItemText(i, 1, strAttrValue);
}
}
}


...全文
283 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2013-12-26
  • 打赏
  • 举报
回复
你#import msxml dll 后 vs 会自动封装那个com组件的接口 原来的load确实返回hresult;但封装后会返回variant_bool 右击那个函数 转到定义会看到

inline VARIANT_BOOL IXMLDOMDocument::load ( const _variant_t & xmlSource ) {
    VARIANT_BOOL _result = 0;
    HRESULT _hr = raw_load(xmlSource, &_result);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _result;
}
zhuyf87 2013-12-26
  • 打赏
  • 举报
回复
raw_load是个纯虚函数,应该由IXMLDOMDocument的派生类来实现。如果找不到,估计是微软封装起来了。 微软的组件或是库,核心一般都不会开源的吧。还是喜欢TinyXml等开源的库,感觉轻量级。
shmiloveyou 2013-12-26
  • 打赏
  • 举报
回复
引用 7 楼 hdt 的回复:
你#import msxml dll 后 vs 会自动封装那个com组件的接口
原来的load确实返回hresult;但封装后会返回variant_bool
右击那个函数 转到定义会看到

谢谢您的帮助,正如您说所,在工程下发现了这两个文件

开始没太注意这两个编译后生成的文件,在选中load函数查看其第一后转到了msxml6.tli文件中,此函数的定义的确如您说所提供的代码一致。

有点不懂的就是“raw_load”函数是个虚函数
    virtual HRESULT __stdcall raw_load (
VARIANT xmlSource,
VARIANT_BOOL * isSuccessful ) = 0;

具体的函数实现在哪儿能看到?或是在哪个dll或是lib文件中?您能帮我解决下这个困惑吗?谢谢。
sd__q 2013-12-25
  • 打赏
  • 举报
回复
IXMLDOMDocument是微软定义的抽象类,而MSXML2::IXMLDOMDocumentPtr实现了这个抽象类的方法
sd__q 2013-12-25
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/en-us/library/ms757878%28v=vs.85%29.aspx
shmiloveyou 2013-12-25
  • 打赏
  • 举报
回复
[/quote]lz注意看load函数的返回值是VARIANT_BOOL,并不是HRESULT,再仔细读读MSDN文档,验证是否正确加载用VARIANT_TRUE和VARIANT_FALSE,并不是S_OK[/quote] 阁下有发现我使用的语句特点:
pDoc->load("G:\\XML读写\\OperateXML\\test.xml");
仅有一个参数的load函数。 你所说的“VARIANT_TRUE和VARIANT_FALSE”是由有两个参数的load函数的第二参数返回的值。我不明白,这两个函数之间返回的值的差别到底是怎么回事!能把你查MSDN的内容粘上来看看吗?
sd__q 2013-12-25
  • 打赏
  • 举报
回复
引用 2 楼 qq2399431200 的回复:
[quote=引用 1 楼 sd__q 的回复:] -1就是成功的返回值呐,msdn中 Return Value Boolean. Returns True if the load succeeded; False if the load failed. load函数的返回值为VARIANT_BOOL,查看定义
/* 0 == FALSE, -1 == TRUE */
typedef short VARIANT_BOOL;
请问你使用的MSDN是那个版本的?我的是MSDN Libary2010,说明如下:
IXMLDOMDocument::load	Visual Studio 2010

The load method loads an XML document with the contents of the file at the specified location.

HRESULT load(
VARIANT varXmlSource, 
VARIANT_BOOL* varIsSuccessful
);

Arguments

varXmlSource
	

[in] VARIANT containing a URL that specifies the location of the XML file. See Remarks.

bIsSuccessful
	

[out] Pointer to a VARIANT_BOOL indicating whether the load was successful.
Return Value

If the method succeeds, it returns S_OK. If it fails, it returns an HRESULT error code.
如果成功返回“S_OK”,我代码中的判断也是“S_OK”啊:
HRESULT loadrs = pDoc->load("G:\\XML读写\\OperateXML\\test.xml");
 //注释1:一直有异常,loadrs的值为-1,但是其后的代码没有问题,数据读取也正常
 if( S_OK != loadrs )
    MessageBox("加载XML错误");
这个地方的“loadrs”值是-1,但是S_OK定义的值是1,主要是后面的代码没有影响,xml文档中的信息也读取出来了! [/quote]lz注意看load函数的返回值是VARIANT_BOOL,并不是HRESULT,再仔细读读MSDN文档,验证是否正确加载用VARIANT_TRUE和VARIANT_FALSE,并不是S_OK
shmiloveyou 2013-12-25
  • 打赏
  • 举报
回复
引用 1 楼 sd__q 的回复:
-1就是成功的返回值呐,msdn中 Return Value Boolean. Returns True if the load succeeded; False if the load failed. load函数的返回值为VARIANT_BOOL,查看定义
/* 0 == FALSE, -1 == TRUE */
typedef short VARIANT_BOOL;
请问你使用的MSDN是那个版本的?我的是MSDN Libary2010,说明如下:
IXMLDOMDocument::load	Visual Studio 2010

The load method loads an XML document with the contents of the file at the specified location.

HRESULT load(
VARIANT varXmlSource, 
VARIANT_BOOL* varIsSuccessful
);

Arguments

varXmlSource
	

[in] VARIANT containing a URL that specifies the location of the XML file. See Remarks.

bIsSuccessful
	

[out] Pointer to a VARIANT_BOOL indicating whether the load was successful.
Return Value

If the method succeeds, it returns S_OK. If it fails, it returns an HRESULT error code.
如果成功返回“S_OK”,我代码中的判断也是“S_OK”啊:
HRESULT loadrs = pDoc->load("G:\\XML读写\\OperateXML\\test.xml");
 //注释1:一直有异常,loadrs的值为-1,但是其后的代码没有问题,数据读取也正常
 if( S_OK != loadrs )
    MessageBox("加载XML错误");
这个地方的“loadrs”值是-1,但是S_OK定义的值是1,主要是后面的代码没有影响,xml文档中的信息也读取出来了!
sd__q 2013-12-25
  • 打赏
  • 举报
回复
-1就是成功的返回值呐,msdn中 Return Value Boolean. Returns True if the load succeeded; False if the load failed. load函数的返回值为VARIANT_BOOL,查看定义
/* 0 == FALSE, -1 == TRUE */
typedef short VARIANT_BOOL;

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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