本文整理汇总了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");
}