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]:~$
有人可以幫我嗎?
感謝,
達明
調試過嗎? – DumbCoder 2011-04-08 14:58:45
@DumbCoder不,我還沒有找到解決方案。如果我刪除了'cout <<「Valeur de la balise:<<(char *)(xpath_objects-> nodesetval-> nodeTab [0] - > content)<<」\ n「;'應用程序不會退出錯誤。 – 2011-04-08 15:01:33
XML DOM在這裏有點不直觀。您需要訪問nodeTab [0] - > children-> content,因爲「Toto」是作爲「nom」元素的子節點的未命名文本節點的內容。 – Luke 2011-04-08 16:18:28