How To Write MinXML

Having created a MinXML element, perhaps by parsing a file and maybe modifying the results, we will often want to print it out to the console or to a file. This is easy to do using the print method. The print method takes a java.io.Writer or java.io.PrintWriter and sends the string representation to that writer. It is worth knowing that MinXML#toString() also uses the print method to generate its result.

Here's an example that creates a simple element and then prints it to the console. To send to the console we have to turn System.out into a java.io.Writer using new OutputStreamWriter(…).

package com.steelypip.tutorials.exercises.minxml;
 
import java.io.OutputStreamWriter;
 
import com.steelypip.powerups.minxml.FlexiMinXML;
import com.steelypip.powerups.minxml.MinXML;
 
public class SimpleOutput {
    public static void main( String[] args ) throws IOException {
 
        //    Create a simple element.
        MinXML simple = new FlexiMinXML( "img" );
        simple.putAttribute( "src", "http://upload.wikimedia.org/wikipedia/commons/a/a7/Uncia_uncia.jpg" );
 
        //    Send it to the console output and follow it with a newline.
        PrintWriter pw = new PrintWriter( System.out, true );
        simple.print( pw );
        pw.println();
 
    }
}

If you try this out in Eclipse, you should get the output:

<img src!="http://upload.wikimedia.org/wikipedia/commons/a/a7/Uncia_uncia.jpg"/>

The drawback of the print method is that it prints to a single line, which makes it impractical for human-reading for anything other than tiny elements. If you want to review anything more complicated, use the prettyPrint method. Here's the same basic code but for a much more complicated MinXML element - notice that you don't need to put an explicit newline at the end.

package com.steelypip.tutorials.exercises.minxml;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import com.steelypip.powerups.minxml.FlexiMinXML;
import com.steelypip.powerups.minxml.MinXML;
 
public class BiggerOutput {
 
    private static MinXML makeBiggerElement( final int level ) {
        if ( level == 0 ) {
            return new FlexiMinXML( "leaf" );
        } else {
            MinXML branch = new FlexiMinXML( "branch" );
            for ( int i = 0; i < level; i++ ) {
                branch.add( makeBiggerElement( level - 1 ) );
            }
            return branch;
        }
    }
 
    public static void main( String[] args ) throws IOException {    
        MinXML bigger = makeBiggerElement( 3 );
        PrintWriter pw = new PrintWriter( System.out, true );
        bigger.prettyPrint( pw );
    }
 
}

You should find it writes the following to the standard output:

<branch>
    <branch>
        <branch>
            <leaf/>
        </branch>
        <branch>
            <leaf/>
        </branch>
    </branch>
    <branch>
        <branch>
            <leaf/>
        </branch>
        <branch>
            <leaf/>
        </branch>
    </branch>
    <branch>
        <branch>
            <leaf/>
        </branch>
        <branch>
            <leaf/>
        </branch>
    </branch>
</branch>