HaniDaher
asked on
load XML file iteration
Hi,
I have an xml File (see attached file)
This file is composed of Blocks, Lines, words, characters:
I am trying to create objects as follows:
Thank you.
00000012-1-R.xml
I have an xml File (see attached file)
This file is composed of Blocks, Lines, words, characters:
Every Block is composed of 1,...,n Lines
Every line is composed of 1,...,k words
Every word is composed of 1,...,l characters
I am trying to create objects as follows:
Block(Int top, Int left, Int bottom, int right, vector<Lines>)
Line(Int top, Int left, Int bottom, int right, vector<words>)
Word(Int top, Int left, Int bottom, int right, vector<characters>)
I am using TinyXML on C++, but i can't link them together, My code can take one object( block,line,word,character) at a time.
void Keywords::checkChild(TiXmlElement *child)
{
if(child)
{
if((string)child->Value() == "block")
{
cout << child->Value()<<endl;
double x1 = atoi(child->Attribute("left"));
double y1 = atoi(child->Attribute("top"));
double x2 = atoi(child->Attribute("right"));
double y2= atoi(child->Attribute("bottom"));
//Vector<Line>lineList
// blockList.push_back(newBlock(y1,x1,y2,x2,lineList));
}
checkChild(child->FirstChildElement());
checkChild(child->NextSiblingElement());
}///end if child
}
Thank you.
00000012-1-R.xml
ASKER
Yes Tommy that's what i thought. I actually managed to find the following solution:
I think it is basically the same idea as yours.
What do you think about the above code?
void parseFile(TiXmlElement* document, vector<Block*>& blocks)
{
for (TiXmlElement* sub = document->GetFirstChildElement("block"); sub; sub = sub->GetNextSiblingElement("block"))
blocks.push_back(parseBlock(sub));
}
Block* parseBlock(TiXmlElement* element)
{
double x1 = atof(element->Attribute("left"));
// ...
vector<Line*> lines;
for (TiXmlElement* sub = element->GetFirstChildElement("line"); sub; sub = sub->GetNextSiblingElement("line"))
lines.push_back(parseLine(sub));
return new Block(x1, ..., lines);
}
Line* parseLine(TiXmlElement* element)
{
double x1 = atof(element->Attribute("left"));
// ...
vector<Word*> words;
for (TiXmlElement* sub = element->GetFirstChildElement("word"); sub; sub = sub->GetNextSiblingElement("word"))
words.push_back(parseWord(sub));
return new Line(x1, ..., words);
}
Word* parseWord(TiXmlElement* element)
{
double x1 = atof(element->Attribute("left"));
// ...
vector<Char*> chars;
for (TiXmlElement* sub = element->GetFirstChildElement("char"); sub; sub = sub->GetNextSiblingElement("char"))
chars.push_back(parseChar(sub));
return new Word(x1, ..., chars);
}
Char* parseChar(TiXmlElement* element)
{
double x1 = atof(element->Attribute("left"));
// ...
return new Char(x1, ...);
}
I think it is basically the same idea as yours.
What do you think about the above code?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Something like
Open in new window