Question

insert (2)

Asked by: valleytech

I am implementing alex idea. I am having little bug here. in parseXMLNode, i can't get the return value from readCurrentXmlTag() .

#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
 
#include <iostream>
using namespace std;
 
 
struct TreeNode
{
    std::string key;
    std::vector<TreeNode> children;
};
 
class Tree
{
    // the one and only root node
    TreeNode root;
    ifstream fin; 
    char holder[100];
    string beforePreviousCurrentTag;// help to solve tag case: /branch ->branch->attribute
 
public:  
    bool    ignoreStartTags();
    bool    parseXML();
    void    draw();
 
private:
    void    parseXMLNode(TreeNode &parentNode);
    string  readCurrentXmlTag();
    string  readCurrentXmlKey(string tag); 
    void    drawNode(TreeNode & parentNode, int indent);
};
 
 
string Tree:: readCurrentXmlKey(string tag)
{
    string key;
    char array[100];
    int counter = 0; 
 
    if( tag == "branch" || tag == "leaf")
    {
        counter = 3;
    }
    else if(tag =="attribute") //recusive function : tag is attribute... case
    {
        counter = 2;
    }
 
    if( !fin.eof())
    {
        for(int i = 0; i < counter; i ++)
        {
            if(tag == "attribute" && beforePreviousCurrentTag == "/branch")
            {
                printf("holder is :%s\n",holder);
                strcpy(array,holder); 
                strcpy(holder,"\0"); //reset content
                beforePreviousCurrentTag ="";// reset  
                cout<<"***special case ***"<<endl;
            }
            else
            {
                fin.getline(array,100);
            } 
            cout<<"XmlKey-> read line : "<<array<<endl;
 
            if( i == 0)
            {
                char * pch;
                char delims[]= "=\"" ;           //"<=\">"; //get " must use \" to distingusih between newline and " 
 
                pch = strtok(array,delims);
              //  printf("1st~~~~~%s\n",pch);
                while(pch != NULL)
                {
                    if( strcmp(pch,"latin_name") ==0 ) 
                    {
                        pch = strtok(NULL,delims);
                      //  printf("2nd~~~~~%s\n",pch);
                        pch = strtok(NULL,delims);
                      //  printf("3rd~~~~~%s\n",pch);  
 
                        key = pch;
                        //cout<<Value<<endl;
                        break;
                    }
                    pch = strtok(NULL,delims);
 
                }//end of  strtok while
            }// end of if counter == 0
        }//end of for
        cout<<"key is :"<<key<<endl;
 
    }// end of file    
    return key;
} 
bool Tree::ignoreStartTags()
{
    char array[1000]; 
    fin.open("Treeinput.txt",ios_base::in); 
    if (!fin) 
    {
        cout << "Fail to open file!!!."<< endl;
        return false;
    }
    else
    {
        // just ignore the first 8 lines
        for( int i = 0; i< 8; i++)
        {
            fin.getline(array,100);
        }
        return true;
    } 
} 
 
 
void Tree::drawNode ( TreeNode &parentNode, int indent)
{
    int i=0,j=0;
    indent ++;
     
    for( j =0; j <indent; j++)
    {
        cout<<"+";
    } 
    cout<<"\""<<parentNode.key<<"\""<<"  "<<endl;
    
    if( parentNode.children.size()== 0)
    {
        return;
    }
    else
    {
        for(int i = 0; i <parentNode.children.size();i++)
        {
            drawNode(parentNode.children[i],indent);
        }
    }
}
 
 
void Tree::draw()
{
    int indent = 0;
    cout<<endl<<"start display tree"<<endl;
    drawNode(root,indent);
}
 
bool Tree::parseXML() 
{
    string tag;
   //here in that function we put the loop you now have at begin of parseXMLNode
   //in case of read errors or missin g tags it return false
    
    if( !ignoreStartTags()) return false; 
    tag = readCurrentXmlTag(); 
    // read root key
    string key = readCurrentXmlKey(tag); 
    if( key.empty()) return false;
    root.key =key; 
    // now the root node is complete like any other parent node...
    parseXMLNode(root);
    return true;
} 
string Tree::readCurrentXmlTag()
{
    string Tag;
    char array[100];
    
    if( !fin.eof())
    {
        fin.getline(array,100);
        cout<<endl<<"XmlTag-> read line : "<<array<<endl; 
        char tmp[100];
        strcpy(tmp,array);
 
        char * pch;
        char delims[]= "< >" ;          
 
        pch = strtok(array,delims);
        while(pch != NULL)
        {
            if( strcmp(pch,"branch")==0 || strcmp (pch,"/branch")== 0||
                strcmp(pch,"leaf")== 0 || strcmp (pch,"/leaf")== 0 || strcmp(pch,"attribute") == 0)
            {
              /*
                if(strcmp(pch,"attribute")== 0 && beforePreviousCurrentTag == "/branch" )
                {
                    strcpy(holder,tmp);
                    printf("holder is %s\n",holder);
                }*/ 
                Tag = pch;
                
                break;
            }
            pch = strtok(NULL,delims);
 
        }//end of  strtok while
        
        if(Tag =="/branch")
        {
           // beforePreviousCurrentTag = Tag;
            cout<<"--->Tag is /branch"<<endl;
        } 
        cout<<"before return, Tag : "<<Tag<<endl;
        return Tag;
    }
    else
    {
        fin.close();
        exit(1);
    }  
}
void Tree::parseXMLNode( TreeNode &parentNode)
{
    cout<<"Beginning of parseXMLNode"<<endl; 
    string tag;
    string key;
    TreeNode child; 
    tag = readCurrentXmlTag();
    cout<<"tag is : "<<endl;  //?????????????? 
    while(  tag != "/branch")
    {
        key = readCurrentXmlKey(tag);
    
        child.key = key;
        parentNode.children.push_back(child);
      
        if(tag == "branch")
        {
            TreeNode &childRef = parentNode.children.back(); 
            draw();
            cout<<"childRef key is "<<childRef.key<<endl;
            parseXMLNode(childRef);
        }
        else if( tag =="leaf")
        {
            draw();
            parseXMLNode(parentNode);
            tag == readCurrentXmlTag();
            if(tag == "/leaf")
            {
                continue;
            }
        }
        tag == readCurrentXmlTag();
    }
}
 
