本文整理匯總了Java中javax.xml.stream.XMLOutputFactory.setProperty方法的典型用法代碼示例。如果您正苦於以下問題:Java XMLOutputFactory.setProperty方法的具體用法?Java XMLOutputFactory.setProperty怎麽用?Java XMLOutputFactory.setProperty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.stream.XMLOutputFactory
的用法示例。
在下文中一共展示了XMLOutputFactory.setProperty方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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("---");
}
示例2: 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);
}
}
示例3: testDuplicateNSDeclaration
import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
@Test
public void testDuplicateNSDeclaration() {
// expect only 1 Namespace Declaration
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<ns1:foo" + " xmlns:ns1=\"http://example.com/\">" + "</ns1:foo>";
// have XMLOutputFactory repair Namespaces
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ofac.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true));
// send output to a Stream
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
StreamResult sr = new StreamResult(buffer);
XMLStreamWriter w = null;
// write a duplicate Namespace Declaration
try {
w = ofac.createXMLStreamWriter(sr);
w.writeStartDocument();
w.writeStartElement("ns1", "foo", "http://example.com/");
w.writeNamespace("ns1", "http://example.com/");
w.writeNamespace("ns1", "http://example.com/");
w.writeEndElement();
w.writeEndDocument();
w.close();
} catch (XMLStreamException xmlStreamException) {
xmlStreamException.printStackTrace();
Assert.fail(xmlStreamException.toString());
}
// debugging output for humans
System.out.println();
System.out.println("actual: \"" + buffer.toString() + "\"");
System.out.println("expected: \"" + EXPECTED_OUTPUT + "\"");
// are results as expected?
Assert.assertEquals(EXPECTED_OUTPUT, buffer.toString());
}
示例4: testXMLOutputFactory
import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
public void testXMLOutputFactory() {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
success("testXMLOutputFactory passed");
}