time 
设为首页】【收藏本站
当前位置: 主页 > JAVA > XML > 解析xml

解析xml

时间:2010-02-28 22:20 点击:1366次 字体:[ ]




1.获取xml的存放路径

   object 为xml的前缀名字 

package com.struts.common;

import java.net.URL;


public class BeansConstants {
	private BeansConstants() { 
	} 
	private static BeansConstants read = new BeansConstants(); 
	public static BeansConstants getInstance() { 
		return read; 
	} 

	public static String getPath(String object) { 
		String src ="com/struts/bean/"+object+".hbm.xml";
		URL url = BeansConstants.class.getClassLoader().getResource("");
		String xml = url.getPath() + src; 
		return xml; 
	} 
}

 2.通过dom4j解析xml的内容

    解析xml节的的为calss属性为table的内容

package com.struts.common;

import org.dom4j.Document;
import org.dom4j.Attribute;
import java.util.List;
import java.util.Iterator;
import java.io.*;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader; 
public class Dom4JParser{
	 public String modifyDocument(File inputXml){
		 String tablevalue="";
		 try{
			 SAXReader saxReader = new SAXReader();
			 Document document = saxReader.read(inputXml);
			 List list = document.selectNodes("//class/@table" );
			 Iterator iter=list.iterator();
			 while(iter.hasNext()){
				 Attribute attribute=(Attribute)iter.next();
				 tablevalue = attribute.getValue();
			 }
		 }catch(DocumentException e){
			 System.out.println(e.getMessage());
		 }
		return tablevalue;
     }
	 
	 public static void main(String[] argv){
		 System.out.println(DateUtil.getStandardDateTime());
		 String src = BeansConstants.getPath("TWarrantApply");
		 Dom4JParser dom4jParser=new Dom4JParser();
		 String tablevalue = dom4jParser.modifyDocument(new File(src));
		 System.out.println(tablevalue);
		 System.out.println(DateUtil.getStandardDateTime());
     }
}

 

上面事例解析时间需要2秒多。感觉是时间太了。但还找不到好的方法来缩短解析时间,如果谁好的方法请指教下

 

3.通过sax+jdom解析xml

public String getTableByObject(String object){
		//构造文档对象
		SAXBuilder sb=new SAXBuilder(); 
	    Document doc = null;
	    try {
			
			doc = (Document) sb.build(new FileInputStream(BeansConstants.getPath(object)));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		//获取根元素
	    Element root=doc.getRootElement(); 
	    
	    //获取子节点
	    Element child = root.getChild("class");
	    
	    String value = child.getAttributeValue("table");
	    
		return value;
    }
	
	public String[] getFieldByObject(String object,String firstChild,String secondChild){
		
		//构造文档对象
		SAXBuilder sb=new SAXBuilder(); 
	    Document doc = null;
	    try {
			
			doc = (Document) sb.build(new FileInputStream(BeansConstants.getPath(object)));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		//获取根元素
	    Element root=doc.getRootElement(); 
	    
	    //获取子节点
	    Element child = root.getChild(firstChild);
	    
	    //获取最小子元素
	    List list=child.getChildren(secondChild);
	    
	    int j =0;
	    for(int i=0;i<list.size();i++){ 
	    	Element element=(Element)list.get(i);
	    	String value1 = element.getAttributeValue("type");
		       if(value1.indexOf("String") >-1){
		    	   j++;
		       }
	    }
	    
	    //声明一个二为树组
	    String arraylist[] = new String[j];
	    
	    j =0;
	    //循环获取元素的健子对值,并存入二维数组
	    for(int i=0;i<list.size();i++){ 
	       Element element=(Element)list.get(i);
	       String value = element.getAttributeValue("name");
	       String value1 = element.getAttributeValue("type");
	       if(value1.indexOf("String") >-1){
	    	   arraylist[j] = value;
	    	   j++;
	       }
	       
	    }
	    
		return arraylist;   
	}

 

 

4.通过sax解析xml  获取下拉框

   selections.xml

<months>
		<month value="01">一月</month>
		<month value="02">二月</month>
		<month value="03">三月</month>
		<month value="04">四月</month>
		<month value="05">五月</month>
		<month value="06">六月</month>
		<month value="07">七月</month>
		<month value="08">八月</month>
		<month value="09">九月</month>
		<month value="10">十月</month>
		<month value="11">十一月</month>
		<month value="12">十二月</month>
	</months>
	<years>
		<year value="2008">2008</year>
		<year value="2009">2009</year>
		<year value="2010">2010</year>
		<year value="2011">2011</year>
	</years>
	<posts>
		<post value="01">经理</post>
		<post value="02">部长</post>
		<post value="03">销售员</post>
	</posts>

 

 

package org.kwb.furniture.common;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.net.URL;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;


public class Selection {
	private Selection() { 
	} 
	private static Selection read = new Selection(); 
	public static Selection getInstance() { 
	return read; 
	} 

	public static String getPath() { 
		URL url = Selection.class.getClassLoader().getResource(""); 
		String xml = url.getPath() + "/selections.xml"; 
		return xml; 
		} 

	public static String[][] getSelection(String selectname){
		
		//构造文档对象
		SAXBuilder sb=new SAXBuilder(); 
	    Document doc = null;
	    try {
			
			doc = sb.build(new FileInputStream(getPath()));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		//获取根元素
	    Element root=doc.getRootElement(); 
	    
	    Namespace ns = root.getNamespace();
	    
	    //根据名字获取跟元素下面的子元素
	    Element month = root.getChild(selectname,ns);
	    
	    //取名字的所有元素 
	    List list=month.getChildren();
	    
	    //声明一个二为树组
	    String arraylist[][] = new String[list.size()][2];
	    
	    //循环获取元素的健子对值,并存入二维数组
	    for(int i=0;i<list.size();i++){ 
	       Element element=(Element)list.get(i);
	       String key = element.getAttributeValue("value");
	       String value = element.getValue();
	       arraylist[i][0] = key;
	       arraylist[i][1] = value;
	    }
	    
		return arraylist;   
	}

}


本文地址 : http://www.fengfly.com/plus/view-170825-1.html
标签: XML
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码: