XML Namespace removal
std::string namespace_removal(const char* str)
{
std::string xml_str = str;
size_t sz = xml_str.length();
int counter_startbrack = 0;
int counter_endbrack = 0;
int prev_sbrack= 0;
std::cout << "String formatter before str process:" << str <<std::endl;
int sbrack_pos = xml_str.find_first_of('<');
if(sbrack_pos != -1)
counter_startbrack++;
int ebrack_pos = xml_str.find_first_of('>');
if(ebrack_pos != -1)
counter_endbrack++;
int slash = xml_str.find('/');
int prev_ebrack = 0;
for(int i=sbrack_pos; i< sz; )
{
std::cout << "I variable is:"<< i <<std::endl;
/* int colon_pos = xml_str.find(':',i);
if(colon_pos != -1 && sbrack_pos < colon_pos && sbrack_pos < ebrack_pos && colon_pos < ebrack_pos)
{
if( slash == sbrack_pos+1 && xml_str.at(sbrack_pos+1) == '/' )
{
std::cout << "Slash Position:" << xml_str.substr(sbrack_pos, colon_pos- sbrack_pos) << " colon_pos" << colon_pos<< " Slash pos:" << slash << " ebrack_pos" << ebrack_pos << std::endl;
xml_str.erase(sbrack_pos+2,colon_pos-sbrack_pos-1);
// sbrack_pos = xml_str.find('<', sbrack_pos+1);
// ebrack_pos = xml_str.find('>', ebrack_pos+1);
slash = xml_str.find('/',slash + 1);
}
else
{
std::cout << "Entire xml_str is:" <<xml_str << std::endl;
std::cout << "Deletion string:" << xml_str.substr(sbrack_pos, colon_pos- sbrack_pos) << " Start colon:"<<colon_pos << " Start bracket pos:" <<sbrack_pos << " End bracket pos:" <<ebrack_pos << std::endl;
std::cout << " Slash position is:" << slash << xml_str.substr(slash, ebrack_pos - slash) <<std::endl;
int recalculate_sbrack_pos = xml_str.find('<',prev_ebrack+1);
int s_quotation = xml_str.find('\"',i);
int e_quotation = xml_str.find('\"',i);
// if( !(next_slash > sbrack_pos && next_slash < ebrack_pos) ) // check if new slash occurs in between
xml_str.erase(sbrack_pos+1,colon_pos- sbrack_pos);
}
} */
int space_pos = xml_str.find(' ',i);
if(space_pos != -1 && space_pos < ebrack_pos) // optimization check, will not allow to enter 2nd loop if no space
{
for(int j=i; j< ebrack_pos; j++)
{
int space_pos = xml_str.find(' ',j);
if(space_pos != -1 && space_pos < ebrack_pos && space_pos > sbrack_pos && j < ebrack_pos)
{
int second_colon_pos = xml_str.find(':',j);
int quotation_pos = -1,next_quotation_pos = -1;
if(second_colon_pos != -1)
{
// Ignore double quoations "" expression inside attribute
if((quotation_pos= xml_str.find('\"',j)) != -1)
{
if(quotation_pos < second_colon_pos)
{
/* find the next quotation and move the character by that position(j variable) */
std::cout << xml_str.at(quotation_pos) << xml_str.at(quotation_pos+1) << xml_str.at(quotation_pos+2) << std::endl;
next_quotation_pos= xml_str.find('\"',quotation_pos+1);
std::cout << xml_str.at(next_quotation_pos) << xml_str.at(next_quotation_pos+1) << xml_str.at(next_quotation_pos+2) << std::endl;
if(next_quotation_pos != -1 && next_quotation_pos < ebrack_pos && second_colon_pos < next_quotation_pos)
{ j = next_quotation_pos;
space_pos = xml_str.find(' ',j); // update the space position for furthur deletion as we are jumping
/* | */ second_colon_pos = xml_str.find(':',j); // update the colon position for checking below:
/* | */ std::cout << "string to be jumped" << xml_str.substr(quotation_pos, next_quotation_pos - quotation_pos) <<std::endl;
/* | */
/* | */ }
/* | */ }
/* ---->*/ if(space_pos == -1) // check the update colon location
{
//j = ebrack_pos+1;
break;
}
// Deletion of xlns: inside angular brackets started!
if(second_colon_pos != -1 && second_colon_pos > space_pos && second_colon_pos < ebrack_pos)
{
std::cout << "Space Position is:"<< space_pos << "Second colon position is:" <<second_colon_pos << "End brack:"<< ebrack_pos <<std::endl;
std::cout <<"String to be deleted in 2nd level:" << xml_str.substr(space_pos+1, second_colon_pos-space_pos) <<std::endl;
xml_str.erase(space_pos+1,second_colon_pos-space_pos);
}
}
// Ignore double quotations functionality end
}
}
}
}
prev_sbrack = sbrack_pos;
sbrack_pos = getNextCharPos( xml_str,'<',sbrack_pos+1);
if(sbrack_pos != -1 && prev_sbrack != -1)
counter_startbrack++;
if(prev_sbrack != -1 && sbrack_pos == prev_sbrack + 1)
{
std::cout << "xml string is:" <<xml_str<<std::endl;
std::cout << "err string: <<<<<<" << sbrack_pos << prev_sbrack <<std::endl;
return "";
}
prev_ebrack = ebrack_pos;
ebrack_pos = getNextCharPos(xml_str ,'>',ebrack_pos+1);
if(ebrack_pos != -1 && prev_ebrack != -1)
counter_endbrack++;
if(prev_ebrack != -1 && ebrack_pos == prev_ebrack + 1 )
{
std::cout << "err string: >>>>>>" <<std::endl;
return "";
}
i = prev_ebrack;
std::cout<< " Next Start brace and End brace is:" << sbrack_pos << " " << ebrack_pos <<std::endl;
}
/* if(counter_endbrack != counter_startbrack)
{
std::cout << "err string: >< >< position of errors is:" <<sbrack_pos << " "<< ebrack_pos <<std::endl;
std::cout << "err is: " << xml_str.at(sbrack_pos-1) << xml_str.at(sbrack_pos) << xml_str.at(sbrack_pos+1) << " "<< xml_str.at(ebrack_pos-1) << xml_str.at(sbrack_pos) << xml_str.at(sbrack_pos+1) <<std::endl;
std::cout << "err string is:" << xml_str <<std::endl;
return "";
} */
std::cout << "String formatter utility 2:" << xml_str <<std::endl;
return xml_str;
}
std::string remove_namespace(const char* str)
{
std::string xml_str = str;
size_t sz = xml_str.length();
std::cout << "String formatter before str process:" << str <<std::endl;
int sbrack_pos = xml_str.find('<');
for(int i=0; i< sz; )
{
int colon_pos = xml_str.find(':',i);
//std::cout << "Colon Position:" << colon_pos << "Starting bracket Position:" << sbrack_pos <<std::endl;
if(colon_pos != -1 && sbrack_pos < colon_pos)
{
if(xml_str.at(sbrack_pos+1) == '/' )
xml_str.erase(sbrack_pos+2,colon_pos-sbrack_pos-1);
else
xml_str.erase(sbrack_pos+1,colon_pos-sbrack_pos);
}
sbrack_pos = xml_str.find('<',sbrack_pos+1);
i = sbrack_pos;
}
std::cout << "String formatter utility 1:" << xml_str <<std::endl;
return xml_str;
}
Comments
Post a Comment