import javax.xml.parsers.SAXParser;
mport javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader;
import java.io.PrintStream;
mport org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
/** SAX Application to beautify XML documents */
public class XMLPrettyApp {
static public void main( String[] args ) throws Exception {
// The XML document is supplied as the first argument
String myXMLFileURL = args[0];
// Step 1: Create a new instance of SAXParserFactory
SAXParserFactory factory = SAXParserFactory.newInstance();
// Step 2: (Optional) Set configuration options
factory.setValidating(false);
// Step 3: Create a JAXP SAX Parser
SAXParser saxParser = factory.newSAXParser();
// Step 4: Get an encapsulated XMLReader
XMLReader xmlReader = saxParser.getXMLReader();
// Step 5: Set a Content Handler for events
XMLPrettyEvents xmlHndlr = new XMLPrettyEvents( System.out );
xmlReader.setContentHandler( xmlHndlr );
// Step 6: Set an Error Handler
XMLPrettyErrors errHndlr = new XMLPrettyErrors( System.err );
xmlReader.setErrorHandler( errHndlr );
// Step 7: Begin the Parsing process
xmlReader.parse( myXMLFileURL );
}
}
/** Simple class to reformat an XML document */
public class XMLPrettyEvents extends DefaultHandler {
private PrintStream out; // Normal Output
private int offset; // Indentation index
private int elementCount; // Count of elements
/** Simple constructor that defines an output stream */
MyEventHandler( PrintStream out ) {
this.out = out;
}
/** Print results with indentation */
private void print( String msg ) {
out.println( getIndent() + msg );
}
/** Return indent based on offset value */
private String getIndent() {
StringBuffer indent = new StringBuffer();
for (int i = 0; i < offset; i++) {
indent.append(" ");
}
return indent.toString();
}
/** Initialize variables on document start */
public void startDocument() {
print("Start of Document");
offset = 0;
}
/** Display tally on document end */
public void endDocument() {
}
/** Display the element start tag */
public void startElement( String uri, String localname,
String qName, Attributes attrs) {
// Build a representative start tag
String e = "<" + qName;
for (int x = 0; x < attrs.getLength(); x++) {
e += " "+attrs.getQName(x)+"='"+attrs.getValue(x)+"'";
}
e += ">";
print(e); // Output the start tag info
off
}
/** Display the closing tag for the element */
public void endElement( String uri, String localname,
String qName ) {
offset--; // Decrement the offset pointer
elementCount++; // Increment the element counter
print("</" + qName + ">"); // Output the end tag info
}
/** Output the text block inside an element */
public void characters( char[] ch, int start, int length ) {
// Builds a string based on char array
StringBuffer sb = new StringBuffer();
sb.append( ch, start, length );
// Output resulting text value
print( sb.toString() );
}
}
/** My Custom XML Error Handler Class */
public class MyErrorHandler implements ErrorHandler {
private PrintStream out;
/** Constructor - Set output stream for warnings */
MyErrorHandler( PrintStream out ) {
this.out = out;
}
/** Warnings - Non fatal, log it and continue */
public void warning(SAXParseException spe) throws SAXException {
out.println("Warning: " + spe.getMessage());
}
/** Errors - Throw an exception with error info */
public void error(SAXParseException spe) throws SAXException {
throw new SAXException( "Error: " + getExceptionInfo(spe) );
}
/** FatalErrors - Throw an exception with fatal error info */
public void fatalError(SAXParseException spe) throws SAXException
{
throw new SAXException( "Fatal: " + getExceptionInfo(spe) );
}
/** Gather some Exception information */
private String getExceptionInfo(SAXParseException spe) {
return "URL=" + spe.getSystemId()
+ " Line=" + spe.getLineNumber() + ": " + spe.getMessage();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
XMl 教程 ppt加实例 (447个子文件)
index.asp 2KB
getcontent.asp 2KB
deletedata.asp 2KB
code9_10.asp 2KB
SAVE.ASP 1KB
code11_3.asp 1KB
suredelete.asp 1KB
code11_7.asp 1KB
code9_4.asp 830B
code11_2.asp 661B
TransOnServer.asp 634B
code11_1.asp 607B
code11_8.asp 521B
code9_5.asp 411B
cdcatalog.asp 317B
code11_4.asp 216B
cdcatalog_server.asp 159B
code9_6.html.bak 591B
code9_8b.html.bak 401B
books.xml.bak 0B
code10_3.class 3KB
code10_4.class 2KB
code10_2.class 2KB
code10_1.class 2KB
code10_5.class 960B
css.css 12KB
style.css 4KB
code10_2.dtd 518B
code10_3.dtd 518B
test.dtd 385B
fclml.dtd 371B
code4_15.dtd 336B
fclml.dtd 273B
code4_18_1.dtd 272B
code4_18.dtd 253B
code4_16_1.dtd 224B
code4_18_3.dtd 221B
code4_18_2.dtd 193B
code4_16_2.dtd 176B
SVGView303.exe 2.28MB
SVGView.exe 2.25MB
editplus.exe 1.25MB
eg_cute.gif 17KB
banner_fazhanguan.gif 15KB
title_ggxx.gif 3KB
title_xsjz.gif 3KB
title_xngg.gif 3KB
title_xsdt.gif 3KB
title_xykx.gif 3KB
count.gif 3KB
title_xntz.gif 3KB
title_xstd.gif 3KB
title_jxxx.gif 3KB
bottomarrow.gif 2KB
night.gif 1KB
day.gif 802B
rss.gif 249B
new.gif 225B
index_13.gif 187B
bg_li.gif 108B
icon.gif 54B
ystat.gif 43B
校内办公网.htm 29KB
[欢迎光临吉林大学].htm 11KB
leavedata.htm 3KB
code7_9.HTM 3KB
code7_9.HTM 3KB
leavemessage.htm 3KB
code9_2.htm 2KB
code1_1.htm 1KB
code7_3.HTM 1KB
code7_3.HTM 1KB
code7_6.HTM 1KB
code7_8.HTM 1KB
code7_5.HTM 1KB
code7_6.HTM 1KB
code7_5.HTM 1KB
code7_7.HTM 1KB
code7_8.HTM 1KB
code7_4.HTM 938B
code7_4.HTM 938B
code7_7.HTM 810B
loadXmlStr.htm 701B
code9_1.htm 666B
code7_1.HTM 642B
brand.htm 622B
code7_1.HTM 576B
code9_3.htm 567B
code7_2.HTM 462B
code7_2.HTM 462B
code10_5.html 3KB
xmlhttp.html 1KB
xpath01.html 1KB
main.html 1KB
xpath.html 1KB
noNameSpaceValidation.html 1KB
noNameSpaceValidation.html 1KB
code2_12.html 1KB
code2_12.html 1KB
students.HTML 965B
共 447 条
- 1
- 2
- 3
- 4
- 5
资源评论
changjianping
- 粉丝: 19
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功