[xml] xmlParseMemory() problem

Xu ,

There can be 3 approaches for sending the XML file / contents thru network

1)      FTP the Xml File

2)      Send the XML as a String Buffer using libxml2 APIs

3)      Send a fixed size buffer which reads the Xml file at Source ( line by line or of a buffer size ) in a Loop till EOF is encountered.

Use  the 3rd Approach if Xml file to send is quite  large.


--------------------     Approach 2       ---------------------

In the 2nd approach  at the Sender 1st get xmlDocPtr of the Xml by using xmlParseFile(const char* filename

Then pass it to the API xmlDocDumpFormatMemory(xmlDocPtr ptrXMLDoc, xmlChar** pmemXmlbuff, int* pnBufSize, int nFormat)

Pass nFormat = 1 to get the Indented representation of the XML string.


Say I have the Buffer & Size Vars as ……..

xmlChar *xmlbuff;

int nBuffersize;

I use the above API like

xmlDocDumpFormatMemory(xmlParseFile(“XMLFileName.xml”), &xmlbuff, &nBuffersize,1)



Sender can then  send this data to the client in 2 steps.

First it sends   the  size of the XML bufer ….. Something like……

char dataBuf[1024];

memset( data,0,sizeof(data) );

sprintf( data,"%d", nBuffersize);

send (SOCKET(Windows) or SD(Unix), (const char*) data , sizeof(data), 0 );


Reciever then collects the size of the XML Buffer


recv(SOCKET(Windows) or SD(Unix), (const char*) data , sizeof(data), 0 );

int nBufSize = atoi(data);

Once he has the size he can do

char* pXmlBuf = new char[nBufSize];


Sender now sends the whole XmlBuffer

if ( nBuffersize > 0 )


            send (SOCKET(Windows) or SD(Unix),(const char*)xmlbuff ,nBuffersize, 0 );



Remember also to free the XML buffer at the sender


Reciever now gather this XML buffer…..

recv(SOCKET(Windows) or SD(Unix), pXmlBuf, sizeof(nBufSize), 0 );


Once Reciever has both he can Parse the In-Memory XML buf  using

xmlParseMemory(pXmlBuf , nBufSize).


--------------------     Approach 3       ---------------------

I guess u r using this approach to send a fix size buffer by reading Xml file at source using File API.

U send it in a loop till EOF is encountered.


In the reciever u r using a loop to recv all data & parsing.

In the 1st call xmlParseMemory() works fine , but in 2nd since Xml Data send is not from start but somewhere from middle so

xmlParseMemory() fails

Alternatively u can keep on appending data to a file & later parse that using xmlParseFile() which works allright.












[xml] xmlParseMemory() problem

  • From: "Xu Xiaogang-a19881" <nickxu motorola com>
  • To: <xml gnome org>
  • Subject: [xml] xmlParseMemory() problem
  • Date: Sun, 1 Jun 2008 18:12:34 +0800
size=2 width="100%" align=center>

Hi Experts,

    I am using xmlParseMemory() to parse the content retrieved from the network, which is not well formatted.

    xmlParseMemory() can work for the first call.

    But if I create a loop to keep parsing the content continuously, this function will return NULL from the second call. If  I save the content to a file, and use xmlParseFile() to parse the contents in the same loop. It can work.  The libxml2 lib I use is libxml2-2.6.30 on Window XP.


    I am not sure whether there is more clean work than xmlFreeDoc() when using xmlParseMemory().







This email may contain confidential or privileged information for the intended recipient(s) and the views expressed in the same are not necessarily the views of Zensar Technologies Ltd. If you are not the intended recipient or have received this e-mail by error, its use is strictly prohibited, please delete the e-mail and notify the sender. Zensar Technologies Ltd. does not accept any liability for virus infected mails.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]