473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Conditional For issues

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_nametag, 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_nametag 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<ticpp::Elementit = elt3-
>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_nametag? 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_nametag 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.

Jul 18 '07 #1
1 1568
Klauer wrote:
[..] the project uses FirstChildElement("dept_name") and then
NextSiblingElement("program") for a for loop. Now since the
<dept_nametag 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<ticpp::Elementit = elt3-
>NextSiblingElement("program"); it != it.end(); it++)
^^^^^^^^^^^^^^
This doesn't look right, but I'll take your word for it
{
.....
}
.....
[..]
Your question has really NOTHING to do with C++ language. Please
consider posting to 'comp.programming' next time. For now, it seems
that this

ticpp::Element *elt3 = child->FirstChildElement("dept_name");
std::string dept; // default - no dept name
if (elt3) { // the child 'dept_name' is found
dept = elt3->GetText();
elt3->NextSiblingElement("program");
}
else { // 'dept_name' is NOT found
elt3 = child->FirstChildElement("program");
if (elt3)
... service the first found 'program'
}

if (elt3) {
// same for loop here...
}

should get it closer to what you want. Since I don't know what
other functionality is available, I cannot rewrite it for you to
make it less ugly.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Winter | last post by:
(Sorry - couldn't find a generic DocBook NG - I hope this is close enough.) I'm considering moving my documentation and translation business - which is currently done in proprietary formats such...
4
by: CGuy | last post by:
Hi, I have an ASPX page which has a datagrid and this datagrid is bound to a Custom Collection. sample code this.DataGrid1.DataSource = UserManager.Users; this.DataGrid1.DataBind();
28
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found...
8
by: Drew | last post by:
I am building an application for keeping track of user permissions here at work. I have built the interfaces, and am now working on the processing page for inserting to the database. I am having...
2
by: Megan | last post by:
Can you write conditional VBA code that affects only one or two records on a continuous subform? I have a form with a subform on it. The parent/ child field that links the forms is CaseID. The...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
10
by: John Smith | last post by:
After reading C# documentation the Conditional attribute seemed the way to go, but after inspecting the IL it seems those methods are still there and I imagine the CLR removes them. Using #if DEBUG...
15
by: Dan Henry | last post by:
I have run across functions in the Linux kernel's MTD driver that have me scratching my head a bit. The functions have the general form: extern int bar(size_t len, size_t *retlen, unsigned char...
3
by: WU10 | last post by:
I have built a simple database to track the mechanical condition of offshore equipment. We established a system of ranking for issues that arise and used conditional formatting of the cell's...
2
by: robertng90025 | last post by:
I'm having a problem with MS Access 2003 and its conditional formatting. I have textboxes on a continuous form whose left and right margins are set to 0.03 inches. Based on each textbox's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.