int main()
{
    Tree tree;
    tree.parseXML();
   
    tree.draw();
 
    return 1;
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-01 at 23:25:38ID24862961
Topic

C++ Programming Language

Participating Experts
2
Points
500
Comments
55

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. implementing LinkedList
    i have a program with 3 classes - SinglyLinkedList, OrderedLis and ListNode and i'm supposed to implement remove() find() insert() i finished the code for find() but how come i keep getting Exception in thread "main" java.lang.ClassCastException at Item.co...
  2. Bug
    I have a question regarding the Datareader, I retrieve values from the datareader using dr.getvalue(0) [dr being the datareader], as i loop through the while dr.read and end while, the dr skips a value and proceeds to the next one in the table, is this a bug on microsoft or h...
  3. Bug with return value of strlcat()
    I am using strlcat() and strlcpy() in the software we are developing for a project. I adobted the version written by "itsmeandnobodyelse" on Jan. 16 (see enclosed code snippet), since the definition does not come with standard Linux. I had a frustrating day of writi...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Infinity08Posted on 2009-11-01 at 23:31:15ID: 25717551

>> I am implementing alex idea.

Which idea is that ?


>> I am having little bug here.

What kind of bug ? How does it manifest itself ?


>> i can't get the return value from readCurrentXmlTag() .

What do you mean by that ? Could it have anything to do with the exit(1) that is still in that function ?

 

by: itsmeandnobodyelsePosted on 2009-11-02 at 08:34:46ID: 25720850

>>>>     tag = readCurrentXmlTag();
>>>>     cout<<"tag is : "<<endl;  //??????????????
>>>>     while(  tag != "/branch")

Instead do

   while(  (tag = readCurrentXmlTag())  != "/branch")
   

With that the while will read the next tag at begin of each cycle.

>>>> I am implementing alex idea

The q. is a follow up of http:Q_24809171.html.

It is to parse a XML tree into a Tree class. The "idea" was to handle all children of a node (leafs or branches) in a loop rather than to handle siblings with a recursive call. Then the recursive call only happens for a new branch.

 

by: Infinity08Posted on 2009-11-02 at 08:38:20ID: 25720901

Right. Thanks for clarifying, Alex.

 

by: valleytechPosted on 2009-11-02 at 09:05:08ID: 25721198

i changed it to
 while(  (tag = readCurrentXmlTag())  != "/branch")
    {
        cout<<"tag is : "<<endl;  //??????????????

        key = readCurrentXmlKey(tag);
    .........
}

tag is still empty. It is supposed to be "branch".


XmlTag-> read line :   <branch>
before return, Tag : branch
tag is :
XmlKey-> read line :      <attribute name="latin_name" value="Acanthocephala

 

by: itsmeandnobodyelsePosted on 2009-11-02 at 09:16:53ID: 25721306

>>>> tag is still empty. It is supposed to be "branch".

Remember there are initial tags which were not parsed. That's why your old code had a loop

    do
    {
        tag = readCurrentXmlTag();  // <branch> of <leaf>
        cout<<"tag is :"<<tag<<endl<<endl;
 
    }while ( tag.compare("")==0);

which was to ignore all definitions that do not belong to tree.

You can put that loop into a function 'ignoreInitialTags()' and call it once in parseXML. Or - maybe better - simply do

while(  (tag = readCurrentXmlTag())  != "/branch")
    {
        cout<<"tag is : "<<endl;  //??????????????
        if (tag.empty()) continue;   // ignores all tags other than branch, leaf, /branch, /leaf

        key = readCurrentXmlKey(tag);
    .........
}

 

by: valleytechPosted on 2009-11-02 at 21:19:50ID: 25726236

   cout<<"tag is : "<<endl;  //??????????????  
it must be empty because i forgot the tag variable hihi.
 Working on the program now :)

 

by: itsmeandnobodyelsePosted on 2009-11-02 at 23:35:53ID: 25726698

>>>> it must be empty because i forgot the tag variable hihi.
Good catch ;-)

 

by: valleytechPosted on 2009-11-02 at 23:40:51ID: 25726719

please tell me my bugs in this bugs

void Tree::parseXMLNode( TreeNode &parentNode)
{
    cout<<"Beginning of parseXMLNode"<<endl;
    string tag;
    string key;
    TreeNode child;

    while(  (tag = readCurrentXmlTag()) != "/branch")
    {
        cout<<"tag is : "<<tag<<endl;  

        key = readCurrentXmlKey(tag);
   
        if(!key.empty())
        {
            child.key = key;
            parentNode.children.push_back(child);
        }
        if(tag == "branch")
        {
            TreeNode &childRef = parentNode.children.back();

            draw();
            cout<<"childRef key is "<<childRef.key<<endl;
            parseXMLNode(childRef);
        }
        else if( tag =="leaf")
        {
            draw();
            parseXMLNode(parentNode);

            tag == readCurrentXmlTag();
            if(tag == "/leaf")
            {
                continue;
            }
        }
    }
    cout<<"current parent is : "<<parentNode.key<<endl<<endl;
    return;
}
 

 

