A simplest XML SAX Writer.

xiaoxiao2021-03-06  44

Java's XML package, no found a package that provides SAX Writer, just need to output an XML file in SAX, so you write one, contain approximately 100 lines, and it is quite simple. / * * CREATED ON 2005-1-29 * * mininal SAX Writer. You can use it like this: * xmlwriter.StartDocument (); * ... * xmlwriter.StartElement ("book"); * xmlwriter.addattribute (" Title "," Book Title "); * XMLWriter.Addattribute (" Price "," $ 100 "); * XMLWRITER.CHARACTER (" this is a good book "); * xmlwriter.enDelement (" book "); * .. * Xmlwriter.enddocument (); * / package com.dyz.xfe;

Import java.io. *;

public class XmlWriter {private BufferedWriter writer = null; private StringBuffer buffer = new StringBuffer (); private boolean isRoot = true; private boolean closed = false; public XmlWriter (String filename) {try {writer = new BufferedWriter (new FileWriter (filename) );} Catch (ooException e) {E.PrintStackTrace ();}} public void startdocument ()} public void startDocument ()} {write = / "1.0 /"?> ");} Catch (IOException E ) {E.PrintStackTrace ();}} public void enddocument () {● {i (buffer.Length ()> Writer.close ()); writer.close ();} catch (ooexception e ) {E.PrintStackTrace ();}} public void startElement (String elem) {if (isroot) isroot = false; else {if (! Closed) buffer.Append (">");} if (buffer.length () > 409 6) {Try {Writer.write (buffer.tostring ());} catch (ioException e) {E.printStackTrace ();} buffer.delete (0, buffer.Length ());} buffer.Append ("< " elem); closed = false;} public void endelement (string elem) {if (! closed) buffer.append ("> "); buffer.append (" "); closed = True;} / ** * Output a xml attribute. * @Param name attribute name. * @

转载请注明原文地址:https://www.9cbs.com/read-56236.html

New Post(0)