XML主要用于数据交换,HTML则用于显示。
相对于DOM的树形解析,SAX采用的是顺序解析,这种解析方法可以快速地读取XML数据的方式。
SAX主要事件:
No. | 方法 | 类型 | 描述 |
1 | public void startDocument() throws SAXException | 普通 | 文档开始 |
2 | public void endDocument() throws SAXException | 普通 | 文档结束 |
3 |
public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException |
普通 | 元素开始,可以取得元素的名称及元素的全部属性 |
4 | public void endElement(String uri,String localName,String qName) throws SAXException | 普通 | 元素结束,可以取得元素的名称及元素的全部属性 |
5 | public void characters(char[] ch,int start,int length) throws SAXException | 普通 | 元素内容 |
SAX解析步骤:
===========>startDocument()
<?xml version="1.0" encoding="GBK"?> <addresslist> ===========>startElement("addresslist") <linkman> ===========>startElement("linkman") <name> ===========>startElement("name") 李四 ============>characters("李四") </name> ============>endElement("name") <email> ============>startElement("email") 111@qq.com ============>characters("111@qq.com") </email> ============>endElement("email") </linkman> ============>endElement("linkman") </addresslist> ============>endElement("addresslist")
============>endDocument()
使用SAX解析器解析需要自己构造解析器:
1 package SAX; 2 3 import org.xml.sax.Attributes; 4 import org.xml.sax.SAXException; 5 import org.xml.sax.helpers.DefaultHandler; 6 public class MYSAX extends DefaultHandler{ 7 public void startDocument() throws SAXException{ 8 System.out.println("<?xml version="1.0" encoding="GBK"?>"); 9 } 10 public void endDocument() throws SAXException{ 11 System.out.println(" 文档读取结束!"); 12 } 13 public void startElement(String url,String localName,String name,Attributes attributes) throws SAXException{ 14 System.out.println("<"); 15 System.out.println(name); 16 if(attributes!=null){ 17 for(int x=0;x<attributes.getLength();x++){ 18 System.out.println(""+attributes.getQName(x)+"=""+attributes.getValue(x)+"""); 19 } 20 } 21 System.out.println(">"); 22 } 23 public void characters(char[] ch,int start,int length) throws SAXException{ 24 System.out.println(new String(ch,start,length)); 25 } 26 public void endElement(String uri,String localName,String name) throws SAXException{ 27 System.out.println("</"); 28 System.out.println(name); 29 System.out.println(">"); 30 } 31 }
然后利用解析器读取XML文件:
1 import java.io.File; 2 import javax.xml.parsers.SAXParser; 3 import javax.xml.parsers.SAXParserFactory; 4 public class TestSAX { 5 public static void main(String[] args) throws Exception{ 6 SAXParserFactory factory=SAXParserFactory.newInstance(); //建立SAX解析器 7 SAXParser parser=factory.newSAXParser(); //构造解析器 8 parser.parse("d:"+File.separator+"output.xml",new MYSAX()); 9 } 10 }
结合了SAX的快速读取的优点和DOM可以任意访问节点的优点而成的JDOM包:
生成一个XML文件的代码:
1 package SAX; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import org.jdom.Attribute; 6 import org.jdom.Document; 7 import org.jdom.Element; 8 import org.jdom.output.XMLOutputter; 9 public class JDOM { 10 public static void main(String [] args){ 11 Element addresslist = new Element("addresslist"); 12 Element linkman = new Element("linkman"); 13 Element name = new Element("name"); 14 Element email = new Element("email"); 15 Attribute Id = new Attribute("id","lxn"); 16 Document doc = new Document(addresslist); 17 name.setText("lisi"); 18 email.setText("123@qq.com"); 19 name.setAttribute(Id); 20 linkman.addContent(name); 21 linkman.addContent(email); 22 addresslist.addContent(linkman); 23 XMLOutputter out = new XMLOutputter(); 24 out.setFormat(out.getFormat().setEncoding("GBK")); 25 try{ 26 out.output(doc,new FileOutputStream("d:"+File.separator+"addresslist.xml")); 27 }catch(Exception e){ 28 e.printStackTrace(); 29 } 30 } 31 }
使用JDOM读取XML文件:
package SAX; import java.io.File; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; public class JDOM{ public static void main(String[] args) throws Exception{ SAXBuilder builder = new SAXBuilder(); Document read_doc = builder.build("d:"+File.separator+"addresslist.xml"); Element stu = read_doc.getRootElement(); List list = stu.getChildren("linkman"); for(int i=0;i<list.size();i++){ Element e=(Element)list.get(i); String name = e.getChildText("linkman"); String id = e.getChild("name").getAttribute("id").getValue(); String email = e.getChildText("email"); System.out.println("——联系人——"); System.out.println("姓名:"+name+",编号:"+id); System.out.println("email:"+email); System.out.println("————"); System.out.println(); } } }
运行结果:
——联系人——
姓名:null,编号:lxn
email:123@qq.com
————