Tuesday, October 27, 2015

XML or .osm special character issue and solution in C


You probably have experience while parsing openstreetmap .osm file - some name [TAG-NAME] appear as

C&C - which is in clear text "C&C". So while you are coding for a .osm or XML parser, it is important to consider such special character at XML specification.

If it need to present such value at literal form, it is important to replace those characters with some other clear text format.

For example, if it need to replace the above "&" a below C program can make an idea.

 char *special_ch_replace   
 (char *s, char ser, char rep)   
 {  
  char *p1;  
  for (p1=s; *p1; p1++)   
  {  
   if (*p1 == ser)  
      {  
    *p1 = rep;  
   }  
  }  
  return s;  
 }  



The program will point the value [TAG-NAME] in *s. Using *p1, it will check every character of that string and iterate until NULL character.
If found anything as given "ser" will replace by given "rep". And finally return the string pointer.

The function can be called as

 char *object_name_tmp;  
 object_name_tmp = special_ch_replace (nodeid, '&', 'u');  
 object_name_tmp = special_ch_replace (object_name_tmp, '"', '-');  

No comments:

Post a Comment