Search code examples
xmlgroovystreamingmarkupbuilder

Add Newlines to Output from Groovy StreamingMarkupBuilder


I am creating a massive xml file in Groovy. I'm using StreamingMarkupBuilder because the regular MarkupBuilder runs me out of Java heap space. I'm looking for a way to insert newlines in the StreamingMarkupBuilder or to parse the stream as it is created. I can't write the entire stream to a variable, it's too big. Here's an approximation of what I'm doing:

<FileWriter I created earlier> << new StreamingMarkupBuilder().bind {
    Root {
        Person()
        ID(idVar)
        Location(locationMethod())
        }
    }

Let me know if you have any clarification questions, I'm happy to work with you.


Solution

  • Taking some time off helped me figure out a solution. I am placing mkp.yield "\r\n" between elements like this:

    Root {
        mkp.yield "\r\n"
        Person()
        mkp.yield "\r\n"
        ID(idVar)
        mkp.yield "\r\n"
        Location(locationMethod())
        mkp.yield "\r\n"
        }
    

    Not pretty, but it's doing what I need.