gpt4 book ai didi

c++ - getElementsByTagName() 不适用于 DOMDOCUMENT60 的特殊字符

转载 作者:行者123 更新时间:2023-11-30 02:36:31 27 4
gpt4 key购买 nike

我正在尝试从此处附加的 xml 文件 (data.xml) 中获取特定的图书 ID。这工作正常 ---- getElementsByTagName(_bstr_t("book"))但是当我搜索特定的图书 ID 时它正在处理异常--- getElementsByTagName(_bstr_t("book[@id='bk101']"))

DOMDOCUMENT60 中存在此问题

我不知道为什么它不能处理特殊字符。有没有人以前遇到过这个问题或有什么建议?

#include "stdafx.h"

#include<iostream>
#include "Msxml.h"
#import "msxml6.dll"
using namespace MSXML2;
#include "stdlib.h"
#include "stdio.h"
#include <string>
using namespace std;

void LoadXML()
{
HRESULT hr1, hr;
hr1 = CoInitialize(NULL); //without this, cocreateinstance returns null pointer.

_variant_t vaNodeVal("C://data.xml");

VARIANT var1 = vaNodeVal;
CComPtr<IXMLDOMDocument2> sSourceInputXml = 0;
hr = sSourceInputXml.CoCreateInstance(__uuidof(MSXML2::DOMDocument60));
int ii=10;
if (hr == S_OK && sSourceInputXml != NULL)
{
hr = sSourceInputXml->load(var1);
int err = GetLastError();
ii=20;
try
{
ii = 30;

CComQIPtr<MSXML2::IXMLDOMNodeList> xmlACPInput;
std::cout << "\nworking";
xmlACPInput = sSourceInputXml->getElementsByTagName(_bstr_t("book[@id='bk101']")); //throwing exception
std::cout << "\n not working";
long lCount = 0;
xmlACPInput->get_length(&lCount);
bool isACP = false;
if (lCount == 0)
{
isACP = false;
}
else
{
isACP = true;
}
}
catch (exception ex)
{
ii = 1101;

}
catch (...)
{
ii = 1100;


}
}


CoUninitialize();
}


int _tmain(int argc, _TCHAR* argv[])
{
LoadXML();
return 0;
}


<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>
After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.
</description>
</book>
</catalog>

最佳答案

来自 https://msdn.microsoft.com/en-us/library/bb985161.aspx

Since the expression can use either the XPath 1.0 or XSL Patterns syntax, there needs to be a way to specify the selection language before using these APIs. To maintain backwards compatibility with existing code, the default selection language is XSL Patterns. To change the current selection language to XPath, call the new setProperty method (see IXMLDOMDocument2) using the SelectionLanguage property name and the value

DomDocument30 的默认选择语言是 XSL Patterns,它是 XPath 1.0 的非标准 Microsoft 实现前身。
并且它隐含地支持将 XSL 模式与 getElementsByTagName 方法一起使用。

由于 DomDocument60 不支持 XSL Patterns,它的默认选择语言是 XPath 并且它的 getElementsByTagName 方法严格要求一个名称而不是一个模式或查询。因此,您需要使用 selectNodes 使用 XPath 进行查询方法。

在 DomDocument60 中,旧的 getElementsByTagName("book[@id='bk101']") 相当于 selectNodes("//book[@id='bk101']")

所以试一试:

xmlACPInput = sSourceInputXml->selectNodes(_bstr_t("//book[@id='bk101']"));

关于c++ - getElementsByTagName() 不适用于 DOMDOCUMENT60 的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32583197/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com