by: itsmeandnobodyelsePosted on 2009-11-02 at 23:59:44ID: 25726809

>>>>       if(!key.empty())

Better handle the bad case where key is empty

if (key.empty())
{
     // you have two choices :
     //  (A)  ignore the tag with the empty key and goon
     continue;
     //  (B)  show error and either return or exit
     // cout << "empty key after readCurrentXmlKey(" << tag << ") " << endl;
     // exit(1);

}      

>>>> else if( tag =="leaf")
>>>>         {
>>>>             draw();
>>>>             parseXMLNode(parentNode);
>>>> 
>>>>             tag == readCurrentXmlTag();
>>>>             if(tag == "/leaf")
>>>>             {
>>>>                 continue;
>>>>             }
>>>>         }

No, you may not handle a leaf same as a branch. So, DON'T call the parseXMLNode(parentNode);


Instead you read the next tag what is ok. But it MUST be a /leaf. So you have to show an error in case the /leaf is missing and not goon.
 

 

by: valleytechPosted on 2009-11-03 at 00:38:36ID: 25726979

u mean like this

void Tree::parseXMLNode( TreeNode &parentNode)
{
    cout<<"Beginning of parseXMLNode"<<endl;
    string tag;
    string key;
    TreeNode child;

    while(  (tag = readCurrentXmlTag()) != "/branch")
    {
        cout<<"**tag is : "<<tag<<endl;  
        key = readCurrentXmlKey(tag);
   
        if( key.empty())
        {
            cout << "empty key after readCurrentXmlKey(" << tag << ") " << endl;
            exit(1);
        }
       
        child.key = key;
        parentNode.children.push_back(child);
       
        if(tag == "branch")
        {
            TreeNode &childRef = parentNode.children.back();

            draw();
            cout<<"childRef key is "<<childRef.key<<endl;
            parseXMLNode(childRef);
        }
        else if( tag =="leaf")
        {
            draw();
            tag == readCurrentXmlTag();
            if(tag == "/leaf")
            {
                continue;
            }
            else
            {
                cout<<"****tag is :"<<tag<<endl;
                cout<<"Missing /leaf tag"<<endl;
            }
        }
    }
    cout<<"current parent is : "<<parentNode.key<<endl<<endl;
    return;
}

by the way, where are u from Alex?

 

by: itsmeandnobodyelsePosted on 2009-11-03 at 01:24:54ID: 25727211

>>>> u mean like this
Yes, though you need to handle the error, not simply go on. I. e. you best exit the program as there is no real chance to recover from those errors. Same is if you have errors when reading the tags. BUT, consider that EOF is not an error, i. e. after the last /branch the xml file necessarily must close somehow ;-)

>>>> by the way, where are u from Alex?
I am from Tuebingen, Germany.

 

by: valleytechPosted on 2009-11-03 at 01:45:59ID: 25727313

i read your profile, but i can't understand your contact ...

 

by: itsmeandnobodyelsePosted on 2009-11-03 at 01:59:07ID: 25727383

The first part of the contact is info@

Did you compile the code? Any problems?

That thread is one of the last ones I will participate on. But it shouldn't become so long as the last one ;-)

 

by: valleytechPosted on 2009-11-03 at 09:40:12ID: 25731437


when i comment out draw() in parseXMLNode, it can't display the tree after finish insertion. Also, It run infinitely with the real input file. So i am working in those issue.
can i email u for helps after u leave EE? hihi

 

by: itsmeandnobodyelsePosted on 2009-11-03 at 09:52:14ID: 25731555

>>>> can i email u for helps after u leave EE? hihi
Did you already make a test? ;-)

>>>> it can't display the tree after finish insertion. Also, It run infinitely with the real input file.
check your function where you read the file. You must check for eof before checking for fail - I mean to remember that you check for !input_stream - cause eof is also a fail condition. In case of eof you must return without error.

Always look at the call stack when debugging. For each new (sub-)branch in the xml you need one parseXMLNode call. And with the last /branch the *only* remaining parseXMLNode should return in the parseXML function.

 

by: valleytechPosted on 2009-11-03 at 09:56:12ID: 25731605

i haven't tested your email before you grant.

 

by: valleytechPosted on 2009-11-03 at 13:40:07ID: 25734089

i have checked my code. It works on sample input file. However, for the original input file, it can reach to the line number 128423 of 993119 lines. Please help.

#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
 
#include <iostream>
using namespace std;
 
 
struct TreeNode
{
    std::string key;
    std::vector<TreeNode> children;
};
 
