[xml] Ask a question about xmlDocDumpFormatMemory() funciton.



Hi all,

I have a problem about xmlDocDumpFormatMemory(...) function.,
my libxml2 verison is libxml2-2.6.27.win32 for winxp and libxml2-2.6.27 for Fedora Core5

THE PROBLEM is when I call xmlDocDumpFormatMemory(pdoc, &tmp,&len,0 ) my program works OK,
but if I call xmlDocDumpFormatMemory(pdoc, &tmp,&len, 1),  xml will find unknown node,
Please see code and description below:

---client code on Windows xp-------------------------
    xmlDocPtr pdoc = NULL;
    xmlNodePtr pnode = NULL;
    xmlChar *tmp = NULL;

    pdoc = xmlNewDoc(BAD_CAST  XML_DEFAULT_VERSION);
    if(pdoc == NULL)
          return -1;
 
    /*create root element*/
    pnode = xmlNewDocNode(pdoc, NULL, BAD_CAST "MARequest", NULL);
    if (pnode == NULL)
        goto ERR;

    /* Make ELEMENT the root node of the tree */
    xmlDocSetRootElement(pdoc, pnode);

    /*Add RequestID
    * generate a unique RequsetID
    */
    unsigned char rid[UUID_LEN] = {0};
    if(build_uuid_string(NULL,rid) == -1)
    {
          goto ERR;
    }
    xmlNewTextChild (pnode, NULL, (xmlChar*)"RequestID", (xmlChar*)rid);

    //add Duration
    char strdur[20] = {0};
    sprintf(strdur,"%d",Duration);
    xmlNewTextChild (pnode, NULL, (xmlChar*)"Duration", (xmlChar*)strdur);

    //save XML doc to memory   
    int len = 0;
    xmlDocDumpFormatMemory(pdoc, &tmp,&len, 1);
#ifdef ENABLE_XMAMS_TRACE
    xmlSaveFormatFile("ack.xml", pdoc,1);
#endif
    *msg = (char*)malloc(len*sizeof(char));
    if(*msg == NULL)
        return -1;
    memset(*msg, 0, len*sizeof(char));

    strcpy(*msg,(char*)tmp);
    //this *msg xml string will be transfored to server , code omit..

/*Here a generate a string, for example
If I call
    xmlDocDumpFormatMemory(pdoc, &tmp,&len,1); it generate a xml string like below:

<?xml version="1.0"?>
<MARequest>
  <RequestID>8e830c0f-617a-46e4-b623-43559a438757</RequestID>
  <Duration>4567</Duration>
</MARequest>

and if I call
xmlDocDumpFormatMemory(pdoc, &tmp,&len,0); it will generate a xml string,with no spaces:

<?xml version="1.0"?>
<MARequest><RequestID>706de392-af8c-4cef-b641-5affbf719ec7</RequestID><Duration>4567</Duration></MARequest>

*/

    xmlFree(tmp);
    xmlFreeDoc(pdoc);


---server code on Fedora      ------------------------

    xmlDocPtr pdoc;     //current document
    xmlNodePtr pcur;    //current node
    xmlChar* xmldoc = xmlCharStrdup(req_msg); //msg is from client

   pdoc = xmlParseDoc(xmldoc);

    if(pdoc == NULL)
        return -1;

    pcur = xmlDocGetRootElement(pdoc);
    if (pcur == NULL) {
        goto ERR;
    }

    //get root node "MARequest"
    if (xmlStrcmp(pcur->name, (const xmlChar *) "MARequest")) {
        goto ERR;
    }

    //parse child document
   pcur = pcur->xmlChildrenNode;
    while (pcur != NULL)
    {
        if ((!xmlStrcmp(pcur->name, (const xmlChar *)"RequestID"))) //get RequestID element
        {
            xmlChar* rid = NULL;
            rid = xmlNodeListGetString(pdoc, pcur->xmlChildrenNode,1);
            strcpy(req->rid, (char*)rid);
            xmlFree(rid);
        }else if ((!xmlStrcmp(pcur->name, (const xmlChar *)"Duration")))
        {
            xmlChar *dur;
            dur = xmlNodeListGetString(pdoc, pcur->xmlChildrenNode,1);
            req->Duration = atoi((char*)dur);
            xmlFree(dur);
        }else
        {
            XTRACE(xerror(__FILE__,__LINE__,stdout,"Meet a wrong XML element\n"));
            XTRACE(xerror(__FILE__,__LINE__,stdout,"invalid xml Element %s %s\n",
                            (char*)pcur->name, (char*)pcur->content));
        //  goto ERR;
        }
    pcur = pcur->next;
    }

    return 0;

/*Server program output
 received msg from client,if client software calls
xmlDocDumpFormatMemory(pdoc, &tmp,&len, 1),

MESSAGE received.
<?xml version="1.0"?>
<MARequest>
  <RequestID>8e830c0f-617a-46e4-b623-43559a438757</RequestID>
  <Duration>4567</Duration>
</MARequest>

[X_ERROR] <xaddrmsg.c: 145> Meet a wrong XML element     /*Why happen this errors?,*/
[X_ERROR] <xaddrmsg.c: 147> invalid xml Element text


[X_ERROR] <xaddrmsg.c: 145> Meet a wrong XML element
[X_ERROR] <xaddrmsg.c: 147> invalid xml Element text


But
if client software calls xmlDocDumpFormatMemory(pdoc, &tmp,&len,0),there is NO ERRORs as above, and my program works.
server output is:

MESSAGE received.
<?xml version="1.0"?>
<MARequest><RequestID>706de392-af8c-4cef-b641-5affbf719ec7</RequestID><OperationType>RENT</OperationType><OwnerIP>2001:da8:1001:4::4</OwnerIP><Scope>5</Scope><MAddrType>UNIMCAST</MAddrType><MAddrQuantity>6</MAddrQuantity><StartTime>2007/03/08 15:31:01</StartTime><Duration>4567</Duration></MARequest>

*/

SO MY QUESTION IS:
Does
xmlDocDumpFormatMemory() function have any differences between winxp and linux, while parsing formated xml strings?


Thanks...

    

--
ââââ  â  ä  æ
âçèâ  â  å  è
âäæâ  â  å  ç
ââââ  â  ä  ä
âSEUâ â  é  å
âXu Chunrongâââ

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