Discussion:
XML exception( my MSN : lookus.cn@gmail.com)
(too old to reply)
lookus
2006-04-27 08:47:37 UTC
Permalink
I'm a newer to xml. And i wanna to use msxml to read xml file.
here is my code
////// msxml_test.cpp
////// use MFC in a shared dll
#include <iostream>
#include <cmath>
#include <string>
#import <msxml4.dll>
#include <msxml.h>
using namespace std;

int main()
{

cout << "BEGIN" <<endl;

HRESULT hr;
long cnt;
try
{

hr = ::CoInitialize(NULL);

MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMNodeListPtr pNodeList;
MSXML2::IXMLDOMNodePtr pNode;

if(FAILED(hr))
throw "failed to com init";

hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if(FAILED(hr))
throw "failed to create DOM DOC";

pDoc->load("d:\\test.xml");

pDoc->get_childNodes(&pNodeList);

////// From here
pNodeList->get_length(&cnt);

cout << "LENGTH " << cnt << endl;

for(cnt = 0 ; (pNode = pNodeList->nextNode()) ; ++cnt);

cout << "Node count " << cnt <<endl;

////// to here

}
catch (string str)
{
cout << "Exception : " << str << endl;
}
catch (...)
{
cout << "Exception Unkonwn\n";
}

::CoUninitialize();

cout << "END" <<endl;

return 0;
}
///////////////////////

****************************** 1
when the test.xml is

<Book id="00322032"><Author>lookus</Author></Book>

it works well, and prints :
BEGIN
LENGTH 1
Node count 1
END

****************************** 2
however when test.xml is

<Book id="00322032"><Author>lookus</Author></Book>
<Bok id="003232"><Author>ookus</Author></Book>

it prints :
BEGIN
LENGTH 0
Node count 0
END

but it's excepted to print
BEGIN
LENGTH 2
Node count 2
END

################################# More
in msdn there is

IXMLDOMNodeList::nextNode
The nextNode method retrieves the next node in the IXMLDOMNodeList
collection.
HRESULT nextNode(
IXMLDOMNode** ppNextItem
);

but when i wrote i got "error C2660: 'nextNode' : function does not
take 1 parameters"

//////////////////////////////////
My MSN and email is ***@gmail.com.
I will appreciate your help.
Andy Dingley
2006-04-27 11:52:33 UTC
Permalink
Post by lookus
I'm a newer to xml. And i wanna to use msxml to read xml file.
<Book id="00322032"><Author>lookus</Author></Book>
<Bok id="003232"><Author>ookus</Author></Book>

That's not well-formed XML, because XML must only have _ONE_ root
element

<Library>
<Book id="00322032"><Author>lookus</Author></Book>
<Book id="003232"><Author>ookus</Author></Book>
</Library>

might work for you


I suspect that the call to load() is failing, but you're not checking
for the return value.

You then query an empty DOM, which (obviously) returns 0 content.
Joe Kesselman
2006-04-27 11:54:59 UTC
Permalink
Post by lookus
<Book id="00322032"><Author>lookus</Author></Book>
<Bok id="003232"><Author>ookus</Author></Book>
Ill-formed document. XML _must_ have a single top-level element. And of
course there's the typo of "Bok" which should have been "book".

Try restructuring as

<Books>
<Book id="00322032"><Author>lookus</Author></Book>
<Book id="003232"><Author>ookus</Author></Book>
</Books>
Post by lookus
but when i wrote i got "error C2660: 'nextNode' : function does not
take 1 parameters"
The standard DOM NodeList has no nextNode operation, and I don't use the
Microsoft implementation, so I can't advise.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Loading...