XML parsing and looping issues

K

Klauer

Hello All,

I thought I posted this, but apparently it did not show up. In any
case, I have an issue that I was hoping someone could help me come up
with an elegant solution for.

I have a project I'm working on in my spare time to take a course
scheduling application written in C++ and try to make it more robust
and conformant to the requirements that it was created for. One of
these issues is reading from an .XML file properly. The requirement
states that a certain tag in a document is optional, but can be used.
There were other applications that create this .xml document that my
project will parse and handle. Some of these .xml files use this
optional tag, other don't. Here's a skeleton layout of the .xml:

<?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 <dept_name> tag is optional. To parse the .xml file, we're using
TinyXML++ (http://code.google.com/p/ticpp/). The project takes this
tag into account when parsing through the document:

for(ticpp::Iterator said:
FirstChildElement("department");
child != child.end(); child++) //child = child-
NextSiblingElement(false))
{
ticpp::Element *elt3 = child->FirstChildElement("dept_name",
false);
std::string dept = elt3->GetText();

for(ticpp::Iterator said:
NextSiblingElement("program"); it != it.end(); it++)
.....
......

While the <dept_name> tag isoptional, because it is specified using
FirstChildElement, calling NextSiblingElement throws an error if
<dept_name> is empty. I want to fix this elegantly and not simply a
hack, since I have the time to do it right, I want to.

I am coming up short on brilliance on how to fix this, and maybe I'm
thinking too deeply about the problem. My only reasonable solution is
to pull out the contents of the for loop into a separate method and
make two separate for loops that are called conditional on the
presense or absense of <dept_name>.

I don't see that being a very elegant solution, and I think it
actually makes the code less manageable. I'm not an expert in C++,
but when I do run this loading of the .xml file, I would like to check
once for the presense of a <dept_name> in the file, and on the first
time that it is not found, to simply omit future checks for the rest
of the file. This file has too many departments to to necessitate
checking for this tag again and again if it can be avoided.

What else can I do to fix this loop and not have it feel like so much
code duplication?

If I'm not clear on all of this, please let me know, and I'll provide
more details, clarity, or whatnot. Thank you.

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top