You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Williams Roger <RW...@OLDMUTUAL.com> on 2001/03/15 10:07:24 UTC

NEWBIE: Trying to use MemBufInputSource in parse member function of DOMParser

Hi

I am trying to use the parse member function(with the DOMParrser) with a
MemBufInputSource instead of a XMLfile or URL. But the Node I get from trhe
NodeList is null. When I use a file instead of a membuf, it works fine. But
when I use a MemBufInputSource teh Node is null.

I read the contents of the xml file. Then I use it in the contruction of the
MemBufInputSource where it is casted to XMLByte.

Can anyone tell me what I am doing wrong or why it is not working. My xml
file looks like this:

<?xml version='1.0' encoding='ascii'?>
<root>
	<locprop>
		location property 1
		location property 2
		<r_object_id>123456789</r_object_id>
	</locprop>
	<updprop>
		<object_name>
			newname
		</object_name>  
	</updprop>
</root>

and here is my code:

#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include <stdio.h>
#include <afx.h>
#include <parsers/SAXParser.hpp>
#include <framework/MemBufInputSource.hpp>
#include <util/PlatformUtils.hpp> 
#include <parsers/DOMParser.hpp> 
#include <dom/DOM_NodeList.hpp>
#include <dom/DOM_Node.hpp> 
#include <dom/DOM_DOMException.hpp>

int main() {
	
	FILE *stream;
	int numread;
	if ((stream = fopen("c:/myxml.xml","r"))==NULL) {
		printf("\n Can't open file \n");
		return 0;
	}
	char szFileContent[500];
	memset(szFileContent,'\0',sizeof(szFileContent)+1);
	numread = fread(szFileContent,sizeof(char),500,stream);
	fclose(stream);
	 
	char*  gXMLInMemBuf = szFileContent;
	static const char*  gMemBufId = "prodInfo";
	//printf("\n%s\n",gXMLInMemBuf);
	

	try
    {
         XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& toCatch)
    {
         cerr << "Error during initialization! Message:\n"
		
              << /*StrX*/(toCatch.getMessage()) << endl;
         return 0;
    }

	const XMLByte* pXmlByte=NULL;
	pXmlByte = (const XMLByte*) gXMLInMemBuf;
	MemBufInputSource * memBufIS;
	try {
		memBufIS = new MemBufInputSource
		(
			(const XMLByte*)gXMLInMemBuf
			, strlen(gXMLInMemBuf)
			, gMemBufId
			, false
		);
	}	
	catch (const XMLException& E) {
		printf("\nexcpetion\n");
	}
	try {
		DOMParser Parser;
		Parser.parse(*memBufIS);	//option1:use membuf
		//Parser.parse("c:/myxml.xml");	//option2:use file
		DOM_Document DomDoc = Parser.getDocument();
		DOM_NodeList NodeList =
DomDoc.getElementsByTagName("r_object_id"); 
		DOM_Node Node = NodeList.item(0);
		if (Node.isNull())
			return 0;
		unsigned int ln = 0;
		ln = NodeList.getLength();
		short NodeType = 0;
		NodeType = Node.getNodeType();
		DOM_Node TextNode = Node.getFirstChild();
		DOMString dsVal=TextNode.getNodeValue();
		int ln1 = dsVal.length();
		char *szVal = dsVal.transcode();
		cout<<"\n"<<szVal<<"\n";
	}
	catch (const XMLException& E) {
		printf("\nexcpetion\n");
		return 0;
	}
	catch (const DOM_DOMException& e)
    {
      DOMString(e.msg);
	return 0;
    }
	return 1;
}

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org