class Tree
{
    // the one and only root node
    TreeNode root;
    ifstream fin;
 
public:  
    bool    ignoreStartTags();
    bool    parseXML();
    void    draw();
 
private:
    void    parseXMLNode(TreeNode &parentNode);
    string  readCurrentXmlTag();
    string  readCurrentXmlKey(string tag); 
    void    drawNode(TreeNode & parentNode, int indent);
};
 
 
string Tree:: readCurrentXmlKey(string tag)
{
    string key;
    char array[100];
    int counter = 0; 
    string str;
 
    if( tag == "branch" || tag == "leaf")
    {
        counter = 3;
    }
    else if(tag =="attribute") 
    {
        counter = 2;
    }
    else if( tag == "/leaf")
    {
        counter = 0;
    }
 
    if( !fin.eof())
    {
        for(int i = 0; i < counter; i ++)
        {
            fin.getline(array,100);
            
            str = array;
            if( str.empty())
            {
                cout<<"in break"<<endl;
                break;
            } 
            cout<<"XmlKey-> read line : "<<array<<endl;
 
            if( i == 0)
            {
                char * pch;
                char delims[]= "=\"" ;           //"<=\">"; //get " must use \" to distingusih between newline and " 
 
                pch = strtok(array,delims);
              //  printf("1st~~~~~%s\n",pch);
                while(pch != NULL)
                {
                    if( strcmp(pch,"latin_name") ==0 ) 
                    {
                        pch = strtok(NULL,delims);
                      //  printf("2nd~~~~~%s\n",pch);
                        pch = strtok(NULL,delims);
                      //  printf("3rd~~~~~%s\n",pch);  
 
                        key = pch;
                        //cout<<Value<<endl;
                        break;
                    }
                    pch = strtok(NULL,delims);
 
                }//end of  strtok while
            }// end of if counter == 0
        }//end of for
        cout<<"key is :"<<key<<endl;
 
    }// end of file    
    return key;
} 
bool Tree::ignoreStartTags()
{
    char array[1000]; 
    fin.open("Treeinput.txt",ios_base::in); 
    if (!fin) 
    {
        cout << "Fail to open file!!!."<< endl;
        return false;
    }
    else
    {
        // just ignore the first 8 lines
        for( int i = 0; i< 8; i++)
        {
            fin.getline(array,100);
        }
        return true;
    } 
} 
 
 
void Tree::drawNode ( TreeNode &parentNode, int indent)
{
    int i=0,j=0;
    indent ++;
     
    for( j =0; j <indent; j++)
    {
        cout<<"+";
    } 
    cout<<"\""<<parentNode.key<<"\""<<"  "<<endl;
    
    if( parentNode.children.size()== 0)
    {
        return;
    }
    else
    {
        for(int i = 0; i <parentNode.children.size();i++)
        {
            drawNode(parentNode.children[i],indent);
        }
    }
}
 
 
void Tree::draw()
{
    int indent = 0;
    cout<<endl<<"start display tree"<<endl;
    drawNode(root,indent);
}
 
bool Tree::parseXML() 
{
    string tag;
   //here in that function we put the loop you now have at begin of parseXMLNode
   //in case of read errors or missin g tags it return false
    
    if( !ignoreStartTags()) return false; 
    tag = readCurrentXmlTag(); 
    // read root key
    string key = readCurrentXmlKey(tag); 
    if( key.empty()) return false;
    root.key =key; 
    // now the root node is complete like any other parent node...
    parseXMLNode(root);
    return true;
} 
string Tree::readCurrentXmlTag()
{
    string Tag;
    char array[100];
    string str;
    
    if( !fin.eof())
    {
        fin.getline(array,100);
         
        str = array;
        if( str.empty() || str == "/tree" || str == "  ")
        {
            return Tag;
        } 
        cout<<endl<<"XmlTag-> read line : "<<array<<endl; 
        char tmp[100];
        strcpy(tmp,array);
 
        char * pch;
        char delims[]= "< >" ;          
 
        pch = strtok(array,delims);
        while(pch != NULL)
        {
            if( strcmp(pch,"branch")==0 || strcmp (pch,"/branch")== 0||
                strcmp(pch,"leaf")== 0 || strcmp (pch,"/leaf")== 0 || strcmp(pch,"attribute") == 0)
            {
                Tag = pch;   
                break;
            }
            pch = strtok(NULL,delims);
 
        }//end of  strtok while
        
        if(Tag =="/branch")
        {
           // beforePreviousCurrentTag = Tag;
            cout<<"--->Tag is /branch"<<endl;
        } 
        cout<<"before return, Tag : "<<Tag<<endl;
        return Tag;
    }
    else
    {
        fin.close();
        //exit(1);
        return Tag;
    }  
}
void Tree::parseXMLNode( TreeNode &parentNode)
{
    cout<<"Beginning of parseXMLNode"<<endl;
    string tag;
    string key;
    TreeNode child; 
    while(  (tag = readCurrentXmlTag()) != "/branch")
    {
        if(tag.empty() || tag == "/tree")
        {
            return;
        } 
        cout<<"**tag is : "<<tag<<endl;  
        key = readCurrentXmlKey(tag);
   
        if( key.empty())
        {
            cout << "empty key after readCurrentXmlKey(" << tag << ") " << endl;
            return;
        }
        
        child.key = key;
        parentNode.children.push_back(child);
        
        if(tag == "branch")
        {
            TreeNode &childRef = parentNode.children.back(); 
            //draw();
            cout<<"childRef key is "<<childRef.key<<endl;
            parseXMLNode(childRef);
        }
        else if( tag =="leaf")
        {
            //draw();
            tag == readCurrentXmlTag();
            if(tag == "/leaf")
            {
                continue;
            }
            else
            {
                cout<<"****tag is :"<<tag<<endl;
                cout<<"Missing /leaf tag"<<endl;
            }
        }
    }
    cout<<"current parent is : "<<parentNode.key<<endl<<endl;
    return;
}
 
