本文整理汇总了Java中javax.xml.stream.XMLOutputFactory.createXMLStreamWriter方法的典型用法代码示例。如果您正苦于以下问题:Java XMLOutputFactory.createXMLStreamWriter方法的具体用法?Java XMLOutputFactory.createXMLStreamWriter怎么用?Java XMLOutputFactory.createXMLStreamWriter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLOutputFactory
的用法示例。
在下文中一共展示了XMLOutputFactory.createXMLStreamWriter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeXMLByStAX
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的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: testUnboundPrefix
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
@Test
public void testUnboundPrefix() 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");
w.writeDefaultNamespace("foo");
w.writeCharacters("---");
w.writeEndElement();
w.writeEndDocument();
w.close();
// Unexpected success
String FAIL_MSG = "Unexpected success. Expected: " + "XMLStreamException - " + "if the namespace URI has not been bound to a prefix "
+ "and javax.xml.stream.isPrefixDefaulting has not been " + "set to true";
System.err.println(FAIL_MSG);
Assert.fail(FAIL_MSG);
} catch (XMLStreamException xmlStreamException) {
// Expected Exception
System.out.println("Expected XMLStreamException: " + xmlStreamException.toString());
}
}
示例3: testBoundPrefix
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的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);
}
}
示例4: testStreamWriterWithStAXResultNStreamWriter
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的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());
}
}
示例5: testStreamResult
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的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());
}
}
示例6: testRepairingPrefix
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
@Test
public void testRepairingPrefix() throws Exception {
try {
// repair namespaces
// use new XMLOutputFactory as changing its property settings
XMLOutputFactory xof = XMLOutputFactory.newInstance();
xof.setProperty(xof.IS_REPAIRING_NAMESPACES, new Boolean(true));
XMLStreamWriter w = xof.createXMLStreamWriter(System.out);
// here I'm trying to write
// <bar xmlns="foo" />
w.writeStartDocument();
w.writeStartElement("foo", "bar");
w.writeDefaultNamespace("foo");
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);
}
}
示例7: serializeCompany
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
public static void serializeCompany(File output, Company c)
throws JAXBException,
FileNotFoundException,
XMLStreamException
{
initializeJaxbContext();
OutputStream os = new FileOutputStream(output);
Marshaller marshaller = jaxbContext.createMarshaller();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(os);
marshaller.marshal(c, writer); // TODO: need a stream writer that does indentation
}
示例8: writeCompany
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
public static void writeCompany(File output, Company c)
throws JAXBException,
FileNotFoundException,
XMLStreamException
{
initializeJaxbContext();
OutputStream os = new FileOutputStream(output);
Marshaller marshaller = jaxbContext.createMarshaller();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(os);
marshaller.marshal(c, writer); // TODO: need a stream writer that does indentation
}
示例9: testEventWriterWithStAXResultNStreamWriter
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
@Test
public void testEventWriterWithStAXResultNStreamWriter() {
String encoding = "";
if (getSystemProperty("file.encoding").equals("UTF-8")) {
encoding = " encoding=\"UTF-8\"";
}
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLStreamWriter swriter = ofac.createXMLStreamWriter(buffer);
StAXResult res = new StAXResult(swriter);
XMLEventWriter writer = ofac.createXMLEventWriter(res);
XMLEventFactory efac = XMLEventFactory.newInstance();
writer.add(efac.createStartDocument(null, "1.0"));
writer.add(efac.createStartElement("", "", "root"));
writer.add(efac.createEndElement("", "", "root"));
writer.add(efac.createEndDocument());
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例10: writeTo
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的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);
}
}
示例11: producePureXMLLogoutRequest
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
private ByteArrayOutputStream producePureXMLLogoutRequest(String logoutUrl, String nameID, String format, String sessionIndex, String issuer, String issueInstant) throws XMLStreamException, UnsupportedEncodingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(baos);
writer.writeStartElement("saml2p", "LogoutRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
writer.writeNamespace("saml2p", "urn:oasis:names:tc:SAML:2.0:protocol");
writer.writeAttribute("ID", "_" + UUID.randomUUID().toString());
writer.writeAttribute("Version", "2.0");
writer.writeAttribute("Destination", logoutUrl);
writer.writeAttribute("IssueInstant", issueInstant + "Z");
writer.writeStartElement("saml2", "Issuer", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.writeNamespace("saml2", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.writeCharacters(issuer);
writer.writeEndElement();
writer.writeStartElement("saml", "NameID", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.writeAttribute("Format", format);
writer.writeCharacters(nameID);
writer.writeEndElement();
writer.writeStartElement("saml2p", "SessionIndex", "urn:oasis:names:tc:SAML:2.0:protocol");
writer.writeCharacters(sessionIndex);
writer.writeEndElement();
writer.writeEndElement();
writer.flush();
return baos;
}
示例12: test
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
/**
* Ensure that charset aliases are checked. The encoding ISO-8859-1 is
* returned as ISO8859_1 by the underlying writer. Thus, if alias are not
* inspected, this test throws an exception.
*/
@Test
public void test() {
final String ENCODING = "ISO-8859-1";
try {
OutputStream out = new ByteArrayOutputStream();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(out, ENCODING);
writer.writeStartDocument(ENCODING, "1.0");
} catch (XMLStreamException e) {
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例13: generateAndReadXml
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
void generateAndReadXml(String content) throws Exception {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
OutputStreamWriter streamWriter = new OutputStreamWriter(stream);
XMLStreamWriter writer = factory.createXMLStreamWriter(streamWriter);
// Generate xml with selected stream writer type
generateXML(writer, content);
String output = stream.toString();
System.out.println("Generated xml: " + output);
// Read generated xml with StAX parser
readXML(output.getBytes(), content);
}
示例14: test1
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
@Test
public void test1() throws Exception {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
StringWriter sw = new StringWriter();
XMLStreamWriter w = xof.createXMLStreamWriter(sw);
w.writeStartDocument();
w.writeStartElement("foo", "bar", "zot");
w.writeDefaultNamespace(null);
w.writeCharacters("---");
}
示例15: fetchFile
import javax.xml.stream.XMLOutputFactory; //导入方法依赖的package包/类
private String fetchFile(final String doc, DOMForest forest, final Map<String, String> documentMap, File destDir) throws IOException, XMLStreamException {
DocumentLocationResolver docLocator = createDocResolver(doc, forest, documentMap);
WSDLPatcher wsdlPatcher = new WSDLPatcher(new PortAddressResolver() {
@Override
public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) {
return null;
}
}, docLocator);
XMLStreamReader xsr = null;
XMLStreamWriter xsw = null;
OutputStream os = null;
String resolvedRootWsdl = null;
try {
XMLOutputFactory writerfactory;
xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false);
writerfactory = XMLOutputFactory.newInstance();
resolvedRootWsdl = docLocator.getLocationFor(null, doc);
File outFile = new File(destDir, resolvedRootWsdl);
os = new FileOutputStream(outFile);
if(options.verbose) {
listener.message(WscompileMessages.WSIMPORT_DOCUMENT_DOWNLOAD(doc,outFile));
}
xsw = writerfactory.createXMLStreamWriter(os);
//DOMForest eats away the whitespace loosing all the indentation, so write it through
// indenting writer for better readability of fetched documents
IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(xsw);
wsdlPatcher.bridge(xsr, indentingWriter);
options.addGeneratedFile(outFile);
} finally {
try {
if (xsr != null) {xsr.close();}
if (xsw != null) {xsw.close();}
} finally {
if (os != null) {os.close();}
}
}
return resolvedRootWsdl;
}