K
Klauer
Hello all,
I'm kind of new to working with C++, and I have an issue in solving an
issue that maybe someone out here can help me with.
I have a piece of code in this project that I'm tasked to try to fix
some minor errors. One of these errors is dealing with XML and
loading in an XML file. The XML file is created by a couple different
applications, and one application leaves out a conditional tag:
<?xml version="1.0"?>
<catalog>
<department>
<dept_name>name of department - optional</dept_name>
<program>
<program_name>name of program</program_name>
<course_list>
<course> ... </course>
...
<course> ... </course>
</course_list>
</program>
...
<program>
...
</program>
</department>
...
<department>...</department>
</catalog>
The program that loads this XML works fine for .xml files that contain
that <dept_name> tag, but will fail horribly on files that don't
contain this.
What is the most troublesome is that this package is using TinyXML
(http://code.google.com/p/ticpp/). This wouldn't be a problem, except
that the project uses FirstChildElement("dept_name") and then
NextSiblingElement("program") for a for loop. Now since the
<dept_name> tag is optional, this seems to crash due to a lack of a
sibling in the xml at all:
ticpp::Element *elt3 = child->FirstChildElement("dept_name");
std::string dept = elt3->GetText();
.....
}
.....
Rather than duplicate code that would be in the for loop for the first
<program>, how can I make this portion of the code more robust in
handling an optional <dept_name> tag? I guess I'm asking for help on
how to make a solution that is elegant and not simply a hack to fix a
bug.
What I am considering doing is to do copy and paste of what is
contained in the for loop for the first element. Secondarily, I was
considering taking out the contents of the for loop and putting into
another method that would be called conditionally dependent on the
presence of that <dept_name> tag or not:
if ( dept_name exists)
{
FirstChildElement "dept_name"
}
else
{
FirstChildElement "program"
callContents of ForLoop
}
for ....
{
callContents of ForLoop
}
Neither of my solutions seem elegant or anything that someone would
want to debug later.
Any suggestions?
Apologies in advance for anything lacking in context, completeness, or
whatnot. I assume at this point that what I'm giving is enough to
give a rough idea of where to go. If not, please advise and I'll give
more details, etc.
I'm kind of new to working with C++, and I have an issue in solving an
issue that maybe someone out here can help me with.
I have a piece of code in this project that I'm tasked to try to fix
some minor errors. One of these errors is dealing with XML and
loading in an XML file. The XML file is created by a couple different
applications, and one application leaves out a conditional tag:
<?xml version="1.0"?>
<catalog>
<department>
<dept_name>name of department - optional</dept_name>
<program>
<program_name>name of program</program_name>
<course_list>
<course> ... </course>
...
<course> ... </course>
</course_list>
</program>
...
<program>
...
</program>
</department>
...
<department>...</department>
</catalog>
The program that loads this XML works fine for .xml files that contain
that <dept_name> tag, but will fail horribly on files that don't
contain this.
What is the most troublesome is that this package is using TinyXML
(http://code.google.com/p/ticpp/). This wouldn't be a problem, except
that the project uses FirstChildElement("dept_name") and then
NextSiblingElement("program") for a for loop. Now since the
<dept_name> tag is optional, this seems to crash due to a lack of a
sibling in the xml at all:
ticpp::Element *elt3 = child->FirstChildElement("dept_name");
std::string dept = elt3->GetText();
{for(ticpp::Iterator said:NextSiblingElement("program"); it != it.end(); it++)
.....
}
.....
Rather than duplicate code that would be in the for loop for the first
<program>, how can I make this portion of the code more robust in
handling an optional <dept_name> tag? I guess I'm asking for help on
how to make a solution that is elegant and not simply a hack to fix a
bug.
What I am considering doing is to do copy and paste of what is
contained in the for loop for the first element. Secondarily, I was
considering taking out the contents of the for loop and putting into
another method that would be called conditionally dependent on the
presence of that <dept_name> tag or not:
if ( dept_name exists)
{
FirstChildElement "dept_name"
}
else
{
FirstChildElement "program"
callContents of ForLoop
}
for ....
{
callContents of ForLoop
}
Neither of my solutions seem elegant or anything that someone would
want to debug later.
Any suggestions?
Apologies in advance for anything lacking in context, completeness, or
whatnot. I assume at this point that what I'm giving is enough to
give a rough idea of where to go. If not, please advise and I'll give
more details, etc.