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

标签 c++ xml msxml xmldom

我正在尝试从此处附加的 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/

相关文章:

javascript - 用于使用命名空间解析 XML 的 JavaScript 库?

.net - 如何声明返回节点集的用户定义函数?

c++ - 为什么我不能使用 xpath 来解析默认 namespace 中的节点?

c++ - pqxx 返回刚刚插入的行的 id

c++ - boost::bind() 绑定(bind)额外的参数?

python - 解析除某些属性值之外的 float 和返回路径

xml - Eclipse 中缺少 xml 配置选项卡?

xml - 在 VBA 中输出 MSXML2 XML 文档的文本时如何获取新行?

c++ - 在 C++ 中避免继承层次结构中的重复代码

c++ - Visual Studio /子系统 :POSIX not appearing in 2008