views:

91

answers:

3

Hi guys, im having a hard time trying to indent .xml files using XMLSerializer. I've tried serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);. I've tried to append \n into FileWriter but the output is the \n's and \t's at the beginning of the file and not in the right place. I've tried setPropery with the proper URI etc.

Part of the code:

XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
            parserFactory .setNamespaceAware(true);
            XmlSerializer serializer = parserFactory .newSerializer();
            File xmlFile = new File(PATH + ".xml");         
            FileWriter writer = new FileWriter(xmlFile);            
            serializer.setOutput(writer);
            //serializer.setProperty(INDENT_URL, INDENT);
            serializer.startDocument("UTF-8", null);
            //serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
            serializer.startTag(null, "bla");
            writer.append('\n');

Im desperate, what should i do?

+1  A: 

This is a solution in Java, andriod does support transformer so this should work.

// import additional packages
import java.io.*;

// import DOM related classes
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

// write the output file
try {
  // create a transformer
  TransformerFactory transFactory = TransformerFactory.newInstance();
  Transformer        transformer  = transFactory.newTransformer();

  // set some options on the transformer
  transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

  // get a transformer and supporting classes
  StringWriter writer = new StringWriter();
  StreamResult result = new StreamResult(writer);
  DOMSource    source = new DOMSource(xmlDoc);

  // transform the xml document into a string
  transformer.transform(source, result);

  // open the output file
  FileWriter outputWriter = new FileWriter(outputFile);
  outputWriter.write(writer.toString());
  outputWriter.close();

} catch(javax.xml.transform.TransformerException e) {
  // do something with this error
}catch (java.io.IOException ex) {
  // do something with this error
}

Source : http://techxplorer.com/2010/05/20/indenting-xml-output-in-java/

JonWillis
I'll give it a try, thx JonWillis!
Philipz
:) I just have good google foo today. XML serialiser is to serialise data to XML, and file writer does writing to a file. So the responsibility to format to something human readable lies in another class if it already exists :)
JonWillis
A: 

Have you tried using these two properties "in combination" on Serializer?

// indentation as 3 spaces
serializer.setProperty(
   "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "   ");
// also set the line separator
serializer.setProperty(
   "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");
naikus
Yes. I did and it gave me this error: java.lang.RuntimeException: Unsupported Property:at org.kxml2.io.KXmlSerializer.setProperty(KXmlSerializer.java:260)....
Philipz
A: 

serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); worked now.

I dont know if i was putting it before serializer.startDocument(encoding, standalone) or there was a error with stuff not related to the .xml creation!

Thanks guys!

Philipz