samplexmlwriter.java

来自「A sample code for the axiom usage」· Java 代码 · 共 79 行

JAVA
79
字号
package org.chinthaka.articles.stax;import javax.xml.stream.XMLOutputFactory;import javax.xml.stream.XMLStreamWriter;import javax.xml.stream.XMLStreamException;import java.io.OutputStream;/* * Copyright 2004,2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */public class SampleXMLWriter {    public void writeSampleXML(OutputStream outStream) {        try {            XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream);            // write the start element of "Article", with the namespace declaration            writer.writeStartElement("article", "Article", "http://www.article.org");            // write the article namespace            writer.writeNamespace("article", "http://www.article.org");            // write the author namespace            writer.writeNamespace("author", "http://author.org");            // write the comment            writer.writeComment("This sample1.xml is used for samples in \"Introducing StAX\" article");            // write the start element of Name element            writer.writeStartElement("Name");            // write characters            writer.writeCharacters("Introducing StAX");            // end element of Name element            writer.writeEndElement();            // write the start element of author element            writer.writeStartElement("author", "Author", "http://author.org");            // write characters            writer.writeCharacters("Eran Chinthaka");            // end element of Author element            writer.writeEndElement();            // write the processing instruction            writer.writeProcessingInstruction("This_is_a_processing_instruction");            // write the end element of Article element            writer.writeEndElement();            writer.flush();            writer.close();        } catch (XMLStreamException e) {            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.        }    }    public static void main(String[] args) {       SampleXMLWriter sampleWriter = new SampleXMLWriter();        sampleWriter.writeSampleXML(System.out);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?