java类javax.xml.stream.events.StartDocument的实例源码

AnimalRepository.java 文件源码 项目:JavaMainRepo 阅读 22 收藏 0 点赞 0 评论 0
public void save(ArrayList<Animal> animals) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);
    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);

    for (XML_Parsable animal : animals) {
        StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.ANIMAL);
        eventWriter.add(sElement);
        eventWriter.add(end);
        animal.encodeToXml(eventWriter);

        EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.ANIMAL);
        eventWriter.add(eElement);
        eventWriter.add(end);
    }

    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}
EmployeeRepository.java 文件源码 项目:JavaMainRepo 阅读 20 收藏 0 点赞 0 评论 0
public void save(ArrayList<Employee> employees) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);
    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);

    for (XML_Parsable employee : employees) {
        StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.EMPLOYEE);
        eventWriter.add(sElement);
        eventWriter.add(end);
        employee.encodeToXml(eventWriter);

        EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.EMPLOYEE);
        eventWriter.add(eElement);
        eventWriter.add(end);
    }
    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}
EntityRepository.java 文件源码 项目:JavaMainRepo 阅读 25 收藏 0 点赞 0 评论 0
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);
    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);

    for (XML_Parsable entity : entities) {
        StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
        eventWriter.add(sElement);
        eventWriter.add(end);
        entity.encodeToXml(eventWriter);

        EndElement eElement = eventFactory.createEndElement("", "", this.entityTag);
        eventWriter.add(eElement);
        eventWriter.add(end);
    }

    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}
EntityRepository.java 文件源码 项目:JavaMainRepo 阅读 25 收藏 0 点赞 0 评论 0
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);
    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);

    for (XML_Parsable entity : entities) {
        StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
        eventWriter.add(sElement);
        eventWriter.add(end);
        entity.encodeToXml(eventWriter);

        EndElement eElement = eventFactory.createEndElement("", "", this.entityTag);
        eventWriter.add(eElement);
        eventWriter.add(end);
    }

    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}
XMLHelper.java 文件源码 项目:switchyard 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Create a document from the specified reader.
 * @param reader The XMLEvent reader.
 * @return The Document.
 * @throws ParserConfigurationException For errors creating the document.
 * @throws XMLStreamException For errors reading the event reader.
 */
