javaWeb-sax解析xml文件

struct.xml

<?xml version="1.0" encoding="UTF-8"?>
<struts>
	<package>
		<action name="islogin" class="com.servlet.tran.UserAction" method="islogin">
			<result name="success">suc.jsp</result>
			<result name="input">err.jsp</result>
		</action>
		
		<action name="isreg" class="com.servlet.tran.UserAction" method="isreg">
			<result name="success">suc.jsp</result>
			<result name="nosave">reg.jsp</result>
			<result name="input">err.jsp</result>
			<result name="aaa">sss.jsp</result>
		</action>
	</package>
</struts>

action.java
package com.xmlsee.test;

import java.util.Map;

public class Action {
	private String name;
	private String classs;
	private String method;
	private Map<String, Result1> mapresult;
	@Override
	public String toString() {
		return "Action [name=" + name + ", classs=" + classs + ", method="
				+ method + ", mapresult=" + mapresult + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClasss() {
		return classs;
	}
	public void setClasss(String classs) {
		this.classs = classs;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public Map<String, Result1> getMapresult() {
		return mapresult;
	}
	public void setMapresult(Map<String, Result1> mapresult) {
		this.mapresult = mapresult;
	}
}
Result1.java

package com.xmlsee.test;

public class Result1 {
	private String name;
	private String text;
	@Override
	public String toString() {
		return "Result1 [name=" + name + ", text=" + text + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
}
MyHandlerStructs.java

package com.xmlsee.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;



public class MyHandlerStructs extends DefaultHandler {
//	用来装载解析后的数据(所有的Student对象-->list
//	单个对象stu
//	用来存放解析过程中的标签名(节点名)--tag

	private	Map<String, Action> mapres;
	private	Map<String, Result1> map1;
	private Action action;
	private Result1 result1;
	private String tag;
	/*fun
	 * 開始文檔
	 * 开始节点
	 * 結束節點
	 * 讀取內容
	 * 返回list
	 * 
	 * */
	
	@Override
		public void startDocument() throws SAXException {
				mapres =new HashMap<String, Action>();
				
		}
	/**
	 * 表示节点开始
	 */
	@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			// TODO Auto-generated method stub
			tag=qName;
			if(tag.equals("action")){
//				mapres =new HashMap<String, Action>();
				map1=new HashMap<String, Result1>();
				action =new Action();
				int count=attributes.getLength();
				for (int i = 0; i < count; i++) {
					String attname =attributes.getQName(i);
					String attvalue=attributes.getValue(i);
					if(attname.equals("name")){
						action.setName(attvalue);//将name添加到对象中
					}else if(attname.equals("class")){
						action.setClasss(attvalue);
					}else if(attname.equals("method")){
						action.setMethod(attvalue);
					}
				}
			}
			if(tag.equals("result")){
//				mapres =new HashMap<String, Action>();
				
				result1=new Result1();
				int count=attributes.getLength();
				for (int i = 0; i < count; i++) {
					String attname =attributes.getQName(i);
					String attvalue=attributes.getValue(i); 
					if(attname.equals("name")){
						result1.setName(attvalue);//将name添加到对象中
					}
				}
			}
			
		}
		/**
		 * 节点结束
		 */
		@Override
			public void endElement(String uri, String localName, String qName)
					throws SAXException {
				// TODO Auto-generated method stub
				tag="";
				/*
				 * 读到result结束节点把读到信息放入map1中,回去再创建一个result
				 * */
				if(qName.equals("result")){
					
					map1.put(result1.getName(), result1);
					action.setMapresult(map1);//把result的数据放入action的属性中

				}
				if(qName.equals("action")){
					mapres.put(action.getName(), action);
//					mapres=null;
				}
			}
		
		@Override
			public void characters(char[] ch, int start, int length)
					throws SAXException {                   
				// TODO Auto-generated method stub
				String content =new String(ch, start, length);
				if(tag.equals("result")){
					result1.setText(content);
				}
			}
			
			public Map<String, Action> getall(){
				return mapres;
			}

}

SaxStrutsTest.java
package com.xmlsee.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class SaxStrutsTest {
	public static void main(String[] args) {
		//1.創建sax解析工廠
		SAXParserFactory factory =SAXParserFactory.newInstance();
		//2.創建解析器對象
		try {
			SAXParser parser =factory.newSAXParser();
			//3.準備文件和輸入流
			String pathString="Struts.xml";
			InputStream is = SaxStrutsTest.class.getClassLoader().getResourceAsStream(pathString);
			//4.將輸入流和hander類給解析器對象
			MyHandlerStructs dh =new MyHandlerStructs();
			parser.parse(is, dh);
			Map<String,Action> map1 = dh.getall();
			
			
			Set<Entry<String,Action>> set = map1.entrySet();
			Iterator<Entry<String, Action>> iter =set.iterator();
			
			while(iter.hasNext()){
				Entry<String, Action> entry=iter.next();
				String key=entry.getKey();
				Action value=entry.getValue();
				System.out.println("action name:"+key+"------"+"action method:"+value.getMethod()+"----action class:"+value.getClasss()+"----result --:");
//				System.out.println(value.getMapresult());
			    Map<String, Result1>mapr=value.getMapresult();
				Set<Entry<String, Result1>> setr = mapr.entrySet();
				Iterator<Entry<String, Result1>> iterr=setr.iterator();
				while(iterr.hasNext()){
					Entry<String, Result1> entryr=iterr.next();
					System.out.println("result name:"+entryr.getKey()+"----text:"+entryr.getValue().getText());
				}
//				System.out.println("action method"+key+"------");
				
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值