使用XMLStreamWriter写XML


package com.hqh.stax;


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

import org.junit.Test;

public class TestXMLStreamWriter {

@Test
public void test() {
XMLStreamWriter writer = null;
try {
writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
//XML头
writer.writeStartDocument("UTF-8", "1.0");
writer.writeEndDocument();

//正文
writer.writeStartElement("person");
writer.writeStartElement("id");
writer.writeCharacters("1");
writer.writeEndElement();
writer.writeEndElement();

//刷新流到目标
writer.flush();

} catch (XMLStreamException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} finally {
if(writer!=null) {
try {
writer.close();
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
}
}

}




结果:
1

你可能感兴趣的:(WEB后台@JAX-WS)