public static Document createDocument(final XMLEventReader reader)
    throws ParserConfigurationException, XMLStreamException {
    final Document doc = getNewDocument();
    final XMLEventWriter writer = EVENT_WRITER_CREATOR.createXMLEventWriter(doc);
    final XMLEvent event = reader.peek();
    int type = event.getEventType();
    boolean omitDoc = false;
    if (type == XMLStreamConstants.START_DOCUMENT) {
        StartDocument startDocument = (StartDocument) event;
        if (startDocument.getVersion() == null) {
            omitDoc = true;
        }
    }
    XMLHelper.copyXMLEventStream(reader, writer, omitDoc);
    return doc;
}
Issue53Test.java 文件源码 项目:openjdk-jdk10 阅读 18 收藏 0 点赞 0 评论 0
@Test
public void testEncodingSet() {
    XMLEventFactory f = XMLEventFactory.newInstance();

    try {
        StartDocument sd = f.createStartDocument("UTF-8");
        System.out.println("Encoding: " + sd.getCharacterEncodingScheme());
        System.out.println("Encoding set: " + sd.encodingSet());
        Assert.assertTrue(sd.encodingSet(), "encoding is set, should return true.");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }

}
Issue53Test.java 文件源码 项目:openjdk-jdk10 阅读 16 收藏 0 点赞 0 评论 0
@Test
public void testStandaloneSet() {
    XMLEventFactory f = XMLEventFactory.newInstance();

    try {
        StartDocument sd = f.createStartDocument("UTF-8", "1.0", true);
        System.out.println(sd.isStandalone());
        System.out.println(sd.standaloneSet());
        Assert.assertTrue(sd.standaloneSet(), "standalone is set, should return true.");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }

}
Issue53Test.java 文件源码 项目:openjdk9 阅读 16 收藏 0 点赞 0 评论 0
@Test
public void testEncodingSet() {
    XMLEventFactory f = XMLEventFactory.newInstance();

    try {
        StartDocument sd = f.createStartDocument("UTF-8");
        System.out.println("Encoding: " + sd.getCharacterEncodingScheme());
        System.out.println("Encoding set: " + sd.encodingSet());
        Assert.assertTrue(sd.encodingSet(), "encoding is set, should return true.");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }

}
Issue53Test.java 文件源码 项目:openjdk9 阅读 18 收藏 0 点赞 0 评论 0
@Test
public void testStandaloneSet() {
    XMLEventFactory f = XMLEventFactory.newInstance();

    try {
        StartDocument sd = f.createStartDocument("UTF-8", "1.0", true);
        System.out.println(sd.isStandalone());
        System.out.println(sd.standaloneSet());
        Assert.assertTrue(sd.standaloneSet(), "standalone is set, should return true.");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }

}
XMLEventStreamReader.java 文件源码 项目:spring4-understanding 阅读 27 收藏 0 点赞 0 评论 0
@Override
public String getVersion() {
    if (this.event.isStartDocument()) {
        return ((StartDocument) this.event).getVersion();
    }
    else {
        return null;
    }
}
XMLEventStreamReader.java 文件源码 项目:spring4-understanding 阅读 24 收藏 0 点赞 0 评论 0
@Override
public boolean isStandalone() {
    if (this.event.isStartDocument()) {
        return ((StartDocument) event).isStandalone();
    }
    else {
        throw new IllegalStateException();
    }
}
XMLEventStreamReader.java 文件源码 项目:spring4-understanding 阅读 27 收藏 0 点赞 0 评论 0
@Override
public boolean standaloneSet() {
    if (this.event.isStartDocument()) {
        return ((StartDocument) this.event).standaloneSet();
    }
    else {
        throw new IllegalStateException();
    }
}
XMLEventStreamReader.java 文件源码 项目:spring 阅读 26 收藏 0 点赞 0 评论 0
@Override
public String getVersion() {
    if (this.event.isStartDocument()) {
        return ((StartDocument) this.event).getVersion();
    }
    else {
        return null;
    }
}
XMLEventStreamReader.java 文件源码 项目:spring 阅读 29 收藏 0 点赞 0 评论 0
@Override
public boolean isStandalone() {
    if (this.event.isStartDocument()) {
        return ((StartDocument) event).isStandalone();
    }
    else {
        throw new IllegalStateException();
    }
}
XMLEventStreamReader.java 文件源码 项目:spring 阅读 26 收藏 0 点赞 0 评论 0
@Override
public boolean standaloneSet() {
    if (this.event.isStartDocument()) {
        return ((StartDocument) this.event).standaloneSet();
    }
    else {
        throw new IllegalStateException();
    }
}
XMLEventFactoryImpl.java 文件源码 项目:javify 阅读 28 收藏 0 点赞 0 评论 0
public StartDocument createStartDocument(String encoding,
                                         String version,
                                         boolean standalone)
{
  return new StartDocumentImpl(location, null, encoding, version,
                               standalone, true, true);
}
XMLEventFactoryImpl.java 文件源码 项目:jvm-stm 阅读 25 收藏 0 点赞 0 评论 0
public StartDocument createStartDocument(String encoding,
                                         String version,
                                         boolean standalone)
{
  return new StartDocumentImpl(location, null, encoding, version,
                               standalone, true, true);
}
XMLEventStreamReader.java 文件源码 项目:teiid 阅读 29 收藏 0 点赞 0 评论 0
public boolean isStandalone() {
    if (event.isStartDocument()) {
        return ((StartDocument) event).isStandalone();
    }
    else {
        throw new IllegalStateException();
    }
}
XMLEventStreamReader.java 文件源码 项目:teiid 阅读 26 收藏 0 点赞 0 评论 0
public String getVersion() {
    if (event.isStartDocument()) {
        return ((StartDocument) event).getVersion();
    }
    else {
        return null;
    }
}
XMLEventStreamReader.java 文件源码 项目:teiid 阅读 22 收藏 0 点赞 0 评论 0
public boolean standaloneSet() {
    if (event.isStartDocument()) {
        return ((StartDocument) event).standaloneSet();
    }
    else {
        throw new IllegalStateException();
    }
}
EntityRepository.java 文件源码 项目:JavaMainRepo 阅读 25 收藏 0 点赞 0 评论 0
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    // Create and write start tag
    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);
    // Create content open tag
    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);

    for (XML_Parsable entity : entities) {
        StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
        eventWriter.add(sElement);
        eventWriter.add(end);

        entity.encodeToXML(eventWriter);

        EndElement eElement = eventFactory.createEndElement("", "", this.entityTag);
        eventWriter.add(eElement);
        eventWriter.add(end);
    }

    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}
