john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

xml stax

//2012-03-06  Be careful as Stax can create malformed or invalid xml
import java.io.File;
import java.io.FileWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

public class XMLParser
{
    public static void main( String args[] )
    {
        //writeXML( "todo" );
    }


    protected void writeXML( String str )
    {
        FileWriter output = null;
        File file = null;
        XMLOutputFactory outputFactory = null;
        XMLStreamWriter xsw = null;
        String filename = "/home/ubuntu/Desktop/temp.xml";
        try{

            file = new File( filename );
            output = new FileWriter( file );
          outputFactory = XMLOutputFactory.newInstance();
          xsw = outputFactory.createXMLStreamWriter( output );

//        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
//        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            xsw.writeStartDocument( "UTF-8" , "1.0" );
            xsw.setPrefix("html", "http://www.w3.org/TR/REC-html40");
            xsw.writeStartElement("http://www.w3.org/TR/REC-html40","html");
            xsw.writeNamespace("html", "http://www.w3.org/TR/REC-html40");
            xsw.writeStartElement("http://www.w3.org/TR/REC-html40","head");
            xsw.writeStartElement("http://www.w3.org/TR/REC-html40","title");
            xsw.writeCharacters("Frobnostication");
            xsw.writeEndElement();
            xsw.writeEndElement();
            xsw.writeStartElement("http://www.w3.org/TR/REC-html40","body");
            xsw.writeStartElement("http://www.w3.org/TR/REC-html40","p");
            xsw.writeCharacters("Moved to");
            xsw.writeStartElement("http://www.w3.org/TR/REC-html40","a");
            xsw.writeAttribute("href","http://frob.com");
            xsw.writeCharacters("here");
            xsw.writeEndElement();
            xsw.writeEndElement();
            xsw.writeEndElement();
            xsw.writeEndElement();
            xsw.writeEndDocument();
            xsw.flush();
            xsw.close();

            System.out.println( filename + " written" );
            //XMLInputFactory inputFactory = XMLInputFactory.newInstance();

        }catch( Exception e )
        {
            e.printStackTrace();
        }

    }



} //end class

  • « Nfs network share
  • Perl function sub parameters »

Published

Mar 7, 2012

Category

java

~113 words

Tags

  • java 252
  • stax 1
  • xml 22