Example #1
0
/**
 * The same as the version above, but writes to a file instead...yes, this could be made
 * with streams and only one code set but streams are slow...the file f should be a buffered
 * file, otherwise things will be very slow (I assume write is not expensive and call it a lot
 */
void SimpleXML::Tag::toXML(int indent, OutputStream* f) {
    if(children.empty() && data.empty()) {
        string tmp;
        tmp.reserve(indent + name.length() + 30);
        tmp.append(indent, '\t');
        tmp.append(1, '<');
        tmp.append(name);
        tmp.append(1, ' ');
        appendAttribString(tmp);
        tmp.append("/>\r\n", 4);
        f->write(tmp);
    } else {
        string tmp;
        tmp.append(indent, '\t');
        tmp.append(1, '<');
        tmp.append(name);
        tmp.append(1, ' ');
        appendAttribString(tmp);
        if(children.empty()) {
            tmp.append(1, '>');
            if(needsEscape(data, false)) {
                string tmp2(data);
                escape(tmp2, false);
                tmp.append(tmp2);
            } else {
                tmp.append(data);
            }
        } else {
            tmp.append(">\r\n", 3);
            f->write(tmp);
            tmp.clear();
            for(Iter i = children.begin(); i!=children.end(); ++i) {
                (*i)->toXML(indent + 1, f);
            }
            tmp.append(indent, '\t');
        }
        tmp.append("</", 2);
        tmp.append(name);
        tmp.append(">\r\n", 3);
        f->write(tmp);
    }
}
Example #2
0
/**
 * The same as the version above, but writes to a file instead...yes, this could be made
 * with streams and only one code set but streams are slow...the file f should be a buffered
 * file, otherwise things will be very slow (I assume write is not expensive and call it a lot
 */
void SimpleXML::Tag::toXML(int indent, OutputStream* f, bool /*noIndent*/ /*false*/) {
	if(children.empty() && data.empty() && !forceEndTag) {
		string tmp;
		tmp.reserve(indent + name.length() + 30);
		tmp.append(indent, '\t');
		tmp.append(1, '<');
		tmp.append(name);
		tmp.append(1, ' ');
		appendAttribString(tmp);
		tmp.append("/>\r\n", 4);
		f->write(tmp);
	} else {
		string tmp;
		tmp.append(indent, '\t');
		tmp.append(1, '<');
		tmp.append(name);
		tmp.append(1, ' ');
		appendAttribString(tmp);
		if(children.empty()) {
			tmp.append(1, '>');
			if(needsEscape(data, false)) {
				string tmp2(data);
				escape(tmp2, false);
				tmp.append(tmp2);
			} else {
				tmp.append(data);
			}
		} else {
			tmp.append(">\r\n", 3);
			f->write(tmp);
			tmp.clear();
			for(auto& i: children) {
				i->toXML(indent + 1, f);
			}
			tmp.append(indent, '\t');
		}
		tmp.append("</", 2);
		tmp.append(name);
		tmp.append(">\r\n", 3);
		f->write(tmp);
	}
}