SA相比DOM有如下好处:不用全部加载完xml后才能开始解析,可以一边加载一便解析,提高效率
package
sax;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxPrinter extends DefaultHandler ... {
public static void main(String[] args) ...{
String realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"dom"+File.separator+"student.xml";
SAXParserFactory spf=SAXParserFactory.newInstance();
try ...{
SAXParser parser=spf.newSAXParser();
parser.parse(new File(realpath), new SaxPrinter());
} catch (ParserConfigurationException e) ...{
e.printStackTrace();
} catch (SAXException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException ...{
//输出xml文档格式,如空格
System.out.print(new String(ch,start,length));
}
public void endElement(String uri, String localName, String name)
throws SAXException ...{
System.out.print("</"+name+">");
}
public void processingInstruction(String target, String data)
throws SAXException ...{
System.out.println("<?"+target+" "+data+"?>");
}
public void startDocument() throws SAXException ...{
//处理指令,如<?xml-stylesheet type="text/xsl" href="student.xsl"?>
System.out.println("<?xml version="1.0" encoding='gb2312'/?>");
}
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException ...{
System.out.print("<"+name);
int len=attributes.getLength();
for (int i = 0; i < len; i++) ...{
System.out.print(" ");
System.out.print(attributes.getQName(i));
System.out.print("="");
System.out.print(attributes.getValue(i));
System.out.print(""");
}
System.out.print(">");
System.out.print("");
}
}
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxPrinter extends DefaultHandler ... {
public static void main(String[] args) ...{
String realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"dom"+File.separator+"student.xml";
SAXParserFactory spf=SAXParserFactory.newInstance();
try ...{
SAXParser parser=spf.newSAXParser();
parser.parse(new File(realpath), new SaxPrinter());
} catch (ParserConfigurationException e) ...{
e.printStackTrace();
} catch (SAXException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException ...{
//输出xml文档格式,如空格
System.out.print(new String(ch,start,length));
}
public void endElement(String uri, String localName, String name)
throws SAXException ...{
System.out.print("</"+name+">");
}
public void processingInstruction(String target, String data)
throws SAXException ...{
System.out.println("<?"+target+" "+data+"?>");
}
public void startDocument() throws SAXException ...{
//处理指令,如<?xml-stylesheet type="text/xsl" href="student.xsl"?>
System.out.println("<?xml version="1.0" encoding='gb2312'/?>");
}
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException ...{
System.out.print("<"+name);
int len=attributes.getLength();
for (int i = 0; i < len; i++) ...{
System.out.print(" ");
System.out.print(attributes.getQName(i));
System.out.print("="");
System.out.print(attributes.getValue(i));
System.out.print(""");
}
System.out.print(">");
System.out.print("");
}
}
测试用的xml
<?
xml version="1.0" encoding="UTF-8"
?>
<? xml-stylesheet type="text/xsl" href="student.xsl" ?>
< students >
< student sn ="01" >
< name > gao </ name >
< age > 27 </ age >
</ student >
< student sn ="02" >
< name > yu </ name >
< age > 24 </ age >
</ student >
</ students >
<? xml-stylesheet type="text/xsl" href="student.xsl" ?>
< students >
< student sn ="01" >
< name > gao </ name >
< age > 27 </ age >
</ student >
< student sn ="02" >
< name > yu </ name >
< age > 24 </ age >
</ student >
</ students >
运行结果:
<?xml version="1.0" encoding='gb2312'/?>
<?xml-stylesheet type="text/xsl" href="student.xsl"?>
<students>
<student sn="01">
<name>gao</name>
<age>27</age>
</student>
<student sn="02">
<name>yu</name>
<age>24</age>
</student>
</students>