Java Tutorial/XML/XMLStreamWriter — различия между версиями

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

Текущая версия на 05:17, 1 июня 2010

Create empty element from XMLStreamWriter

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
public class EmptyElement {
  public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeEmptyElement("empty");
    writer.writeAttribute("attribute", "true");
    writer.writeEndDocument();
    writer.flush(); // write /> to the console
  }
}





XMLStreamWriter

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
public class AttributeStreamOutput {
    public static void main(String[] args) throws Exception {
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
        writer.writeStartDocument("1.0");
        writer.writeStartElement("addresses");
        writer.writeStartElement("address");
        writer.writeAttribute("type", "work");
        writer.writeStartElement("street");
        writer.writeCharacters("1111 Ave");
        writer.writeComment("comments");
        writer.writeEndElement();
        writer.writeEmptyElement("inner");
        writer.flush();
        System.out.println();
        writer.writeAttribute("otherAttribute", "true");
        writer.flush();
        System.out.println();
        writer.writeEndElement();
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
    }
}