int main()
{
    Tree tree;
    tree.parseXML();
   
    tree.draw();
 
    return 1;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:

Select allOpen in new window

 

by: itsmeandnobodyelsePosted on 2009-11-03 at 23:18:03ID: 25737108

>>>> It works on sample input file.

Can you post your sample input file again (as a code snippet).

>>>> it can reach to the line number 128423 of 993119 lines.
Is there any special at this line? Or do you have memory problems?

You have a std::vector<TreeNode> children for each node. That probably is some 100 bytes for an average node or more.

Can you check the memory while your prog is running?

 

by: itsmeandnobodyelsePosted on 2009-11-03 at 23:21:54ID: 25737124

I can't run your program with the big inputfile before evening when I am at my appartment. But even than I only have a notebook as I am not at my home office but at my project's site.

 

by: valleytechPosted on 2009-11-03 at 23:29:16ID: 25737144

how can i check the memory ? I attach my sample input file in code snippet
I run it on vmware virtual machine which has 1032 MB

here is line 128423

 <leaf>
              <attribute name="latin_name" value="Platyarthrus haplophthalmoides haplophthalmoides"/>
              <attribute name="common_name" value=""/>
              <attribute name="rank" value="Subspecies"/>
             </leaf>

and here is those lines at the end of the real input file

<leaf>
    <attribute name="latin_name" value="Pentastomida"/>
    <attribute name="common_name" value="pentastomes"/>
    <attribute name="rank" value="Subphylum"/>
   </leaf>
  </branch>
 </branch>
</tree>



 

by: valleytechPosted on 2009-11-03 at 23:39:42ID: 25737182

you can run my program in the evening because it takes a while. I will come back later at Germany night time. Thanks a lot.

 

by: itsmeandnobodyelsePosted on 2009-11-04 at 00:32:36ID: 25737392

>>>> </tree>
Do you already handle taht?


>>>> run it on vmware virtual machine which has 1032 MB
Hmm. I assume you were running a debug version what means double memory for each piece of memory allocated at runtime.

Is it Windows platform? If yes, you should be able to start the taskmanager (CTRL+ALT+DEL or CTRL+ALT-INSERT) which has a performance tab where you could check the memory currently used (and size of swap file).

In any case a xml with 1 million lines probably is too big for your vmware environment. Can you post the full assignment so taht I could check whether the size of the input file is made intentionally so big in order to force your program to use minimal structures, e. g.

struct TreeNode
{
     char* szText;
     TreeNode* rightSibling;
     TreeNode* firstChild;
};

would have less overhead as your current node structure but could used alternatively with little changes.

 

by: itsmeandnobodyelsePosted on 2009-11-04 at 00:42:25ID: 25737427

You could try whether the following program would run:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

int main(int nargs, char* szargs[])
{
    if (nargs != 2)
    {
         std::cout << "usage: " << szargs[0] << " <path_to_xml_file> " << std::endl;
         return 1;
    }
    std::string line;
    std::ifstream ifs(szargs[1]);
    if (!ifs)
    {
         std::cout << "file " << szargs[1] << " couldn't be opened " << std::endl;
         return 2;
    }
    std::vector<std::string> > alllines;
    while (std::getline(ifs, line))
    {
         alllines.push_back(line);      
    }
    std::cout << "# of lines read: " << alllines.size() << std::endl;
    return 0;
}

It only reads the full xml to memory. In case the space of your programs was limited you might get problems here as well, or differently said, if the above wouldn't run there is no chance to process the tree program using your current environment.

 

by: itsmeandnobodyelsePosted on 2009-11-04 at 00:43:30ID: 25737431

>>>> std::vector<std::string> > alllines;
Should be
std::vector<std::string> alllines;

 

by: valleytechPosted on 2009-11-04 at 09:45:59ID: 25741862

here is the output of your program

C:\OPENGL\Debug>testMemory C:\OpenGL\TreeinputBK.txt
# of lines read: 993119

It is windows platform Alex. In my code, I add condition to handle /tree already.

 

by: itsmeandnobodyelsePosted on 2009-11-04 at 15:43:13ID: 25745412

It worked.  Below are the last lines of output:

++++++++"Pseudechiniscus suillus facettalis"
++++++++"Pseudechiniscus suillus suillus"
+++++++"Pseudechiniscus victor"
+++++"Echiniscoididae"
++++++"Echiniscoides"
+++++++"Echiniscoides higginsi"
+++++++"Echiniscoides hoepneri"
+++++++"Echiniscoides pollocki"
+++++++"Echiniscoides sigismundi"
++++++++"Echiniscoides sigismundi galliensis"
++++++++"Echiniscoides sigismundi groenlandicus"
++++++++"Echiniscoides sigismundi hispaniensis"
++++++++"Echiniscoides sigismundi mediterranicus"
++++++++"Echiniscoides sigismundi sigismundi"
+++++"Oreellidae"
++++++"Oreella"
+++++++"Oreella minor"
+++++++"Oreella mollis"
+++++++"Oreella vilucensis"
+++"Mesotardigrada"
++++"Thermozodiidae"
+++++"Thermozodium"
++++++"Thermozodium esakii"
+++"Pentastomida"

 

by: valleytechPosted on 2009-11-04 at 16:07:21ID: 25745556

could you please post the code? how can mine doesn't work?

 

by: itsmeandnobodyelsePosted on 2009-11-04 at 22:35:53ID: 25747185

>>>> could you please post the code?

I made a copy-paste of your last posted code.

>>>> how can mine doesn't work?

I run it on XP with a Core2Duo 2 x 2.1 GHz and 2 GB RAM. It finally needed about 35 MB memory and it lasted more than one hour. I assume your VM could not provide enough resources. Do you run it from the IDE, from command line or from explorer?

What you can do:

- I assume you have a home cumputer not running in a VM. Build the program there and try it. If you don't have a compiler get you the Visual Studio 2008 express edition.

- Remove all cout statements or put them between #ifdef _DEBUG - #endif or replace them by calls to OutputDebugString().

- In the drawNode use a ostringstream instead of cout and write the line to an output file.

- Build a release version of the program and run it from the command line.

 

by: itsmeandnobodyelsePosted on 2009-11-04 at 22:38:51ID: 25747203

BTW, your sample xml is not properly ended. It doesn't close all open branches with </branch> and there is no </tree>.

 

by: valleytechPosted on 2009-11-04 at 22:55:24ID: 25747251

I run it from IDE. Let me make change as your recommendations. I wonder one more thing in my code

 if( str.empty() || str == "/tree" || str == "  ")

I include str == "  " because I see it get "  " during debugging. However, I don't know how it that value. Please explain. Thanks.

 

by: itsmeandnobodyelsePosted on 2009-11-04 at 23:22:55ID: 25747357

>>>> I include str == "  "

Sometimes there are space characters in text files especially when edited manually.

You should add a function trim which removes trailing blanks:

std::string & trim(std::string & s)
{
    int len = (int)s.size();
    while (len > 0 && isspace(s[--len]))
           s.resize(len);
    return s;
}

With that you can use trim(str) instead of str and it would remove the trailing blanks.

   

 

by: valleytechPosted on 2009-11-05 at 00:21:26ID: 25747557

It still gets stuck the line 128423. I run it on  windows vista Pentium Dual Core 2.50Ghz, 3 GB as administrator.

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 00:27:00ID: 25747575

>>>> It still gets stuck the line 128423.
What happens exactly? Any error message? How do you know the line number if you removed all teh cout statements? Try to run it in a dos box (copy the .exe from Release or Debug to the place where the xml files resides).
But increase the buffers of the dos box (5000). That way you'll get your output directly to the console.

 

by: valleytechPosted on 2009-11-05 at 00:38:32ID: 25747622

I compile it and get the exe file. Then I copy both exe file and input file to vista system and run. I am using the Textpad to open the input file with Line Number feature. Output file last line is Platyarthrus haplophthalmoides haplophthalmoides. So i can get line number  is  128423

 

by: valleytechPosted on 2009-11-05 at 00:39:30ID: 25747624

also , no error message

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 00:42:45ID: 25747635

>>>> Output file last line is Platyarthrus haplophthalmoides haplophthalmoides

How to you produce the outputfile?

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 00:46:22ID: 25747646

When running it in the IDE in Debug Mode you could set a break-point after final draw. Then you either should have a debug assertion somehow or it successfully reaches the breakpoint. In teh last case the *additional* console window should show you the last output lines.

 

by: valleytechPosted on 2009-11-05 at 00:49:56ID: 25747656

here is how i make output file

=====
 
void Tree::drawNode ( TreeNode &parentNode, int indent)
{
    int i=0,j=0;
    indent ++;
     
    for( j =0; j <indent; j++)
    {
        cout<<"+";

        fout<<"+";
    }
    cout<<"\""<<parentNode.key<<"\""<<"  "<<endl;

    fout<<"\""<<parentNode.key<<"\""<<"  "<<endl;

    if( parentNode.children.size()== 0)
    {
        return;
    }
    else
    {
        for(int i = 0; i <parentNode.children.size();i++)
        {
            drawNode(parentNode.children[i],indent);
        }
    }
}
 
 
void Tree::draw()
{
    int indent = 0;
    cout<<endl<<"start display tree"<<endl;

    fout.open("Treeoutput.txt", ios::out );

    drawNode(root,indent);

    fout.close();
}

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 01:05:07ID: 25747726

>>>> //draw();

The call of draw in parseXmlNode hopefully was commented ?


Note, your code was not the same than the one I have used. I took it from #25734089 which only outputs to cout.

I would suggest you take that code, build it, copy exe and inputfile to some (new) folder and run it from the console like

   treetest > output.txt

what would put all output into the output.txt.

     


 

by: valleytechPosted on 2009-11-05 at 01:18:41ID: 25747799

i use #25734089 code
>>>> //draw();

The call of draw in parseXmlNode hopefully was commented ?
--> i commented out those draw() in parseXmlNode.
treetest > output.txt  produce same output file. I post the latest code here. You can try it.

#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
 
#include <iostream>
using namespace std;
 
 
struct TreeNode
{
    std::string key;
    std::vector<TreeNode> children;
};
 
class Tree
{
    // the one and only root node
    TreeNode root;
    ifstream fin; 
    ofstream fout;
 
public:  
    bool    ignoreStartTags();
    bool    parseXML();
    void    draw();
 
private:
    void    parseXMLNode(TreeNode &parentNode);
    string  readCurrentXmlTag();
    string  readCurrentXmlKey(string tag); 
    void    drawNode(TreeNode & parentNode, int indent);
    string & trim(std::string & s);
};
 
 
string & Tree :: trim(std::string & s)
{
    int len = (int)s.size();
    while (len > 0 && isspace(s[--len]))
           s.resize(len);
    return s;
} 
string Tree:: readCurrentXmlKey(string tag)
{
    string key;
    char array[100];
    int counter = 0; 
    string str;
 
    if( tag == "branch" || tag == "leaf")
    {
        counter = 3;
    }
    else if(tag =="attribute") 
    {
        counter = 2;
    }
    else if( tag == "/leaf")
    {
        counter = 0;
    }
 
    if( !fin.eof())
    {
        for(int i = 0; i < counter; i ++)
        {
            fin.getline(array,100);
            
            str = array; 
            if( str.empty())
            {
              //  cout<<"in break"<<endl;
                break;
            } 
            //cout<<"XmlKey-> read line : "<<array<<endl;
 
            if( i == 0)
            {
                char * pch;
                char delims[]= "=\"" ;           //"<=\">"; //get " must use \" to distingusih between newline and " 
 
                pch = strtok(array,delims);
              //  printf("1st~~~~~%s\n",pch);
                while(pch != NULL)
                {
                    if( strcmp(pch,"latin_name") ==0 ) 
                    {
                        pch = strtok(NULL,delims);
                      //  printf("2nd~~~~~%s\n",pch);
                        pch = strtok(NULL,delims);
                      //  printf("3rd~~~~~%s\n",pch);  
 
                        key = pch;
                        //cout<<Value<<endl;
                        break;
                    }
                    pch = strtok(NULL,delims);
 
                }//end of  strtok while
            }// end of if counter == 0
        }//end of for
       // cout<<"key is :"<<key<<endl;
 
    }// end of file    
    return key;
} 
bool Tree::ignoreStartTags()
{
    char array[1000]; 
    fin.open("Treeinput.txt",ios_base::in); 
    if (!fin) 
    {
        cout << "Fail to open file!!!."<< endl;
        return false;
    }
    else
    {
        // just ignore the first 8 lines
        for( int i = 0; i< 8; i++)
        {
            fin.getline(array,100);
        }
        return true;
    } 
} 
 
 
void Tree::drawNode ( TreeNode &parentNode, int indent)
{
    int i=0,j=0;
    indent ++;
     
    for( j =0; j <indent; j++)
    {
        cout<<"+"; 
        fout<<"+";
    } 
    cout<<"\""<<parentNode.key<<"\""<<"  "<<endl; 
    fout<<"\""<<parentNode.key<<"\""<<"  "<<endl; 
    if( parentNode.children.size()== 0)
    {
        return;
    }
    else
    {
        for(int i = 0; i <parentNode.children.size();i++)
        {
            drawNode(parentNode.children[i],indent);
        }
    }
}
 
 
void Tree::draw()
{
    int indent = 0;
    cout<<endl<<"start display tree"<<endl; 
    fout.open("Treeoutput.txt", ios::out ); 
    drawNode(root,indent); 
    fout.close();
}
 
bool Tree::parseXML() 
{
    string tag;
   //here in that function we put the loop you now have at begin of parseXMLNode
   //in case of read errors or missin g tags it return false
    
    if( !ignoreStartTags()) return false; 
    tag = readCurrentXmlTag(); 
    // read root key
    string key = readCurrentXmlKey(tag); 
    if( key.empty()) return false;
    root.key =key; 
    // now the root node is complete like any other parent node...
    parseXMLNode(root);
    return true;
} 
string Tree::readCurrentXmlTag()
{
    string Tag;
    char array[100];
    string str;
    
    if( !fin.eof())
    {
        fin.getline(array,100);
         
        str = array; 
        trim(str); 
        if( str.empty() || str == "/tree" || str == "  ")
        {
            return Tag;
        } 
        //cout<<endl<<"XmlTag-> read line : "<<array<<endl; 
        char tmp[100];
        strcpy(tmp,array);
 
        char * pch;
        char delims[]= "< >" ;          
 
        pch = strtok(array,delims);
        while(pch != NULL)
        {
            if( strcmp(pch,"branch")==0 || strcmp (pch,"/branch")== 0||
                strcmp(pch,"leaf")== 0 || strcmp (pch,"/leaf")== 0 || strcmp(pch,"attribute") == 0)
            {
                Tag = pch;   
                break;
            }
            pch = strtok(NULL,delims);
 
        }//end of  strtok while
        
        if(Tag =="/branch")
        {
          
          //  cout<<"--->Tag is /branch"<<endl;
        } 
        //cout<<"before return, Tag : "<<Tag<<endl;
        return Tag;
    }
    else
    {
        fin.close();
        //exit(1);
        return Tag;
    }  
}
void Tree::parseXMLNode( TreeNode &parentNode)
{
   // cout<<"Beginning of parseXMLNode"<<endl;
    string tag;
    string key;
    TreeNode child; 
    while(  (tag = readCurrentXmlTag()) != "/branch")
    {
        if(tag.empty() || tag == "/tree")
        {
            return;
        } 
        //cout<<"**tag is : "<<tag<<endl;  
        key = readCurrentXmlKey(tag);
   
        if( key.empty())
        {
            cout << "empty key after readCurrentXmlKey(" << tag << ") " << endl;
            return;
        }
        
        child.key = key;
        parentNode.children.push_back(child);
        
        if(tag == "branch")
        {
            TreeNode &childRef = parentNode.children.back(); 
            //draw();
            //cout<<"childRef key is "<<childRef.key<<endl;
            parseXMLNode(childRef);
        }
        else if( tag =="leaf")
        {
            //draw();
            tag == readCurrentXmlTag();
            if(tag == "/leaf")
            {
                continue;
            }
            else
            {
               // cout<<"****tag is :"<<tag<<endl;
               // cout<<"Missing /leaf tag"<<endl;
            }
        }
    }
    //cout<<"current parent is : "<<parentNode.key<<endl<<endl;
    return;
}
 
int main()
{
    Tree tree;
    tree.parseXML();
   
    tree.draw(); 
    
 
    return 1;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:

Select allOpen in new window

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 02:07:03ID: 25748001

>>>> treetest > output.txt  produce same output file.

Shouldn't. Firstly all your debug outputs should be included in the output.txt. That probably is more than that of the final draw and might give you a clue why it breaks.

You might download the xml using the link you gave in the previous question. Make a diff to the one you were using. BTW, I hope your program is using 'Multibyte Character Set' and not UNICODE?

 

by: valleytechPosted on 2009-11-05 at 08:55:57ID: 25751466

good morning Alex,
I commented all cout. How so can I have all debug output? Let me download the xml file and try it again.

 

by: valleytechPosted on 2009-11-05 at 09:17:30ID: 25751677

after i removed all commments, I get this info before it stop

XmlTag-> read line :             <branch>
before return, Tag : branch
**tag is : branch
XmlKey-> read line :              <attribute name="latin_name" value="Platyarthrus haplophthalmoides"/>
XmlKey-> read line :              <attribute name="common_name" value=""/>
XmlKey-> read line :              <attribute name="rank" value="Species"/>
key is :Platyarthrus haplophthalmoides
childRef key is Platyarthrus haplophthalmoides

XmlTag-> read line :              <leaf>
before return, Tag : leaf
**tag is : leaf
XmlKey-> read line :               <attribute name="latin_name" value="Platyarthrus haplophthalmoides haplophthalmoides"
in break
key is :Platyarthrus haplophthalmoides haplophthalmoides

start display tree
...
   
which trace to
        if( str.empty())
            {
                cout<<"in break"<<endl;
                break;
            }

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 09:34:25ID: 25751852

>>>> How so can I have all debug output?
First goal is to find out why your code doesn't work and mine worked.

So, take the code from #25734089 and the xml file from the link. That is what I used.

Run it from the command line so that all output was wriiten to text file.

 

by: valleytechPosted on 2009-11-05 at 09:42:49ID: 25751920

after i removed all commments, I get this info before it stop

XmlTag-> read line :             <branch>
before return, Tag : branch
**tag is : branch
XmlKey-> read line :              <attribute name="latin_name" value="Platyarthrus haplophthalmoides"/>
XmlKey-> read line :              <attribute name="common_name" value=""/>
XmlKey-> read line :              <attribute name="rank" value="Species"/>
key is :Platyarthrus haplophthalmoides
childRef key is Platyarthrus haplophthalmoides

XmlTag-> read line :              <leaf>
before return, Tag : leaf
**tag is : leaf
XmlKey-> read line :               <attribute name="latin_name" value="Platyarthrus haplophthalmoides haplophthalmoides"
in break
key is :Platyarthrus haplophthalmoides haplophthalmoides

start display tree
...
   
which trace to
        if( str.empty())
            {
                cout<<"in break"<<endl;
                break;
            }

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 09:49:34ID: 25751983

Hmmm.

Replace

>>>>            fin.getline(array,100);
>>>>            str = array;

by

      getline(fin, str);

I used an elder compiler which may not complain if the inputline was longer than 100. But maybe your compiler was not so *gentle* and may have returned an empty string.

 

by: valleytechPosted on 2009-11-05 at 10:39:47ID: 25752473

Replace

>>>>            fin.getline(array,100);
>>>>            str = array;

by

      getline(fin, str);

gives output below:
XmlTag-> read line :  <branch>
before return, Tag : branch
XmlKey-> read line : ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ
XmlKey-> read line : ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ
XmlKey-> read line : ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ
key is :

start display tree
+""  

 

by: valleytechPosted on 2009-11-05 at 11:21:23ID: 25752903

i think i found solution. let me double check first hihi

 

by: valleytechPosted on 2009-11-05 at 11:47:06ID: 25753157

i got it.Can you guess how? Can i ask one more general question about this program before i close it :)

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 12:12:02ID: 25753424

>>>> i got it.Can you guess how?

You increased the array[100] by array[1000] ?

 

by: valleytechPosted on 2009-11-05 at 12:15:43ID: 25753453

you are right :) I increase array[100] to array [200]

 