EntityRepository.java 文件源码 项目:JavaMainRepo 阅读 24 收藏 0 点赞 0 评论 0
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));

    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");

    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);

    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);

    for (XML_Parsable entitity : entities) {
        StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
        eventWriter.add(sElement);
        eventWriter.add(end);

        entitity.encodeToXml(eventWriter);

        EndElement eElement = eventFactory.createEndElement("", "", this.entityTag);
        eventWriter.add(eElement);
        eventWriter.add(end);
    }

    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}
EntityRepository.java 文件源码 项目:JavaMainRepo 阅读 22 收藏 0 点赞 0 评论 0
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException {

        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        // Create XMLEventWriter
        XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));
        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent end = eventFactory.createDTD("\n");
        // Create and write Start Tag
        StartDocument startDocument = eventFactory.createStartDocument();
        eventWriter.add(startDocument);
        // Create content open tag
        StartElement configStartElement = eventFactory.createStartElement("", "", "content");
        eventWriter.add(configStartElement);
        eventWriter.add(end);

        for (XML_Parsable entity : entities) {
            StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
            eventWriter.add(sElement);
            eventWriter.add(end);

            entity.encodeToXml(eventWriter);

            EndElement eElement = eventFactory.createEndElement("", "", this.entityTag);
            eventWriter.add(eElement);
            eventWriter.add(end);

        }
        eventWriter.add(eventFactory.createEndElement("", "", "content"));
        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();
    }
EntityRepository.java 文件源码 项目:JavaMainRepo 阅读 23 收藏 0 点赞 0 评论 0
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException {

        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        // Create XMLEventWriter
        XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));
        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent end = eventFactory.createDTD("\n");
        // Create and write Start Tag
        StartDocument startDocument = eventFactory.createStartDocument();
        eventWriter.add(startDocument);
        // Create content open tag
        StartElement configStartElement = eventFactory.createStartElement("", "", "content");
        eventWriter.add(configStartElement);
        eventWriter.add(end);

        for (XML_Parsable entity : entities) {
            StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
            eventWriter.add(sElement);
            eventWriter.add(end);

            entity.encodeToXml(eventWriter);

            EndElement eElement = eventFactory.createEndElement("", "", this.entityTag);
            eventWriter.add(eElement);
            eventWriter.add(end);

        }
        eventWriter.add(eventFactory.createEndElement("", "", "content"));
        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();
    }
AnimalRepository.java 文件源码 项目:JavaMainRepo 阅读 25 收藏 0 点赞 0 评论 0
public void save(ArrayList<Animal> animals) throws FileNotFoundException, XMLStreamException {

        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        // Create XMLEventWriter
        XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent end = eventFactory.createDTD("\n");
        // Create and write Start Tag
        StartDocument startDocument = eventFactory.createStartDocument();
        eventWriter.add(startDocument);
        // Create content open tag
        StartElement configStartElement = eventFactory.createStartElement("", "", "content");
        eventWriter.add(configStartElement);
        eventWriter.add(end);

        for (XML_Parsable animal : animals) {
            StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.ANIMAL);
            eventWriter.add(sElement);
            eventWriter.add(end);

            animal.encodeToXml(eventWriter);

            EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.ANIMAL);
            eventWriter.add(eElement);
            eventWriter.add(end);

        }
        eventWriter.add(eventFactory.createEndElement("", "", "content"));
        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();
    }
