getElementsByTagName()不使用DOMDOCUMENT60的特殊字符

时间:2015-09-15 10:03:39

标签: c++ xml msxml xmldom

我正在尝试从此处附加的xml文件中获取特定的书籍ID(data.xml)。 这工作正常---- getElementsByTagName(_bstr_t("book")) 但是当我搜索某个特定的书籍时 它正在通过例外--- 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>

1 个答案:

答案 0 :(得分:2)

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

  

因为表达式可以使用XPath 1.0或XSL Patterns   语法,需要有一种方法来指定选择语言   在使用这些API之前。保持向后兼容性   现有代码,默认选择语言是XSL Patterns。至   将当前选择语言更改为XPath,调用新语言   使用SelectionLanguage的setProperty方法(请参阅IXMLDOMDocument2)   属性名称和值

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']"));