f.setProperty(XMLOutputFactory2.P_AUTOMATIC_EMPTY_ELEMENTS,
Boolean.TRUE);
f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_NAMES,
Boolean.TRUE);
Writer w = new PrintWriter(System.out);
XMLStreamWriter2 sw = (XMLStreamWriter2) f.createXMLStreamWriter(w);
final String dtdStr =
"<!ELEMENT root (elem, elem3)>\n"
+"<!ATTLIST root attr CDATA #IMPLIED>\n"
+"<!ATTLIST root another CDATA #IMPLIED>\n"
+"<!ELEMENT elem ANY>\n"
+"<!ELEMENT elem3 ANY>\n"
;
XMLValidationSchemaFactory vd = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_DTD);
XMLValidationSchema schema = vd.createSchema(new StringReader(dtdStr));
//sw.validateAgainst(schema);
sw.writeStartDocument();
sw.writeComment("Comment!");
sw.writeCharacters("\r");
sw.writeStartElement("root?");
sw.writeAttribute("attr", "value");
sw.writeAttribute("another", "this & that");
//sw.writeAttribute("attr", "whatever"); // error!
sw.writeStartElement(null, "elem");
sw.writeCharacters("Sub-text");
sw.writeEndElement();
//sw.writeStartElement("elem3:foo"); // error, colon inside local name
sw.writeStartElement("elem3");
sw.writeEndElement();
//sw.writeCharacters("Root text <> ]]>\n");
sw.writeEndElement();
//sw.writeEmptyElement("secondRoot"); // error!
sw.writeCharacters("\n"); // white space in epilog
sw.writeProcessingInstruction("target", "some data");
sw.writeCharacters("\n"); // white space in epilog
sw.writeEndDocument();
sw.flush();
sw.close();
w.close();
}