gpt4 book ai didi

c++ - getElementsBytagName 不适用于 DomDocument60

转载 作者:太空宇宙 更新时间:2023-11-04 13:29:04 26 4
gpt4 key购买 nike

getElementsByTagName() 适用于 DomDocument,但不适用于 DomDocument60

在下面的代码片段中,getElementsByTagName(_bstr_t(".//author"), &xml2); 返回 xml2 中的 NULL 指针。

当我从 DomDocument60 更改为 DomDocument 时,它才起作用。我需要在我的项目中使用 DomDocument60。

知道如何解决这个问题,如果我使用 DomDocument60。感谢您的帮助。

下面是片段:

#include "stdafx.h"

#import "msxml6.dll" raw_interfaces_only raw_native_types
using namespace MSXML2;

#include "stdlib.h"
#include "stdio.h"
#include <string>
#include <list>
using namespace std;

void LoadXML()
{
HRESULT hr1, hr;
hr1 = CoInitialize(NULL);
_variant_t vaNodeVal("c://Data.xml");

VARIANT var1 = vaNodeVal;
CComPtr<IXMLDOMDocument2> sSourceInputXml = 0;
hr = sSourceInputXml.CoCreateInstance(__uuidof(MSXML2::DOMDocument60));

if (hr == S_OK && sSourceInputXml != NULL)
{
VARIANT_BOOL isSuccessful = VARIANT_FALSE;

hr = sSourceInputXml->load(var1, &isSuccessful);

if( hr == S_OK && isSuccessful == VARIANT_TRUE )
{

CComQIPtr<MSXML2::IXMLDOMNodeList> xml2;
sSourceInputXml->getElementsByTagName(_bstr_t(".//author"), &xml2); //xml2 returned over here is NULL

long lCount = 0;
xml2->get_length(&lCount);
bool isPresent = false;
if (lCount == 0)
{
isPresent = false;
}
else
{
isPresent = true;
}
}
}


CoUninitialize();
}

使用的Data.xml是:

<?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>

最佳答案

我也遇到过这个问题——getElementsByTagName 似乎只查看当前节点的直接子节点(例如,在文档节点上,“catalog”会返回一个元素,但“book”会返回无)。我不确定,但我相信这是因为 XML 6 不支持 XSLPattern 选择。如果您想坚持使用 DOMDocument60,我最好的建议(虽然不是最有效的)是在使用 getElementsByTagName 时递归节点树拿起你正在寻找的一切。

关于c++ - getElementsBytagName 不适用于 DomDocument60,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32241807/

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