Re: Easiest way to read a XML doc from file, reformat it and write
it indented to a second file?
Raymond Schanks wrote:
Now I want to read this XML doc file into Java, then reformat it so
that the hierarchy levels are indented in a human radble format "as usual".
How can I do this most easily (preferable without third party libraries but
only J2SE built-in functions)?
Probably something involving SAX. SAX is "event driven" and
lightweight, and doesn't require that you read an entire file into
memory. This would allow you to process files of any length, even those
that exceed your memory.
The following is adapted from some code in Learning Java, 3rd ed., by
O'Reilly. That book has tons of stuff in it, including a good overview
of all the different XML parsers in Java. I recommend it if you don't
have a better Java book on your shelf.
This code is very simple and needs work, but will get you started.
You'll have to add formatting for the attributes and add the <>
characters around elements as well.
package test;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author Brenden
*/
public class XmlFormatter {
public static void main( String[] args )
{
// test harness
String[] inputVectors = {
"<test><x /><y></y><z><a><b><c></c></b></a></z></test>",
};
StringWriter swout = new StringWriter( 1024 );
formatXmlStreams( new StringReader( inputVectors[0] ), swout );
System.out.println( swout );
}
static void formatXmlStreams( Reader input, Writer output ) {
try
{
SAXParserFactory factory =
SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
FormatterHandler formatter = new FormatterHandler();
xmlReader.setContentHandler( formatter );
xmlReader.parse( new InputSource( input ) );
} catch( ParserConfigurationException ex )
{
Logger.getLogger( XmlFormatter.class.getName() ).
log( Level.SEVERE, null, ex );
} catch( IOException ex )
{
Logger.getLogger( XmlFormatter.class.getName() ).
log( Level.SEVERE, null, ex );
} catch( SAXException ex )
{
Logger.getLogger( XmlFormatter.class.getName() ).
log( Level.SEVERE, null, ex );
}
}
}
class FormatterHandler extends DefaultHandler
{
int level=-1;
@Override
public void startElement( String namespace, String localname,
String qname, Attributes attrib )
{
indent( ++level );
System.out.println( "Start element: "+qname+" "+attrib );
}
@Override
public void endElement( String namespace, String localname,
String qname )
{
indent( level-- );
System.out.println( "End element: "+qname );
}
private void indent(int level) {
for( int i = 0; i < level; i++){
System.out.print( " " );
}
}
}
// Output:
Start element: test
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$AttributesProxy@b02e7a
Start element: x
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$AttributesProxy@b02e7a
End element: x
Start element: y
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$AttributesProxy@b02e7a
End element: y
Start element: z
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$AttributesProxy@b02e7a
Start element: a
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$AttributesProxy@b02e7a
Start element: b
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$AttributesProxy@b02e7a
Start element: c
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$AttributesProxy@b02e7a
End element: c
End element: b
End element: a
End element: z
End element: test
BUILD SUCCESSFUL (total time: 0 seconds)