Conditional For issues

Discussion in 'C++' started by Klauer, Jul 18, 2007.

  1. Klauer

    Klauer Guest

    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();

    for(ticpp::Iterator<ticpp::Element> it = 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_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.
     
    Klauer, Jul 18, 2007
    #1
    1. Advertising

  2. Klauer wrote:
    > [..] 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<ticpp::Element> it = 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
     
    Victor Bazarov, Jul 18, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris P
    Replies:
    0
    Views:
    448
    Chris P
    Oct 28, 2003
  2. Willem Oosthuizen

    Conditional signal declaration

    Willem Oosthuizen, Jul 16, 2003, in forum: VHDL
    Replies:
    5
    Views:
    7,029
    Mario Trams
    Jul 17, 2003
  3. itsme
    Replies:
    1
    Views:
    1,689
    Ralf Hildebrandt
    Jul 23, 2003
  4. Anand P Paralkar
    Replies:
    2
    Views:
    9,542
    Srinivasan Venkataramanan
    Aug 4, 2003
  5. Alec S.
    Replies:
    10
    Views:
    10,305
    Alec S.
    Apr 16, 2005
Loading...

Share This Page