You could process “raw” XML output (which is always well formed) with XSLT to produce pretty much any structure you want. Introducing structure this way has two possible drawbacks:
• XSLT processors are not fast, especially on very large documents; most are written in Java, but even the C++ implementations take longer than you might find tolerable in production.
• Designing the XSLT template to add the structure could be a guru-level task.