本文整理汇总了Java中javax.xml.stream.XMLStreamWriter.close方法的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamWriter.close方法的具体用法?Java XMLStreamWriter.close怎么用?Java XMLStreamWriter.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLStreamWriter
的用法示例。
在下文中一共展示了XMLStreamWriter.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeXMLByStAX
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public static void writeXMLByStAX() throws XMLStreamException, FileNotFoundException {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(new FileOutputStream("output.xml"));
writer.writeStartDocument();
writer.writeCharacters(" ");
writer.writeComment("testing comment");
writer.writeCharacters(" ");
writer.writeStartElement("catalogs");
writer.writeNamespace("myNS", "http://blog.csdn.net/Chinajash");
writer.writeAttribute("owner", "sina");
writer.writeCharacters(" ");
writer.writeStartElement("http://blog.csdn.net/Chinajash", "catalog");
writer.writeAttribute("id", "007");
writer.writeCharacters("Apparel");
// 写入catalog元素的结束标签
writer.writeEndElement();
// 写入catalogs元素的结束标签
writer.writeEndElement();
// 结束 XML 文档
writer.writeEndDocument();
writer.close();
System.out.println("ok");
}
示例2: writeXMLFile
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public void writeXMLFile(HashMap<String,String> lines,String path) throws IOException, XMLStreamException{
XMLOutputFactory xof = XMLOutputFactory.newInstance();
final XMLStreamWriter xtw = xof.createXMLStreamWriter(new FileWriter(path));
xtw.writeStartDocument("utf-8","1.0");
xtw.writeCharacters("\n");
lines.forEach((key,value) ->{
try {
xtw.writeStartElement(key);
xtw.writeCharacters(value);
xtw.writeEndElement();
xtw.writeCharacters("\n");
} catch (XMLStreamException ex) {
Logger.getLogger(XMLController.class.getName()).log(Level.SEVERE, null, ex);
}
});
xtw.writeEndDocument();
xtw.flush();
xtw.close();
}
示例3: writeDocument
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public static void writeDocument(Document doc, OutputStream outputStream, String namespaceURI) throws XMLStreamException, IOException {
if(outputFactory == null) {
outputFactory = XMLOutputFactory.newInstance();
}
XMLStreamWriter xsw = null;
try {
if(doc instanceof TextualDocument) {
xsw = outputFactory.createXMLStreamWriter(outputStream,
((TextualDocument)doc).getEncoding());
xsw.writeStartDocument(((TextualDocument)doc).getEncoding(), "1.0");
}
else {
xsw = outputFactory.createXMLStreamWriter(outputStream);
xsw.writeStartDocument("1.0");
}
newLine(xsw);
writeDocument(doc, xsw, namespaceURI);
}
finally {
if(xsw != null) {
xsw.close();
}
}
}
示例4: writeXcesAnnotations
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
/**
* Save annotations to the given output stream in XCES format, with
* their IDs included as the "n" attribute of each <code>struct</code>.
* The stream is <i>not</i> closed by this method, that is left to
* the caller.
*
* @param annotations the annotations to save, typically an
* AnnotationSet
* @param os the output stream to write to
* @param encoding the character encoding to use.
*/
public static void writeXcesAnnotations(Collection<Annotation> annotations,
OutputStream os, String encoding) throws XMLStreamException {
XMLStreamWriter xsw = null;
try {
if(outputFactory == null) {
outputFactory = XMLOutputFactory.newInstance();
}
if(encoding == null) {
xsw = outputFactory.createXMLStreamWriter(os);
xsw.writeStartDocument();
}
else {
xsw = outputFactory.createXMLStreamWriter(os, encoding);
xsw.writeStartDocument(encoding, "1.0");
}
newLine(xsw);
writeXcesAnnotations(annotations, xsw);
}
finally {
if(xsw != null) {
xsw.close();
}
}
}
示例5: testStreamWriterWithStAXResultNStreamWriter
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Test
public void testStreamWriterWithStAXResultNStreamWriter() {
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLStreamWriter writer = ofac.createXMLStreamWriter(buffer);
StAXResult res = new StAXResult(writer);
writer = ofac.createXMLStreamWriter(res);
writer.writeStartDocument("1.0");
writer.writeStartElement("root");
writer.writeEndElement();
writer.writeEndDocument();
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例6: generateXML
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
private void generateXML(XMLStreamWriter writer, String sequence)
throws XMLStreamException {
char[] seqArr = sequence.toCharArray();
writer.writeStartDocument();
writer.writeStartElement("root");
// Use writeCharacters( String ) to write characters
writer.writeStartElement("writeCharactersWithString");
writer.writeCharacters(sequence);
writer.writeEndElement();
// Use writeCharacters( char [], int , int ) to write characters
writer.writeStartElement("writeCharactersWithArray");
writer.writeCharacters(seqArr, 0, seqArr.length);
writer.writeEndElement();
// Close root element and document
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
writer.close();
}
示例7: doRecycle
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
public void doRecycle(XMLStreamWriter r) {
if (r instanceof HasEncodingWriter) {
r = ((HasEncodingWriter)r).getWriter();
}
if(zephyrClass.isInstance(r)) {
// this flushes the underlying stream, so it might cause chunking issue
try {
r.close();
} catch (XMLStreamException e) {
throw new WebServiceException(e);
}
pool.set(r);
}
if(r instanceof RecycleAware)
((RecycleAware)r).onRecycled();
}
示例8: testBoundPrefix
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Test
public void testBoundPrefix() throws Exception {
try {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
XMLStreamWriter w = xof.createXMLStreamWriter(System.out);
// here I'm trying to write
// <bar xmlns="foo" />
w.writeStartDocument();
w.writeStartElement("foo", "bar", "http://namespace");
w.writeCharacters("---");
w.writeEndElement();
w.writeEndDocument();
w.close();
// Expected success
System.out.println("Expected success.");
} catch (Exception exception) {
// Unexpected Exception
String FAIL_MSG = "Unexpected Exception: " + exception.toString();
System.err.println(FAIL_MSG);
Assert.fail(FAIL_MSG);
}
}
示例9: testStreamResult
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Test
public void testStreamResult() {
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
StreamResult sr = new StreamResult(buffer);
XMLStreamWriter writer = ofac.createXMLStreamWriter(sr);
writer.writeStartDocument("1.0");
writer.writeStartElement("root");
writer.writeEndElement();
writer.writeEndDocument();
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例10: writeXml
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public static void writeXml(Path configDir, Path xmlFile) throws IOException, XMLStreamException {
XMLOutputFactory output = XMLOutputFactory.newInstance();
try (Writer writer = Files.newBufferedWriter(xmlFile, StandardCharsets.UTF_8)) {
XMLStreamWriter xmlWriter = output.createXMLStreamWriter(writer);
try {
xmlWriter.writeStartDocument(StandardCharsets.UTF_8.toString(), "1.0");
xmlWriter.writeStartElement("config");
try (DirectoryStream<Path> ds = Files.newDirectoryStream(configDir, entry -> Files.isRegularFile(entry) && entry.getFileName().toString().endsWith(".properties"))) {
for (Path file : ds) {
String fileName = file.getFileName().toString();
String fileNameWithoutExtension = fileName.substring(0, fileName.length() - 11);
xmlWriter.writeStartElement(fileNameWithoutExtension);
Properties properties = new Properties();
try (Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
properties.load(reader);
}
for (String name : properties.stringPropertyNames()) {
String value = properties.getProperty(name);
xmlWriter.writeStartElement(name);
xmlWriter.writeCharacters(value);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
}
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
} finally {
xmlWriter.close();
}
}
}
示例11: testSOAPEnvelope1
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Test
public void testSOAPEnvelope1() throws Exception {
setup();
File f = new File(this.getClass().getResource(INPUT_FILE1).getFile());
System.out.println("***********" + f.getName() + "***********");
DOMSource src = makeDomSource(f);
Node node = src.getNode();
XMLStreamWriter writer = staxOut.createXMLStreamWriter(new PrintStream(System.out));
DOMUtil.serializeNode((Element) node.getFirstChild(), writer);
writer.close();
assert (true);
System.out.println("*****************************************");
}
示例12: writeTo
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
public void writeTo(DatasetConfig datasetConfig, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
final String hostname;
if (httpHeaders.containsKey(WebServer.X_DREMIO_HOSTNAME)) {
hostname = (String) httpHeaders.getFirst(WebServer.X_DREMIO_HOSTNAME);
} else {
hostname = masterNode;
}
// Change headers to force download and suggest a filename.
String fullPath = Joiner.on(".").join(datasetConfig.getFullPathList());
httpHeaders.putSingle(HttpHeaders.CONTENT_DISPOSITION, format("attachment; filename=\"%s.tds\"", fullPath));
try {
final XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(entityStream, "UTF-8");
xmlStreamWriter.writeStartDocument("utf-8", "1.0");
writeDatasource(xmlStreamWriter, datasetConfig, hostname, mediaType);
xmlStreamWriter.writeEndDocument();
xmlStreamWriter.close();
} catch (XMLStreamException e) {
throw UserExceptionMapper.withStatus(
UserException.dataWriteError(e)
.message("Cannot generate TDS file")
.addContext("Details", e.getMessage()),
Status.INTERNAL_SERVER_ERROR
).build(logger);
}
}
示例13: writeTo
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
public void writeTo(HashMap hashMap, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
try {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(outputStream);
xmlWriter.writeStartElement("entity");
writeValue(hashMap, xmlWriter);
xmlWriter.writeEndElement();
xmlWriter.flush();
xmlWriter.close();
} catch (XMLStreamException e) {
throw new IOException(e);
}
}
示例14: writeTo
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
public void writeTo(ArrayList arrayList, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
try {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(outputStream);
writeValue(arrayList, xmlWriter);
xmlWriter.flush();
xmlWriter.close();
} catch (XMLStreamException e) {
throw new IOException(e);
}
}
示例15: toString
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(super.toString());
String content;
try {
Message msg = getMessage();
if (msg != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlWriter = XMLStreamWriterFactory.create(baos, "UTF-8");
msg.copy().writeTo(xmlWriter);
xmlWriter.flush();
xmlWriter.close();
baos.flush();
XMLStreamWriterFactory.recycle(xmlWriter);
byte[] bytes = baos.toByteArray();
//message = Messages.create(XMLStreamReaderFactory.create(null, new ByteArrayInputStream(bytes), "UTF-8", true));
content = new String(bytes, "UTF-8");
} else {
content = "<none>";
}
} catch (Throwable t) {
throw new WebServiceException(t);
}
buf.append(" Content: ").append(content);
return buf.toString();
}