2011-04-08 4 views
0

저는 libxml의 초보자이며 이상한 행동을 봅니다 : xmlNode의 내용에 액세스하려고하면 응용 프로그램이 자동 종료됩니다.libxml이 노드의 내용에 액세스하면 응용 프로그램이 종료됩니다.

내 코드 : 내 XML 파일

// 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); 

:이 xmlNode의

<personnage> 

    <nom>Toto</nom> 

</personnage> 

설명 :

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]aturday:~$ 

누군가 나를 도와 줄 수 있습니까?

감사합니다,

데미안 사실

+0

디버깅했는지 여부 – DumbCoder

+0

@DumbCoder 아니요, 아직 해결책을 찾지 못했습니다. Voutur de la balise를 제거하면 "<< (char *) (xpath_objects-> nodesetval-> nodeTab [0] -> content) <<"\ n "; 오류. –

+0

여기서 XML DOM은 다소 비현실적입니다. "Toto"는 "nom"요소의 자식 인 이름없는 텍스트 노드의 내용이므로 nodeTab [0] -> children-> content에 액세스해야합니다. – Luke

답변

0

은 누가가 말한대로, libxml를 들어,하는 XMLNode의 내용은 다른 노드입니다. 그래서 우리는 선택한 노드의 내용을 읽으려면 자식에 액세스해야합니다.

내 경우에는,이 솔루션은 다음과 같습니다

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

당신에게 누가 복음을 감사드립니다.

관련 문제