views:

11

answers:

1

I am using Staxmate API to generate XML file. After reading the tutorial: http://staxmate.codehaus.org/Tutorial I tried making the changes in my code. At last I added the call

doc.setIndentation("\n  ", 1, 1);

Which causes the newly generated XML file to be empty! Without this method call entire XML file gets generated as expected.

Suspecting something fishy in in project setup, I created a Test class in the same package with the code given in tutorial:

package ch.synlogic.iaf.export;

import java.io.File;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;

import org.codehaus.staxmate.SMOutputFactory;
import org.codehaus.staxmate.out.SMOutputDocument;
import org.codehaus.staxmate.out.SMOutputElement;

public class Test {

public static void main(String[] args) {
 main("c:\\tmp\\empl.xml");
}

public static void main(String fname)
{
 // 1: need output factory
 SMOutputFactory outf = new SMOutputFactory(XMLOutputFactory.newInstance());
 SMOutputDocument doc;
 try {
  doc = outf.createOutputDocument(new File(fname));

 // (optional) 3: enable indentation (note spaces after backslash!)
 doc.setIndentation("\n  ", 1, 1);
 // 4. comment regarding generation time
 doc.addComment(" generated: "+new java.util.Date().toString());
 SMOutputElement empl = doc.addElement("employee");
 empl.addAttribute(/*namespace*/ null, "id", 123);
 SMOutputElement name = empl.addElement("name");
 name.addElement("first").addCharacters("Tatu");
 name.addElement("last").addCharacters("Saloranta");
 // 10. close the document to close elements, flush output
 doc.closeRoot();
 } catch (XMLStreamException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
}

Now when I invoke the main(String) method from my code the problem still persists whereas if I just run class Test as it is it works smoothly! My code involves database initializations and some other product specific actions.

I am lost, any thoughts on how should I proceed with this?

+1  A: 

Indentation works with Woodstox API

WstxOutputFactory factory = new WstxOutputFactory();
factory.setProperty(WstxOutputFactory.P_AUTOMATIC_EMPTY_ELEMENTS, true);
SMOutputFactory outf = new SMOutputFactory(factory);
doc = outf.createOutputDocument(fout);
doc.setIndentation("\n  ", 1, 1);
thequark
Quick question: which implementation did you initially use? Sun's Stax implementation that comes with JDK 1.6? It is possible that there is a bug in interaction of the two, since StaxMate is mostly tested using Woodstox (there are some tests for Sjsxp too, just not as manuy)
StaxMan
Btw: my guess as to what is going on is that the Stax implementation in question does not flush or close underlying File (or rather, FileOutputStream).Stax API has little bit strange definition of what streamWriter.close() should do which causes some issues.So it may be that passing FileOutputStream and explicitly closing it afterwards would work best.However, as far as I know, Woodstox would handle this case as expected.
StaxMan