本文整理汇总了Java中javax.xml.stream.XMLOutputFactory类的典型用法代码示例。如果您正苦于以下问题:Java XMLOutputFactory类的具体用法?Java XMLOutputFactory怎么用?Java XMLOutputFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XMLOutputFactory类属于javax.xml.stream包,在下文中一共展示了XMLOutputFactory类的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: testXMLStreamWriter
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
/**
* Test XMLStreamWriter parsing a file with an external entity reference.
*/
@Test
public void testXMLStreamWriter() {
try {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(System.out);
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
String file = getClass().getResource("XMLEventWriterTest.xml").getPath();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StreamSource(new File(file)));
// adds the event to the consumer.
eventWriter.add(eventReader);
eventWriter.flush();
eventWriter.close();
// expected success
} catch (Exception exception) {
exception.printStackTrace();
Assert.fail(exception.toString());
}
}
示例3: test
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
@Test
public void test() {
try {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
StreamResult sr = new StreamResult();
XMLStreamWriter xsw = xof.createXMLStreamWriter(sr);
NamespaceContext nc = xsw.getNamespaceContext();
System.out.println(nc.getPrefix(XMLConstants.XML_NS_URI));
System.out.println(" expected result: " + XMLConstants.XML_NS_PREFIX);
System.out.println(nc.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
System.out.println(" expected result: " + XMLConstants.XMLNS_ATTRIBUTE);
Assert.assertTrue(nc.getPrefix(XMLConstants.XML_NS_URI) == XMLConstants.XML_NS_PREFIX);
Assert.assertTrue(nc.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) == XMLConstants.XMLNS_ATTRIBUTE);
} catch (Throwable ex) {
Assert.fail(ex.toString());
}
}
示例4: testCreateStartDocument_DOMWriter
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
/**
* @bug 8139584
* Verifies that the resulting XML contains the standalone setting.
*/
@Test
public void testCreateStartDocument_DOMWriter()
throws ParserConfigurationException, XMLStreamException {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
XMLEventWriter eventWriter = xof.createXMLEventWriter(new DOMResult(doc));
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true);
eventWriter.add(event);
eventWriter.flush();
Assert.assertEquals(doc.getXmlEncoding(), "iso-8859-15");
Assert.assertEquals(doc.getXmlVersion(), "1.0");
Assert.assertTrue(doc.getXmlStandalone());
}
示例5: setUp
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
@BeforeMethod
public void setUp() {
// want a Factory that repairs Namespaces
xmlOutputFactory = XMLOutputFactory.newInstance();
xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
// new OutputStream
byteArrayOutputStream = new ByteArrayOutputStream();
// new Writer
try {
xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(byteArrayOutputStream, "utf-8");
} catch (XMLStreamException xmlStreamException) {
Assert.fail(xmlStreamException.toString());
}
}
示例6: writeDocument
import javax.xml.stream.XMLOutputFactory; //导入依赖的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();
}
}
}
示例7: writeTo
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
public void writeTo(SOAPMessage saaj) throws SOAPException {
try {
// TODO what about in-scope namespaces
// Not very efficient consider implementing a stream buffer
// processor that produces a DOM node from the buffer.
Transformer t = XmlUtil.newTransformer();
SOAPHeader header = saaj.getSOAPHeader();
if (header == null)
header = saaj.getSOAPPart().getEnvelope().addHeader();
// TODO workaround for oracle xdk bug 16555545, when this bug is fixed the line below can be
// uncommented and all lines below, except the catch block, can be removed.
// t.transform(epr.asSource(localName), new DOMResult(header));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter w = XMLOutputFactory.newFactory().createXMLStreamWriter(baos);
epr.writeTo(localName, w);
w.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
fac.setNamespaceAware(true);
Node eprNode = fac.newDocumentBuilder().parse(bais).getDocumentElement();
Node eprNodeToAdd = header.getOwnerDocument().importNode(eprNode, true);
header.appendChild(eprNodeToAdd);
} catch (Exception e) {
throw new SOAPException(e);
}
}
示例8: init
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
/**
* Initialize an instance of this XMLStreamWriter. Allocate new instances
* for all the data structures. Set internal flags based on property values.
*/
private void init() {
fReuse = false;
fNamespaceDecls = new ArrayList();
fPrefixGen = new Random();
fAttributeCache = new ArrayList();
fInternalNamespaceContext = new NamespaceSupport();
fInternalNamespaceContext.reset();
fNamespaceContext = new NamespaceContextImpl();
fNamespaceContext.internalContext = fInternalNamespaceContext;
// Set internal state based on property values
Boolean ob = (Boolean) fPropertyManager.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
fIsRepairingNamespace = ob.booleanValue();
ob = (Boolean) fPropertyManager.getProperty(Constants.ESCAPE_CHARACTERS);
setEscapeCharacters(ob.booleanValue());
}
示例9: reset
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
/**
* Reset this instance so that it can be re-used. Clears but does not
* re-allocate internal data structures.
*
* @param resetProperties Indicates if properties should be read again
*/
void reset(boolean resetProperties) {
if (!fReuse) {
throw new java.lang.IllegalStateException(
"close() Must be called before calling reset()");
}
fReuse = false;
fNamespaceDecls.clear();
fAttributeCache.clear();
// reset Element/NamespaceContext stacks
fElementStack.clear();
fInternalNamespaceContext.reset();
fStartTagOpened = false;
fNamespaceContext.userContext = null;
if (resetProperties) {
Boolean ob = (Boolean) fPropertyManager.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
fIsRepairingNamespace = ob.booleanValue();
ob = (Boolean) fPropertyManager.getProperty(Constants.ESCAPE_CHARACTERS);
setEscapeCharacters(ob.booleanValue());
}
}
示例10: writeTo
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
public void writeTo(SOAPMessage saaj) throws SOAPException {
try {
// TODO what about in-scope namespaces
// Not very efficient consider implementing a stream buffer
// processor that produces a DOM node from the buffer.
Transformer t = XmlUtil.newTransformer();
SOAPHeader header = saaj.getSOAPHeader();
if (header == null)
header = saaj.getSOAPPart().getEnvelope().addHeader();
// TODO workaround for oracle xdk bug 16555545, when this bug is fixed the line below can be
// uncommented and all lines below, except the catch block, can be removed.
// t.transform(epr.asSource(localName), new DOMResult(header));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter w = XMLOutputFactory.newFactory().createXMLStreamWriter(baos);
epr.writeTo(localName, w);
w.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
DocumentBuilderFactory fac = XmlUtil.newDocumentBuilderFactory(false);
fac.setNamespaceAware(true);
Node eprNode = fac.newDocumentBuilder().parse(bais).getDocumentElement();
Node eprNodeToAdd = header.getOwnerDocument().importNode(eprNode, true);
header.appendChild(eprNodeToAdd);
} catch (Exception e) {
throw new SOAPException(e);
}
}
示例11: init
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
/**
* Initialize an instance of this XMLStreamWriter. Allocate new instances
* for all the data structures. Set internal flags based on property values.
*/
private void init() {
fReuse = false;
fNamespaceDecls = new ArrayList<>();
fPrefixGen = new Random();
fAttributeCache = new ArrayList<>();
fInternalNamespaceContext = new NamespaceSupport();
fInternalNamespaceContext.reset();
fNamespaceContext = new NamespaceContextImpl();
fNamespaceContext.internalContext = fInternalNamespaceContext;
// Set internal state based on property values
Boolean ob = (Boolean) fPropertyManager.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
fIsRepairingNamespace = ob;
ob = (Boolean) fPropertyManager.getProperty(Constants.ESCAPE_CHARACTERS);
setEscapeCharacters(ob);
}
示例12: reset
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
/**
* Reset this instance so that it can be re-used. Clears but does not
* re-allocate internal data structures.
*
* @param resetProperties Indicates if properties should be read again
*/
void reset(boolean resetProperties) {
if (!fReuse) {
throw new java.lang.IllegalStateException(
"close() Must be called before calling reset()");
}
fReuse = false;
fNamespaceDecls.clear();
fAttributeCache.clear();
// reset Element/NamespaceContext stacks
fElementStack.clear();
fInternalNamespaceContext.reset();
fStartTagOpened = false;
fNamespaceContext.userContext = null;
if (resetProperties) {
Boolean ob = (Boolean) fPropertyManager.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
fIsRepairingNamespace = ob;
ob = (Boolean) fPropertyManager.getProperty(Constants.ESCAPE_CHARACTERS);
setEscapeCharacters(ob);
}
}
示例13: testStreamReader
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
@Test
public void testStreamReader() {
XMLInputFactory ifac = XMLInputFactory.newInstance();
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
try {
ifac.setProperty(ifac.IS_REPLACING_ENTITY_REFERENCES, new Boolean(false));
XMLStreamReader re = ifac.createXMLStreamReader(this.getClass().getResource(INPUT_FILE).toExternalForm(),
this.getClass().getResourceAsStream(INPUT_FILE));
while (re.hasNext()) {
int event = re.next();
if (event == XMLStreamConstants.START_ELEMENT && re.getLocalName().equals("bookurn")) {
Assert.assertTrue(re.getAttributeCount() == 0, "No attributes are expected for <bookurn> ");
Assert.assertTrue(re.getNamespaceCount() == 2, "Two namespaces are expected for <bookurn> ");
}
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例14: testCreateStartDocument
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
/**
* @bug 8139584
* Verifies that the resulting XML contains the standalone setting.
*/
@Test
public void testCreateStartDocument() throws XMLStreamException {
StringWriter stringWriter = new StringWriter();
XMLOutputFactory out = XMLOutputFactory.newInstance();
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEventWriter eventWriter = out.createXMLEventWriter(stringWriter);
XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true);
eventWriter.add(event);
eventWriter.flush();
Assert.assertTrue(stringWriter.toString().contains("encoding=\"iso-8859-15\""));
Assert.assertTrue(stringWriter.toString().contains("version=\"1.0\""));
Assert.assertTrue(stringWriter.toString().contains("standalone=\"yes\""));
}
示例15: testWriteComment
import javax.xml.stream.XMLOutputFactory; //导入依赖的package包/类
/**
* Test of main method, of class TestXMLStreamWriter.
*/
@Test
public void testWriteComment() {
try {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html href=\"http://java.sun.com\"><!--This is comment-->java.sun.com</a:html>";
XMLOutputFactory f = XMLOutputFactory.newInstance();
// f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
// Boolean.TRUE);
StringWriter sw = new StringWriter();
XMLStreamWriter writer = f.createXMLStreamWriter(sw);
writer.writeStartDocument("UTF-8", "1.0");
writer.writeStartElement("a", "html", "http://www.w3.org/TR/REC-html40");
writer.writeAttribute("href", "http://java.sun.com");
writer.writeComment("This is comment");
writer.writeCharacters("java.sun.com");
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
sw.flush();
StringBuffer sb = sw.getBuffer();
System.out.println("sb:" + sb.toString());
Assert.assertTrue(sb.toString().equals(xml));
} catch (Exception ex) {
Assert.fail("Exception: " + ex.getMessage());
}
}