by: valleytechPosted on 2009-11-05 at 12:39:07ID: 25753748

N1
   N11
       L111
       L112
       L113
   N12
   N13
       N131
          L1311
       N132
          L1321


. By using your idea,no matter how many leafs  such as L111, L112, L113... recursive function will return N11 address because those leafs are taken care of a loop.
  By using Ike idea, let say we have 3 leafs L111, L112,L113.
             After it done L113, recursive function return N11 address
            After it done L112, recursive function return N11 address
           After it done L111, recursive function return N11 address

Am i right? Thanks a lot.

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 22:15:16ID: 25756957

>>>>  By using Ike idea, let say we have 3 leafs L111, L112,L113.

After detetecting /leaf L113 recursive function returns. Parent is N11.
Then L112 recursive function returns as well (after having called recursive leaf function for L113). Parent is N11.
Then L111 recursive function returns as well (after having called recursive leaf function for L112). Parent is N11.
Then N11 recursive function returns as well  (after having called recursive leaf function for L112). Parent is N1.
Then new branch will be detected in N11 and recursive call for N12. Parent is N1.

 

by: itsmeandnobodyelsePosted on 2009-11-05 at 23:25:37ID: 25757278

Following Ike's idea you have two parsing functions, parseXMLNode and parseXMLLeaf.

The parseXMLNode

   - checks for branch tag at the beginning what is a sub branch and calls recursively parseXMLNode
   - checks for leaf tag at the beginning what is a leaf and calls parseLeafNode
   - both calls will return some time
   - now it gets difficult!
   - next tag must be /branch what is the end of the own (current) branch
   - checks for branch tag which would be a sibling and calls recursively parseXMLNode (same parent)
   - return

The parseXMLLeaf

   - next tag must be /leaf what is the end of the own (current) leaf
   - checks for leaf tag which would be a sibling and calls recursively parseXMLLeaf (same parent)
   - return

You could also do with one function but it makes the whole thing more difficult.

The main point in parseXMLNode following Ike's way is to have two recursive kind of calls, one for going down and one for going same level.

The idea here was to do all same level nodes/leafs in a loop what is much simpler.
   

 

by: valleytechPosted on 2009-11-05 at 23:38:22ID: 25757329

I can see the difference between two approaches now.Thanks a lot for your help. I just have a completely new question for this tree, but wonder whether to post it.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...