Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. XML-safe strings, and iostreams

XML-safe strings, and iostreams

Scheduled Pinned Locked Moved C / C++ / MFC
c++xmlquestionannouncement
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Simon Steele
    wrote on last edited by
    #1

    Hi, 1. Surely somebody here has written a really efficient and fast function which will replace special characters in a string with their XML entity equivalents? Every programmer using C++ and XML must surely have had to write this by now? I've just knocked a quick one up here: //typedef basic_string tstring; void XMLSafeString(tstring& str) { int len = str.size(); TCHAR * buffer = new TCHAR[len+1]; _tcscpy(buffer, str.c_str()); // make an attempt at reducing re-allocs... str.reserve(len + 20); str = _T(""); for(int i = 0; i < len; i++) { switch(buffer[i]) { case _T('"'): str += """; break; case _T('<'): str += "<"; break; case _T('>'): str += ">"; break; case _T('&'): str += "&"; break; case _T('\''): str += "'"; break; default: str += i; } } delete [] buffer; } but I'm sure there must be better ways. Also, I would like to see an efficient solution that doesn't modify the incoming string, but returns a tstring. The reason for this is: 2. I want to write out XML using the ofstream class. Unfortunately, I can't use the function I've got above without permanently modifying my input strings - which is a no-no. Therefore, I will have to copy every string into another object, and pass the xmlsafe-ised version of that string into the ofstream. This kind of reduces the simplicity of iostreams. Does anyone have anything clever like a modifier which enables XML character escaping? Thanks in advance for any suggestions, code, etc... Oh, and a quick disclaimer: I haven't tested the above code yet. -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

    P S 2 Replies Last reply
    0
    • S Simon Steele

      Hi, 1. Surely somebody here has written a really efficient and fast function which will replace special characters in a string with their XML entity equivalents? Every programmer using C++ and XML must surely have had to write this by now? I've just knocked a quick one up here: //typedef basic_string tstring; void XMLSafeString(tstring& str) { int len = str.size(); TCHAR * buffer = new TCHAR[len+1]; _tcscpy(buffer, str.c_str()); // make an attempt at reducing re-allocs... str.reserve(len + 20); str = _T(""); for(int i = 0; i < len; i++) { switch(buffer[i]) { case _T('"'): str += """; break; case _T('<'): str += "<"; break; case _T('>'): str += ">"; break; case _T('&'): str += "&"; break; case _T('\''): str += "'"; break; default: str += i; } } delete [] buffer; } but I'm sure there must be better ways. Also, I would like to see an efficient solution that doesn't modify the incoming string, but returns a tstring. The reason for this is: 2. I want to write out XML using the ofstream class. Unfortunately, I can't use the function I've got above without permanently modifying my input strings - which is a no-no. Therefore, I will have to copy every string into another object, and pass the xmlsafe-ised version of that string into the ofstream. This kind of reduces the simplicity of iostreams. Does anyone have anything clever like a modifier which enables XML character escaping? Thanks in advance for any suggestions, code, etc... Oh, and a quick disclaimer: I haven't tested the above code yet. -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

      P Offline
      P Offline
      palbano
      wrote on last edited by
      #2

      Sounds like great work for regular expressions no?

      "No matter where you go, there your are..." - Buckaoo Banzi

      -pete

      S 1 Reply Last reply
      0
      • P palbano

        Sounds like great work for regular expressions no?

        "No matter where you go, there your are..." - Buckaoo Banzi

        -pete

        S Offline
        S Offline
        Simon Steele
        wrote on last edited by
        #3

        Possibly, but will this be faster than my brute force replacer? Also, it involves the inclusion of a regular expression library. I'm not against the idea, but it has drawbacks. Also, would I have to use x different regexs where each x represents one transformation: " -> " ' -> ' ... or is there a way I can do all of these with one regex? Thanks for your help, -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

        1 Reply Last reply
        0
        • S Simon Steele

          Hi, 1. Surely somebody here has written a really efficient and fast function which will replace special characters in a string with their XML entity equivalents? Every programmer using C++ and XML must surely have had to write this by now? I've just knocked a quick one up here: //typedef basic_string tstring; void XMLSafeString(tstring& str) { int len = str.size(); TCHAR * buffer = new TCHAR[len+1]; _tcscpy(buffer, str.c_str()); // make an attempt at reducing re-allocs... str.reserve(len + 20); str = _T(""); for(int i = 0; i < len; i++) { switch(buffer[i]) { case _T('"'): str += """; break; case _T('<'): str += "<"; break; case _T('>'): str += ">"; break; case _T('&'): str += "&"; break; case _T('\''): str += "'"; break; default: str += i; } } delete [] buffer; } but I'm sure there must be better ways. Also, I would like to see an efficient solution that doesn't modify the incoming string, but returns a tstring. The reason for this is: 2. I want to write out XML using the ofstream class. Unfortunately, I can't use the function I've got above without permanently modifying my input strings - which is a no-no. Therefore, I will have to copy every string into another object, and pass the xmlsafe-ised version of that string into the ofstream. This kind of reduces the simplicity of iostreams. Does anyone have anything clever like a modifier which enables XML character escaping? Thanks in advance for any suggestions, code, etc... Oh, and a quick disclaimer: I haven't tested the above code yet. -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

          S Offline
          S Offline
          Simon Steele
          wrote on last edited by
          #4

          Firstly, the above code contains a bug: str += i; should be: str += buffer[i]; Also, I have found a simple way of using the above function with iostreams. It works like this:struct FormatXML { tstring str_; explicit FormatXML(const tstring& str) : str_(str) { XMLSafeString(str_); } friend std::ostream& operator<<(std::ostream& s, const FormatXML& x) { s << x.str_; return s; } };
          This copies the input string, and performs the XML safety check on construction. The safe string is then passed into the stream. It would be used like this: tstring str = _T("My string with \"Quotes\"."); myostream << FormatXML(str); This isn't very efficient, however. :(( Therefore, I'm still on the lookout for cool code. Let me know! -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups