Trying the following code..
=================================
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);
}
}
=================================
It�s throwing build-time error.
C:\java\jdk1.5.0_06\bin\javac -source 1.5 SampleXMLWriter.java
SampleXMLWriter.java:3: package javax.xml.stream does not exist
import javax.xml.stream.XMLOutputFactory;
^
SampleXMLWriter.java:4: package javax.xml.stream does not exist
import javax.xml.stream.XMLStreamWriter;
^
SampleXMLWriter.java:5: package javax.xml.stream does not exist
import javax.xml.stream.XMLStreamException;
^
SampleXMLWriter.java:26: cannot find symbol
symbol : class XMLStreamWriter
location: class org.chinthaka.articles.stax.SampleXMLWriter
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStr
eamWriter(outStream);
^
SampleXMLWriter.java:26: cannot find symbol
symbol : variable XMLOutputFactory
location: class org.chinthaka.articles.stax.SampleXMLWriter
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStr
eamWriter(outStream);
^
SampleXMLWriter.java:68: cannot find symbol
symbol : class XMLStreamException
location: class org.chinthaka.articles.stax.SampleXMLWriter
} catch (XMLStreamException e) {