2011-04-08 59 views
0

我是一名libxml初学者,遇到一个奇怪的行为: 当我尝试访问和xmlNode的内容时,应用程序以无提示方式退出。libxml访问节点导致应用程序退出的内容

我的代码:

// Initialisation des pointeurs 
xmlDocPtr doc; 
xmlXPathContextPtr xpath_context; 
xmlXPathObjectPtr xpath_objects; 

// Chargement du document et création du contexte pour xpath 
doc = xmlParseFile(nom.c_str()); 
xpath_context = xmlXPathNewContext(doc); 

// Recherche via xpath 
xpath_objects = xmlXPathEvalExpression((xmlChar*)("//personnage/nom"), xpath_context); 
if(xpath_objects == NULL) 
    cout << "La balise nom est obligatoire !\n"; 

// Affichage des résultats 
cout << "Nom de la balise : " << xpath_objects->nodesetval->nodeTab[0]->name << "\n"; 
cout << "Valeur de la balise : " << (char*)(xpath_objects->nodesetval->nodeTab[0]->content) << "\n"; 
cout << "Fin\n"; 

// Libération de la mémoire 
xmlXPathFreeObject(xpath_objects); 
xmlXPathFreeContext(xpath_context); 
xmlFreeDoc(doc); 

我的XML文件:

<personnage> 

    <nom>Toto</nom> 

</personnage> 

的xmlNode说明:

Structure xmlNode 
struct _xmlNode { 
    void * _private : application data 
    xmlElementType type : type number, must be second ! 
    const xmlChar * name : the name of the node, or the entity 
    struct _xmlNode * children : parent->childs link 
    struct _xmlNode * last : last child link 
    struct _xmlNode * parent : child->parent link 
    struct _xmlNode * next : next sibling link 
    struct _xmlNode * prev : previous sibling link 
    struct _xmlDoc * doc : the containing document End of common p 
    xmlNs * ns : pointer to the associated namespace 
    xmlChar * content : the content 
    struct _xmlAttr * properties : properties list 
    xmlNs * nsDef : namespace definitions on this node 
    void * psvi : for type/PSVI informations 
    unsigned short line : line number 
    unsigned short extra : extra data for XPath/XSLT 
} 

完整文档,请点击这里:http://xmlsoft.org/html/libxml-tree.html#xmlNode

这是输出:

Nom de la balise : nom 
Valeur de la balise : [email protected]:~$ 

有人可以帮我吗?

感谢,

达明

+0

调试过吗? – DumbCoder 2011-04-08 14:58:45

+0

@DumbCoder不,我还没有找到解决方案。如果我删除了'cout <<“Valeur de la balise:<<(char *)(xpath_objects-> nodesetval-> nodeTab [0] - > content)<<”\ n“;'应用程序不会退出错误。 – 2011-04-08 15:01:33

+0

XML DOM在这里有点不直观。您需要访问nodeTab [0] - > children-> content,因为“Toto”是作为“nom”元素的子节点的未命名文本节点的内容。 – Luke 2011-04-08 16:18:28

回答

0

事实上,卢克说,对libxml的,XMLNode的内容是另一个节点。 因此,我们必须访问孩子阅读所选节点的内容。

在我的情况下,解决办法是:

cout << "Valeur de la balise : " << (char*)(xpath_objects->nodesetval->nodeTab[0]->children->content) << "\n"; 

谢谢卢克。