- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
TagsTree ParseXML(const char file_name[])
{
ifstream input_file(file_name, std::ios::in);
string content;
if(!input_file.good())
{
throw "can't open xml";
}
while(!input_file.eof())
{
char buffer[256];
input_file.read(buffer, 256);
streamsize read_count = input_file.gcount();
content.append(buffer, buffer+read_count);
}
input_file.close();
auto Cleanup = [&content](const string& what_to_del) -> void
{
string::size_type pos = content.find(what_to_del);
while(pos != string::npos)
{
content.erase(pos, what_to_del.size());
pos = content.find(what_to_del, pos);
}
};
Cleanup("\n");
Cleanup("\t");
Cleanup(" ");
string::size_type comment_begin = 0;
string::size_type comment_end = 0;
for(;;)
{
string::size_type comment_begin = content.find("<!--", comment_end);
if(comment_begin == string::npos)
{
break;
}
string::size_type comment_end = content.find(">", comment_begin+3);
if(comment_end == string::npos)
{
throw "invalid xml: no comment closing brace";
}
content.erase(comment_begin, comment_end-comment_begin+1);
comment_end = comment_begin;
}
string::size_type header_begin = content.find("<?xml");
if(header_begin == string::npos)
{
throw "invalid xml: no header";
}
string::size_type header_end = content.find(">", header_begin+4);
if(header_end == string::npos)
{
throw "invalid xml: no header closing brace";
}
content.erase(comment_begin, header_end-header_begin+1);
auto CutTagAndContent = [](string& from, string& tag, string& content) -> void
{
string::size_type position = from.find('>');
if(position == string::npos)
{
throw "invalid xml: no tag closing brace";
}
tag = from.substr(1, position-1);
position = from.find("</"+tag+'>', position);
if(position == string::npos)
{
throw "invalid xml: no closing tag";
}
content = from.substr(tag.size()+2, position-tag.size()-2);
from.erase(0, position+tag.size()+3);
};
if(content[0] != '<')
{
throw "invalid xml: to root tag";
}
TagsTree result;
CutTagAndContent(content, result.Node.name, result.Node.content);
TagsTree::children_vectorT children;
children.push_back(&result);
do
{
for(auto i = children.begin(); i!= children.end(); i++)
{
while(!(**i).Node.content.empty())
{
if((**i).Node.content[0]!='<')
{
break;
}
TAG temporary;
CutTagAndContent((**i).Node.content, temporary.name, temporary.content);
(**i).Push(temporary);
}
}
children = EnlistChildren(children);
}
while(!children.empty());
return result;
}
Говнонедопарсер недоговноXML. Дерево тэгов - отдельная кучка.