Problem in reading XML file Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Problem in reading XML file

Post by evstevemd »

Hi friends,
I have problem I cannot spot the problem.
I have XML file reader that I use as setting class. I can write fine but I cannot read! I have followed the code with debugger and I can see each time it gets empty variable (It catches the settings well but cannot get the value)

Anyone to test/help me spot what i'm doing wrong
Thanks

CODE

Code: Select all

//Load Settings
wxString GeneralSettings::loadSetting(wxString& groupName, wxString& settingName) {
	if(!wxFileName::FileExists(confFileName)) {
		//return empty string - THINK OF LOADING DEFAULT SETTINGS
		return wxEmptyString;//Conf file doesn't exists
	}
	wxXmlDocument doc;
	if(!doc.Load(confFileName)) {
		return wxEmptyString;//Failed to load the file
	}
	//That File loaded successfully
	wxXmlNode* childNode = doc.GetRoot()->GetChildren();
	while(childNode) {
		//Search for the group
		if(childNode->GetName()==groupName) {		
			//wxMessageBox(wxT("Group Found: ")+groupName);
			//Found group
			//Now searching for setting
			wxXmlNode* settingNode = childNode->GetChildren();		
			while(settingNode) {
				if(settingNode->GetName()==settingName) {
					//found settings, return that value
					wxString settString = settingNode->GetContent();//HERE VALUE IS EMPTY
					//wxMessageBox(wxT("Setting Found: ")+settString);
					return settString;
				}
				//Search next setting node
				settingNode=settingNode->GetNext();
			}
		}
		//search next node
		childNode=childNode->GetNext();
	}
	//nothing found
	return wxEmptyString;
	
}
<?xml version="1.0" encoding="utf-8"?>
<Settings>
<BookSettings>
<ActiveBook>0</ActiveBook>
<ActiveChapter>0</ActiveChapter>
<ActiveShelf>0</ActiveShelf>
</BookSettings>
</Settings>
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Post by evstevemd »

Any help?
I cannot yet figure out my error!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
utelle
Moderator
Moderator
Posts: 1129
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Post by utelle »

Add the following method to your class and call it for your XML node settingNode:

Code: Select all

wxString GetNodeContent(const wxXmlNode* node)
{
  const wxXmlNode* n = node;
  if (n == NULL) return wxEmptyString;

  n = n->GetChildren();
  while (n)
  {
    if (n->GetType() == wxXML_TEXT_NODE ||
        n->GetType() == wxXML_CDATA_SECTION_NODE)
      return n->GetContent();
    n = n->GetNext();
  }
  return wxEmptyString;
}
This would return the first text node content. In case the content of your node has a more complex form you might need to concatenate the different parts in the while loop.

Example for a more complex content form:

Code: Select all

<settingNode>
First part
<!-- comment -->
Second part
</settingNode>
The above method would only return the string "First part".

Regards,

Ulrich
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Post by evstevemd »

Thanks Urlich,
I would be happy to tell me what I was doing wrong there!
Thanks
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
utelle
Moderator
Moderator
Posts: 1129
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Post by utelle »

evstevemd wrote:I would be happy to tell me what I was doing wrong there!
Well, it depends on the XML node type what method GetContent returns. The XML node settingNode in your example is an element node for which GetContent returns an empty string, but the node has a child node of type text which represents the textual content between the elements <settingNode> and </settingNode> and for such nodes of type text GetContent returns this textual content.

Regards,

Ulrich
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Post by evstevemd »

utelle wrote:
evstevemd wrote:I would be happy to tell me what I was doing wrong there!
Well, it depends on the XML node type what method GetContent returns. The XML node settingNode in your example is an element node for which GetContent returns an empty string, but the node has a child node of type text which represents the textual content between the elements <settingNode> and </settingNode> and for such nodes of type text GetContent returns this textual content.

Regards,

Ulrich
Clear now,
thanks
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
vitavetalwidgets
In need of some credit
In need of some credit
Posts: 1
Joined: Wed Jun 21, 2017 1:09 pm

Re: Problem in reading XML file

Post by vitavetalwidgets »

Because node content is a child for it's node tag element you have to use GetChildren() once more.
For such simple XML Node you can use a simplier method:

if(settingNode->GetName()==settingName) {
wxString settString = settingNode->GetChildren()->GetContent();//MUST BE CORRECT
return settString;
}
Post Reply