Link to home
Start Free TrialLog in
Avatar of Veeresh Virkar
Veeresh Virkar

asked on

getElementsByTagName() does not work with DomDocument60

Hi,

getElementsByTagName() works with DomDocument, but it does not work with DomDocument60.

In the below snippet, getElementsByTagName(_bstr_t(".//author"), &xml2); return a NULL pointer in xml2.

When i change from DomDocument60 to DomDocument, only then it works. i need to use DomDocument60 in my project.

Any idea how this issue can be fixed, if i use DomDocument60. Any help is appreciated.
Below is the snippet:

#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();  
}

Open in new window


Data.xml used is:
<?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>

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

please try this property:
https://msdn.microsoft.com/en-us/library/ms762283%28v=vs.85%29.aspx

I read that resolveExternals is False on DOMDocument60 and True on DOMDocument, by default.
so try to set it to try before loading the XML
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.