2011-06-04 7 views
0

我正在使用C++。我有以下XML:反序列化時微小的xml空點引用錯誤?

<data> 
<name> me</name> 
<street /> 
</data> 

我想反序列化這個XML和我所做的:

TiXMlDocument doc, 
tiXmlHandle handle(&doc) 
TiXmlElement* sec; 
sec=handle.FirstChild("data").FirstChild("name").element; 
if (sec) 
{ const char* str=sec->GetText(); 
} 

當我寫:

sec=handle.FirstChild("data").FirstChild("street").element; 
if (sec) 
{ const char* str=sec->GetText(); //here i have a null reference. pointer error. 
} 

我需要一些幫助與空指針在上面的代碼中指出。

我該如何解決這個問題?

+0

是 '秒' 和 '節' 是一回事嗎? – philhobgen 2011-06-04 09:02:44

+2

XML區分大小寫 - '數據'和'數據'是不同的。 – philhobgen 2011-06-04 09:03:07

回答

1

應該這樣:

sec=handle.FirstChild("Data").FirstChild("name").element; 
if (section) 
{ 
    const char* str=section->gettext(); 
} 

不是:

sec=handle.FirstChild("data").FirstChild("name").element; //you are placing element in the variable sec, so now read the text from sec: 
if (sec) 
{ 
    const char* str=sec->gettext(); 
} 
+0

是的。你是對的!我編輯了我的代碼。爲什麼我收到分段錯誤?需要一些幫助! – marryy 2011-06-04 09:21:35

+0

告訴我你是如何改變你的代碼? – 2011-06-04 09:54:22

+0

順便說一句,你有沒有在你的代碼中初始化'doc'變量? – 2011-06-04 09:55:53