MSXML的loadXML无法加载甚至形成良好的XML [英] MSXML's loadXML fails to load even well formed xml
问题描述
我在c ++的MSXML之上写了一个包装。加载方法如下所示。
代码的问题是有时无法加载格式良好的XML。
I have written a wrapper on top of MSXML in c++ . The load method looks like as below.
The problem with the code is it fails to load well formed xml sometimes.
在将xml作为字符串传递之前,我做一个字符串搜索xmlns并替换all使用xmlns:dns发生xmlns。
在下面的代码中我删除bom字符。然后我尝试加载使用MSXML loadXML方法。如果加载成功,我按照代码中所示设置命名空间。
Before passing the xml as string I do a string search for xmlns and replace all occurrence of xmlns with xmlns:dns. In the code below I remove bom character. Then i try to load using the MSXML loadXML method . If load succeeds I set the namespace as shown in the code.
Class XmlDocument{
MSXML2::IXMLDOMDocument2Ptr spXMLDOM;
....
}
/ XmlDocument方法
// XmlDocument methods
void XmlDocument::Initialize()
{
CoInitialize(NULL);
HRESULT hr = spXMLDOM.CreateInstance(__uuidof(MSXML2::DOMDocument60));
if ( FAILED(hr) )
{
throw "Unable to create MSXML:: DOMDocument object";
}
}
bool XmlDocument::LoadXml(const char* xmltext)
{
if(spXMLDOM != NULL)
{
char BOM[3] = {0xEF,0xBB,0xBF};
//detect unicode BOM character
if(strncmp(xmltext,BOM,sizeof(BOM)) == 0)
{
xmltext += 3;
}
VARIANT_BOOL bSuccess = spXMLDOM->loadXML(A2BSTR(xmltext));
if ( bSuccess == VARIANT_TRUE)
{
spXMLDOM->setProperty("SelectionNamespaces","xmlns:dns=\"http://www.w3.org/2005/Atom\"");
return true;
}
}
return false;
}
我试图调试仍然无法解释为什么有时loadXML )无法加载甚至形成良好的XML。我在代码中做错了什么。非常感谢任何帮助。
I tried to debug still could not figure why sometimes loadXML() fails to load even well formed xmls. What am I doing wrong in the code. Any help is greatly appreciated.
感谢
JeeZ
Thanks JeeZ
推荐答案
p>有关此特定问题,请参阅传递给loadXML的字符串必须为UTF-16编码的BSTR 。
For this specific issue, please refer to Strings Passed to loadXML must be UTF-16 Encoded BSTRs.
总的来说,xml解析器不是为内存字符串解析设计的,例如loadXML不能识别BOM,它对编码有限制。相反,xml解析器被设计用于带有编码检测的字节数组形式,这对于标准解析器是至关重要的。为了更好地利用MSXML,请考虑从IStream或Win32文件加载。
Overall, xml parser is not designed for in memory string parsing, e.g. loadXML does not recognize BOM, and it has restriction on the encoding. Rather, an xml parser is designed for byte array form with encoding detection, which is critical for a standard parser. To better leverage MSXML, please consider loading from IStream or a Win32 file.
这篇关于MSXML的loadXML无法加载甚至形成良好的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!