Converting specical characters in XML stream

I have an XML stream which contains special characters such ' stored in a CString object. Is there any method other than replacing individual characters in the stream to convert these special characters ?

Answers


The only characters that need escaping are " < > &.

But I would recommend the use a standard XML library. That would not only take care of escaping, but a lot of other problems (encoding, entities, validation, etc.)


I frankly don't see another option.


If you can, install a filter in the writer. This allows you to write the stream char by char and replace the special characters when you encounter them (saving you from having to allocate a second string object).

Try to output as many characters of the string at once as possible because calling write() in a loop for every single character is expensive. Instead use this pseudo code:

int start = 0;
for (int i=0; i<str.length(); i++) {
    char c = str.getChar(i);
    String emit = null;
    switch (c) {
    case '<': emit = "&lt;"; break;
    case '>': emit = "&gt;"; break;
    case '\'': emit = "&apos;"; break;
    case '"': emit = "&quot;"; break;
    }
    if (emit != null) {
        write(str,start,i);
        start = i;
        write(emit);
    }
}
if (start != str.length()) {
    write(str);
}

In the common case, the loop will traverse the string once (which is fast) and call write() once.

If you can't install a filter in the writer, you can use the same code to filter the string. In the common case, you will simply return the input string. In the if (emit != null), you can then allocate a new copy. But since this will only happen if there are special characters in the string, this is much cheaper than creating a copy for every string.


Need Your Help

How to overcome “No repositories found” error in IntelliJ

maven intellij-idea nexus

I'm getting "No repository found" errors in IntelliJ not only for our own repository, but even for the majority of the repositories listed by default under Maven configuration. Only jfrog.org and

How does one parse simple inline markup (i.e. *bold*), in Python?

python parsing wikitext creole

How does one implement a parser (in Python) for a subset of wikitext that modifies text, namely:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.