本文整理匯總了Java中javax.xml.stream.XMLOutputFactory.newInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java XMLOutputFactory.newInstance方法的具體用法?Java XMLOutputFactory.newInstance怎麽用?Java XMLOutputFactory.newInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.stream.XMLOutputFactory
的用法示例。
在下文中一共展示了XMLOutputFactory.newInstance方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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());
}
}
示例2: writeXMLFile
import javax.xml.stream.XMLOutputFactory; //導入方法依賴的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: 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: 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());
}
}
示例5: 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);
}
}
示例6: 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());
}
}
示例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: testStreamWriterWithStAXResultNEventWriter
import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
@Test
public void testStreamWriterWithStAXResultNEventWriter() throws Exception {
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
StAXResult res = new StAXResult(writer);
XMLStreamWriter swriter = ofac.createXMLStreamWriter(res);
Assert.fail("Expected an Exception as XMLStreamWriter can't be created " + "with a StAXResult which has EventWriter.");
} catch (Exception e) {
System.out.println(e.toString());
}
}
示例9: 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
}
示例10: setUp
import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
@BeforeMethod
public void setUp() {
try {
outputFactory = XMLOutputFactory.newInstance();
inputFactory = XMLInputFactory.newInstance();
} catch (Exception ex) {
Assert.fail("Could not create XMLInputFactory");
}
}
示例11: createInitXML
import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
public void createInitXML(String xml_path,String subnet,String count) throws SAXException, ParserConfigurationException, IOException, XMLStreamException{
XMLOutputFactory xof = XMLOutputFactory.newInstance();
XMLStreamWriter xtw = null;
xtw = xof.createXMLStreamWriter(new FileWriter("init.xml"));
xtw.writeStartDocument("utf-8","1.0");
xtw.writeCharacters("\n");
xtw.writeStartElement("SETTINGS");
xtw.writeCharacters("\n");
xtw.writeStartElement("XML_PATH");
xtw.writeCharacters("\n");
xtw.writeCharacters(xml_path);
xtw.writeCharacters("\n");
xtw.writeEndElement();
xtw.writeCharacters("\n");
xtw.writeStartElement("SUBNET");
xtw.writeCharacters("\n");
xtw.writeCharacters(subnet);
xtw.writeCharacters("\n");
xtw.writeEndElement();
xtw.writeCharacters("\n");
xtw.writeStartElement("count");
xtw.writeCharacters("\n");
xtw.writeCharacters(count);
xtw.writeCharacters("\n");
xtw.writeEndElement();
xtw.writeCharacters("\n");
xtw.writeEndElement();
xtw.writeCharacters("\n");
xtw.writeEndDocument();
xtw.flush();
xtw.close();
}
示例12: 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;
}
示例13: toXml
import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
/**
* Returns a string containing the specified document in GATE XML
* format.
*
* @param doc the document
*/
public static String toXml(Document doc) {
try {
if(outputFactory == null) {
outputFactory = XMLOutputFactory.newInstance();
}
StringWriter sw = new StringWriter(doc.getContent().size().intValue()
* DocumentXmlUtils.DOC_SIZE_MULTIPLICATION_FACTOR);
XMLStreamWriter xsw = outputFactory.createXMLStreamWriter(sw);
// start the document
if(doc instanceof TextualDocument) {
xsw.writeStartDocument(((TextualDocument)doc).getEncoding(), "1.0");
}
else {
xsw.writeStartDocument("1.0");
}
newLine(xsw);
writeDocument(doc, xsw, "");
xsw.close();
return sw.toString();
}
catch(XMLStreamException xse) {
throw new GateRuntimeException("Error converting document to XML", xse);
}
}
示例14: testXMLOutputFactory
import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
public void testXMLOutputFactory() {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
success("testXMLOutputFactory passed");
}
示例15: testCR6420953
import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
@Test
public void testCR6420953() {
try {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
StringWriter sw = new StringWriter();
XMLStreamWriter w = xof.createXMLStreamWriter(sw);
w.writeStartDocument();
w.writeStartElement("element");
w.writeDefaultNamespace(XML_CONTENT);
w.writeNamespace("prefix", XML_CONTENT);
w.writeAttribute("attribute", XML_CONTENT);
w.writeAttribute(XML_CONTENT, "attribute2", XML_CONTENT);
w.writeAttribute("prefix", XML_CONTENT, "attribute3", XML_CONTENT);
w.writeCharacters("\n");
w.writeCharacters(XML_CONTENT);
w.writeCharacters("\n");
w.writeCharacters(XML_CONTENT.toCharArray(), 0, XML_CONTENT.length());
w.writeCharacters("\n");
w.writeEndElement();
w.writeEndDocument();
w.flush();
System.out.println(sw);
// make sure that the generated XML parses
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.newDocumentBuilder().parse(new InputSource(new StringReader(sw.toString())));
} catch (XMLStreamException xmlStreamException) {
xmlStreamException.printStackTrace();
Assert.fail(xmlStreamException.toString());
} catch (SAXException saxException) {
saxException.printStackTrace();
Assert.fail(saxException.toString());
} catch (ParserConfigurationException parserConfigurationException) {
parserConfigurationException.printStackTrace();
Assert.fail(parserConfigurationException.toString());
} catch (IOException ioException) {
ioException.printStackTrace();
Assert.fail(ioException.toString());
}
}