SAX对xml的解析,javabean封装

book.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
	<书 评价="好书啊">
		<书名>Java就业培训教程</书名>
		<作者>张孝祥</作者>
		<售价>39.00元</售价>
		</书>
	<书>
		<书名>JavaScript网页开发</书名>
		<作者>黎活动</作者>
		<售价>28.00元</售价>
	</书>


</书架>


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.XMLReader;
import org.xml.sax.helpers.DefaultHandler;


public class demo {

	/**
	 * @param args
	 * @throws Exception 
	 * @throws ParserConfigurationException 
	 */
	public static void main(String[] args) throws ParserConfigurationException, Exception {
		// TODO Auto-generated method stub
		//创建工厂
		SAXParserFactory factory=SAXParserFactory.newInstance();
		//解析器
		SAXParser parser=factory.newSAXParser();
		//读取器
		XMLReader reader=parser.getXMLReader();
		//内容处理器
		reader.setContentHandler(new Hander());
		//读取xml内容
		reader.parse("src/book.xml");

	}

	static class Hander extends DefaultHandler
	{

		@Override //startElement
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
				System.out.println("<"+qName+">");
		}

		@Override//endElement
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
				System.out.println("<"+qName+">");
		}

		@Override//characters
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			 System.out.println(new String(ch,start,length));
		}
		
	}
}

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.XMLReader;
import org.xml.sax.helpers.DefaultHandler;


public class demo2 {
	//得到作者
	/**
	 * @param args
	 * @throws Exception 
	 * @throws ParserConfigurationException 
	 */
	public static void main(String[] args) throws ParserConfigurationException, Exception {
		// TODO Auto-generated method stub
		SAXParserFactory factory=SAXParserFactory.newInstance();
		SAXParser parser=factory.newSAXParser();
		XMLReader reader=parser.getXMLReader();
		reader.setContentHandler(new Hander());
		reader.parse("src/book.xml");

	}

	static class Hander extends DefaultHandler
	{
		private String markName;
		
		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
				markName=qName;
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
				
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
				 if(markName.equals("作者"))
				 {
					 System.out.println(new String(ch,start,length));
				 }
		}
		
	}
}

下面是利用javabean封装

book.java

public class book {

	private String name;
	private String author;
	private String price;

public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}


}
demo.java
import java.awt.List;
import java.util.ArrayList;

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.XMLReader;
import org.xml.sax.helpers.DefaultHandler;


public class demo3 {
	//javabean封装xml
	/**
	 * @param args
	 * @throws Exception 
	 * @throws ParserConfigurationException 
	 */
	@SuppressWarnings("unused")
	public static void main(String[] args) throws ParserConfigurationException, Exception {
		// TODO Auto-generated method stub
		SAXParserFactory factory=SAXParserFactory.newInstance();
		SAXParser parser=factory.newSAXParser();
		XMLReader reader=parser.getXMLReader();
		Hander hand=new Hander();
		reader.setContentHandler(hand);
		reader.parse("src/book.xml"); 
		ArrayList<book>list=hand.getList();
		System.out.println(list);

	}

	static class Hander extends DefaultHandler
	{
		private ArrayList<book> list=new ArrayList<book>();
		private String markName;
		book b;
		public ArrayList<book> getList()
		{
			return list;
		}
		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
				if(qName.equals("书"))
				{
					b=new book();
				}
				markName=qName;
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
				if(qName.equals("书"))
				{
					list.add(b);
					b=null;
				}
				markName=null;
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
				if(("书名").equals(markName))
				{
					b.setName(new String(ch,start,length));
				}
				else if(("作者").equals(markName))
				{
					b.setAuthor(new String(ch,start,length));
				}
				else if(("售价").equals(markName))
				{
					b.setPrice(new String(ch,start,length));
				}
		}
		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值