EmployeeRepository.java 文件源码 项目:JavaMainRepo 阅读 27 收藏 0 点赞 0 评论 0
public void save(ArrayList<Employee> employees) throws FileNotFoundException, XMLStreamException {

        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        // Create XMLEventWriter
        XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent end = eventFactory.createDTD("\n");
        // Create and write Start Tag
        StartDocument startDocument = eventFactory.createStartDocument();
        eventWriter.add(startDocument);
        // Create content open tag
        StartElement configStartElement = eventFactory.createStartElement("", "", "content");
        eventWriter.add(configStartElement);
        eventWriter.add(end);

        for (XML_Parsable animal : employees) {
            StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.EMPLOYEE);
            eventWriter.add(sElement);
            eventWriter.add(end);

            animal.encodeToXml(eventWriter);

            EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.EMPLOYEE);
            eventWriter.add(eElement);
            eventWriter.add(end);

        }
        eventWriter.add(eventFactory.createEndElement("", "", "content"));
        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();
    }
AnimalRepository.java 文件源码 项目:JavaMainRepo 阅读 29 收藏 0 点赞 0 评论 0
public void save(ArrayList<Animal> animals) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    // Create XMLEventWriter
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
    // Create an EventFactory
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    // Create and write Start Tag
    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);
    // Create content open tag
    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);

    for (XML_Parsable animal : animals) {
        StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.ANIMAL);
        eventWriter.add(sElement);
        eventWriter.add(end);

        animal.encodeToXml(eventWriter);

        EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.ANIMAL);
        eventWriter.add(eElement);
        eventWriter.add(end);

    }
    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}
EmployeeRepository.java 文件源码 项目:JavaMainRepo 阅读 18 收藏 0 点赞 0 评论 0
public void save(ArrayList<Employee> employees) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    // Create XMLEventWriter
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
    // Create an EventFactory
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    // Create and write Start Tag
    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);
    // Create content open tag
    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);

    for (XML_Parsable employee : employees) {
        StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.EMPLOYEE);
        eventWriter.add(sElement);
        eventWriter.add(end);

        employee.encodeToXml(eventWriter);

        EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.EMPLOYEE);
        eventWriter.add(eElement);
        eventWriter.add(end);

    }
    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}
EntityRepository.java 文件源码 项目:JavaMainRepo 阅读 24 收藏 0 点赞 0 评论 0
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    // Create XMLEventWriter
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));
    // Create an EventFactory
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    // Create and write Start Tag
    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);
    // Create content open tag
    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);

    for (XML_Parsable entity : entities) {
        StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
        eventWriter.add(sElement);
        eventWriter.add(end);

        entity.encodeToXml(eventWriter);

        EndElement eElement = eventFactory.createEndElement("", "", this.entityTag);
        eventWriter.add(eElement);
        eventWriter.add(end);

    }
    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}
AnimalRepository.java 文件源码 项目:JavaMainRepo 阅读 27 收藏 0 点赞 0 评论 0
public void save(ArrayList<Animal> animals) throws FileNotFoundException, XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    // Create XMLEventWriter
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
    // Create a EventFactory
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    // Create and write Start Tag
    StartDocument startDocument = eventFactory.createStartDocument();
    eventWriter.add(startDocument);
    // Create content open tag
    StartElement configStartElement = eventFactory.createStartElement("", "", "content");
    eventWriter.add(configStartElement);
    eventWriter.add(end);
    for (XML_Parsable animal : animals) {
        StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.ANIMAL);
        eventWriter.add(sElement);
        eventWriter.add(end);
        animal.encodeToXml(eventWriter);
        EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.ANIMAL);
        eventWriter.add(eElement);
        eventWriter.add(end);
    }
    eventWriter.add(eventFactory.createEndElement("", "", "content"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
}


问题


面经


文章

微信
公众号

扫码关注公众号