Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml commands compatibility vc++

Status
Not open for further replies.

DeFre

Programmer
Jun 18, 2003
3
FR
Here is a sample of my program which doesn’t work.



I try to retrieve the rood node through an xml tree.







#include <iostream.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <errno.h>

#include <sys/types.h>

#include <sys/stat.h>





#ifdef CPLUSPLUS



extern &quot;C&quot;

{

#endif

#include <libxml/parser.h>

#include <libxml/tree.h>

#include <libxml/xpath.h>

#include <libxml/xmlmemory.h>

#include <libxslt/xslt.h>



#ifdef CPLUSPLUS

}

#endif







int main(int argc, char **argv) {





/**** affectation des chaines de caractere utilisées dans le fichier xml ***/

/*

const char * morceau = &quot;morceau&quot;;

const char * song = &quot;song&quot;;

const char * first = &quot;first&quot;;

*/



/**** Déclarations des pointeurs ***/



xmlDocPtr xmldoc = NULL;

xmlXPathContextPtr xmldoc_context = NULL;

xmlXPathObjectPtr xmlobject;

xmlNodePtr root;





/**** Vérification de la création de xmldoc et de la valeur 0 pour ce meme ***/

if (!xmldoc)

{

cout <<&quot;là c'est bon\n&quot;;

}



/**** Arborescence xpath on veu récupérer le text sous song ***/

const char path_template[] =&quot;morceau/first/song/text()&quot;;

xmlChar *path;

xmldoc = xmlParseFile(&quot;structsimple.xml&quot;);

if (!xmldoc)

{

cout <<&quot;damn&quot;; //si le parsing est pas bon



}

if (xmldoc)

{

cout <<&quot;doc parse\n&quot;;

}



root = xmlDocGetRootElement(xmldoc);

printf(&quot;Document root element name is %s\n&quot;, root->name);





return 1;



}



structsimple.xml



<?xml version=&quot;1.0&quot;?>

<morceau><first><song n=&quot;001&quot;/>

</first></morceau>



The matter is that after the parsing which seems to work, the xmlGetRootElement such as others xml commands like xmlXPathInit (that’s my problem!)… doesn’t work and make my program crash.



I work on win xp and visual C++.



Can you help me with this one?



De - Fre
 
You don't really care about the result of the function xmlParseFile() - except for cout domn / doc parse.
Are you sure this call is successful ?

Also you ignore the result of xmlDocGetRootElement().
Are you sure this call is successful ?

Which xml-parser do you use ?

/JOlesen
 
he is using libxml

[blue]
#include <libxml/parser.h>

#include <libxml/tree.h>

#include <libxml/xpath.h>

#include <libxml/xmlmemory.h>
[/blue]

-pete
 
You're right I'm not sure the document has been parsed. Do you believe that even with the 2 cout tests it could not be parsed well?

How would you manage this?

De - Fre
 
I dont know the xml-library you use, but I guess from your code.


xmldoc = xmlParseFile(&quot;structsimple.xml&quot;);
if (!xmldoc) {
cout <<&quot;damn&quot;; //si le parsing est pas bon
}
else {
cout <<&quot;doc parsed ok\n&quot;;
root = xmlDocGetRootElement(xmldoc);
if(root) {
printf(&quot;Document root element name is %s\n&quot;, root->name);
}
else {
cout <<&quot;damn - root not found !&quot;;
}
}
}


/JOlesen

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top