⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmloutputter.java

📁 一个java源代码包
💻 JAVA
字号:
import java.util.*;
import org.xml.sax.*;
import java.io.*;

public class XMLOutputter implements DocumentHandler
{

	private Writer writer=null;
	
	public void setDocumentLocator(Locator locator)
	{		
	}
	
	public void startDocument() throws SAXException
	{
		try
		{
			FileWriter fwriter=new FileWriter("aa.xml");
			writer=new BufferedWriter(fwriter);
			writer.write("<?xml version='1.0'>\n");
		}
		
		catch (java.io.IOException err)
		{
			throw new SAXException(err);
		}
	}
	
	public void endDocument() throws SAXException
	{
		
	}
	
	public void startElement(String name, AttributeList attributes)
	throws SAXException
	{
		try
		{
			writer.write("<");
			writer.write(name);
			
			for (int i=0;i<attributes.getLength();i++)
			{
				writer.write(" ");
				writeAttribute(attributes.getName(i),attributes.getValue(i));
			}
			writer.write(">");
		}
		catch(java.io.IOException err)
		{
			throw new SAXException(err);
		}
	}
	
	protected void writeAttribute(String attname,String value)throws SAXException
	{
		try
		{
			writer.write(attname);
			writer.write("='");
			char[] attval = value.toCharArray();
			char[] attesc = new char[value.length()*8];
			int newlen = escape(attval,0,value.length(),attesc);
			writer.write(attesc,0,newlen);
			writer.write("'");
		}
		catch(java.io.IOException err)
		{
			throw new SAXException(err);
		}
	}
	
	public void endElement(String name) throws SAXException
	{
		try
		{
			writer.write("</"+name+">");	
		}
		catch(java.io.IOException err)
		{
			throw new SAXException(err);
		}
	}
	
	public void characters(char[] ch, int start, int length) throws SAXException
	{
		try
		{
			char[] dest = new char[length*8];
			int newlen = escape(ch, start, length, dest);
			writer.write(dest,0,newlen);
		}
		catch(java.io.IOException err)
		{
			throw new SAXException(err);
		}
	}
	
	public void ignorableWhitespace(char[] ch, int start, int length)
	throws SAXException
	{
		characters(ch, start, length);
	}
	
	public void processingInstruction(String target, String data)
	throws SAXException
	{
		try
		{
			writer.write("<?" + target + ' ' + data + "?");
		}
		catch(java.io.IOException err)
		{
			throw new SAXException(err);
		}
	}
	
	private int escape(char ch[], int start, int length, char[] out)
	{
		int o=0;
		for(int i=start; i<start+length; i++)
		{
			if(ch[i]=='<')
			{
				("&lt;").getChars(0,4,out,o); o+=4;
			}
			else if(ch[i]=='>')
			{
				("&gt;").getChars(0,4,out,o); o+=4;
			}
			else if(ch[i]=='&')
			{
				("&amp;").getChars(0,5,out,o); o+=5;
			}
			else if(ch[i]=='\"')
			{
				("&#34;").getChars(0,5,out,o); o+=5;
			}
			else if(ch[i]=='\'')
			{
				("&#39;").getChars(0,5,out,o); o+=5;
			}
			else if(ch[i]<127)
			{
				out[o++]=ch[i];
			}
			else
			{
				out[o++]='&';
				out[o++]='#';
				String code = Integer.toString(ch[i]);
				int len = code.length();
				code.getChars(0,len,out,o); o+=len;
				out[o++]=';';				
			}
		}
		return o;	
		
	}
	
	public static void  main(String args[])throws Exception
	{	
		Indenter app = new Indenter(ParserManager.makeParser());
		app.setDocumentHanler(new XMLOutputter());
		app.parse(args[0]);
		
		/*try
		{
			XMLOutputter outer = new XMLOutputter();
			outer.startDocument();
			outer.writer.close();	
		}
		catch(IOException err)
		{
			System.out.println("the Exception is: "+err);
		}*/
	}

}

⌨️ 快捷键说明

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