📄 xmloutputter.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]=='<')
{
("<").getChars(0,4,out,o); o+=4;
}
else if(ch[i]=='>')
{
(">").getChars(0,4,out,o); o+=4;
}
else if(ch[i]=='&')
{
("&").getChars(0,5,out,o); o+=5;
}
else if(ch[i]=='\"')
{
(""").getChars(0,5,out,o); o+=5;
}
else if(ch[i]=='\'')
{
("'").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 + -