Java Tutorial/XML/XMLOutputFactory

Материал из Java эксперт
Перейти к: навигация, поиск

XMLOutputFactory

   <source lang="java">

import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; public class DefaultNamespaceStreamOutput {

 public static void main(String[] args) throws Exception {
   XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
   XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
   writer.setDefaultNamespace("http://www.example.ru/ns1");
   writer.writeStartElement("http://www.example.ru/ns1", "sample");
   writer.writeDefaultNamespace("http://www.example.ru/ns1");
   writer.writeEmptyElement("inner");
   writer.setDefaultNamespace("http://www.example.ru/ns2");
   writer.writeStartElement("http://www.example.ru/ns2", "inner2");
   writer.writeDefaultNamespace("http://www.example.ru/ns2");
   writer.writeNamespace("ns1", "http://www.example.ru/ns1");
   writer.writeEmptyElement("http://www.example.ru/ns1", "inner");
   writer.writeEndDocument();
   writer.flush();
 }

}</source>