`
E路之吕
  • 浏览: 15063 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
读取xml中国家列表[没有xml,生成默认xml文件] java
public void loadCountry() {
		countrySelectItems.clear();
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder;
		Document doc = null;
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
			logger.info(" path of country file:" + FILE_PATH_OF_COUNTRY);
			File file = new File(FILE_PATH_OF_COUNTRY);
			if(!file.exists()){			//generate new file if isn't exist
				BuildXML();
			}
			doc = docBuilder.parse (file);
		} catch (Exception e) {
			e.printStackTrace();
		}
        doc.getDocumentElement().normalize();
        NodeList countryList = doc.getElementsByTagName("country");
        String countryKey = "";
        String englishName = "";
        String chineseName = "";
        for (int i = 0; i < countryList.getLength(); i++) {
        	Node node = countryList.item(i);
        	if(node.getNodeType() == Node.ELEMENT_NODE){
				Element countryCodeElement = (Element) node;
				
				NodeList countryKeyList = countryCodeElement.getElementsByTagName("country-key");
				countryKey = countryKeyList.item(0).getChildNodes().item(0).getNodeValue().trim();
			
				NodeList englishNameList = countryCodeElement.getElementsByTagName("english-name");
				englishName = englishNameList.item(0).getChildNodes().item(0).getNodeValue().trim();
				
				NodeList chineseNameList = countryCodeElement.getElementsByTagName("chinese-name");
				chineseName = chineseNameList.item(0).getChildNodes().item(0).getNodeValue().trim();
				
				countrySelectItems.add(new SelectItem(countryKey,englishName + (StringUtil.isNotEmpty(chineseName) ? "(" + chineseName + ")" : "")));
			}
		}
	}
	
	public void BuildXML() throws FileNotFoundException, IOException {
		List<Country> countryList = new ArrayList<Country>();
		countryList.add(new Country("86","CN","China","中国"));
		countryList.add(new Country("1","US","United States of America","美国"));
		countryList.add(new Country("44","GB","United Kingdom","英国"));
		
		org.jdom.Element root = new org.jdom.Element("countries");
		org.jdom.Document doc = new org.jdom.Document(root);
		root = doc.getRootElement();
		BuildXMLDoc(root,countryList);
		Format format = Format.getPrettyFormat();  
        XMLOutputter XMLOut = new XMLOutputter(format); 
        /**
         * /mnt/vishuo-data/common/countries.xml
         * [, mnt, vishuo-data, common, countries.xml]
         */
        String[] folders = FILE_PATH_OF_COUNTRY.split("/");
        String tmpPath = "";
        for (int i = 0; i < folders.length - 1; i++) {	//create folder
        	if(StringUtil.isNotEmpty(folders[i])){
        		tmpPath += "/" + folders[i] ;
        		File file = new File(tmpPath);
        		if(!file.exists() && !file.isDirectory()){   
        			logger.info(tmpPath + " not exist!!!");
        		    file.mkdir();    
        		}else{
        			logger.info(tmpPath + " has existed!!!");
        		}
        	}else{
        		continue;
        	}
		}
        FileOutputStream fos = new FileOutputStream(FILE_PATH_OF_COUNTRY);
        XMLOut.output(doc, fos);  
        if(fos != null)
        	fos.close();
	}
	
	
	private void BuildXMLDoc(org.jdom.Element element,List<Country> list) {
		List<Country> subList = list;
		for (Country country : subList) {
			org.jdom.Element subElement = new org.jdom.Element("country");       
			subElement.addContent(new org.jdom.Element("country-code").setText(country.getCountryCode()));    
			subElement.addContent(new org.jdom.Element("country-key").setText(country.getCountryKey()));
			subElement.addContent(new org.jdom.Element("english-name").setText(country.getEnglishName()));    
			subElement.addContent(new org.jdom.Element("chinese-name").setText(country.getChineseName()));    
			subElement = ((org.jdom.Element) element).addContent(subElement);
		}
	}
Java树形结构 java
	/**
	 * 引入richfaces-components-api-4.2.0.Final.jar包
	 */

	/**
	 * divison_code,divison_name,belong_to,is_leaf,path
	 * 110000,北京,86,0,86/110000/
	 * 110106,丰台区,110000,1,86/110000/110106
	 * 110108,海淀区,110000,1,86/110000/110108
	 * 340000,安徽,86,0,86/340000/
	 * 340100,合肥市,340000,1,86/340000/340100
	 * 340800,安庆市,340000,1,86/340000/340800
	 * 86,中国,root,0,86/
	 */
@Entity
@Table(name = "t_divison")
public class Divison implements Serializable {

	private static final long serialVersionUID = -8954403438095413490L;

	@Id
    @Column(name = "divison_code")
    private String divisonCode;

    @Column(name = "divison_name")
    private String divisonName;

    @Column(name = "belong_to")
    private String belongTo;
    
    @Column(name = "is_leaf",columnDefinition="varchar(2) default '0'")
    private String isLeaf;
    
    @Column(name = "path",length=255)
    private String path;

	public String getDivisonCode() {
		return divisonCode;
	}

	public void setDivisonCode(String divisonCode) {
		this.divisonCode = divisonCode;
	}

	public String getDivisonName() {
		return divisonName;
	}

	public void setDivisonName(String divisonName) {
		this.divisonName = divisonName;
	}

	public String getBelongTo() {
		return belongTo;
	}

	public void setBelongTo(String belongTo) {
		this.belongTo = belongTo;
	}

	public String getIsLeaf() {
		return isLeaf;
	}

	public void setIsLeaf(String isLeaf) {
		this.isLeaf = isLeaf;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
}

public class TreeBeanDavison extends TreeNodeImpl implements Serializable{
	
	private static final long serialVersionUID = 4872745664974841036L;
	
	private Divison data;
	
	public TreeBeanDavison() {
		super();
	}
	public TreeBeanDavison(boolean leaf, Object data) {
		this.data = (Divison) data;
	}
	public TreeBeanDavison(Object data) {
		this.data = (Divison) data;
	}
	public Divison getData() {
		return data;
	}

	public void setData(Divison data) {
		this.data = data;
	}
}
    /**实现**/
	private List<Divison> divisonList = new ArrayList<Divison>();
	
	TreeBeanDavison rootNode = new TreeBeanDavison(false,new Divison());

	private void getDivisonRootNode() {
		divisonList = statisticsQueryService.findByBelongTo("root");
		buildTree(rootNode,divisonList);
	}

	private void buildTree(TreeBeanDavison treeNode,List<Divison> list) {
		for (int i = 0; i < list.size(); i++) {
			Divison divison = list.get(i);
			boolean isLeaf = divison.getIsLeaf().equals(ISLEAF) ? true : false;
			TreeBeanDavison subTreeNode = new TreeBeanDavison(true,divison);
			treeNode.addChild(i, subTreeNode);
			if(!isLeaf){
				List<Divison> subList = statisticsQueryService.findByBelongTo(divison.getDivisonCode());
				buildTree(subTreeNode,subList);
			}
		}
	}
Java实现生成XML文件 java
	/**
	 * 引入jdom.jar包
	 */

	/**
	 * divison_code,divison_name,belong_to,is_leaf,path
	 * 110000,北京,86,0,86/110000/
	 * 110106,丰台区,110000,1,86/110000/110106
	 * 110108,海淀区,110000,1,86/110000/110108
	 * 340000,安徽,86,0,86/340000/
	 * 340100,合肥市,340000,1,86/340000/340100
	 * 340800,安庆市,340000,1,86/340000/340800
	 * 86,中国,root,0,86/
	 */

	private void newXML() {
		divisonList = statisticsQueryService.findByBelongTo("root");
		try {
			BuildXML();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public void BuildXML() throws FileNotFoundException, IOException{
		Element root = new Element("divisons");
		root.setAttribute("count", ""+divisonList.size());
		Document doc = new Document(root);
		root = doc.getRootElement();
		BuildXMLDoc(root,divisonList);
		Format format = Format.getPrettyFormat();  
        XMLOutputter XMLOut = new XMLOutputter(format); 
//        String filePath = this.getClass().getResource("/").getPath();
//        filePath = filePath.substring(1, filePath.indexOf("classes"));
        String filePath = "C:\\Users\\lxh\\Desktop\\divisons.xml";
        XMLOut.output(doc, new FileOutputStream(filePath));  
	}

    private void BuildXMLDoc(Element element,List<Divison> list) {
 		List<Divison> subList = list;
		for (Divison divison : subList) {
			Element subElement = new Element("sub-divison");       
			subElement.addContent(new Element("divison-code").setText(divison.getDivisonCode()));    
			subElement.addContent(new Element("divison-name").setText(divison.getDivisonName()));    
			if(!divison.getIsLeaf().equals(ISLEAF)){
				List<Divison> _subList = statisticsQueryService.findByBelongTo(divison.getDivisonCode());
				BuildXMLDoc(subElement,_subList);
			}
			subElement = element.addContent(subElement);
		}
	}
Java读取Properties文件 java
	private String driver;
	private String url;
	private String user;
	private String password;
	private Properties props = new Properties();

	
	private void loadResorces() {
		try {
			String filePath = this.getClass().getResource("/").getPath();
			filePath = filePath.substring(1, filePath.indexOf("classes"));
			FileInputStream inputFile = new FileInputStream(filePath + "jdbc.properties");//读取WEB-INF文件夹下的Properties文件
			//InputStream inputFile = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");//读取resources文件夹下的Properties文件
			props.load(inputFile);
			url = props.getProperty("jdbc.url");
			driver = props.getProperty("jdbc.driver");
			user = props.getProperty("jdbc.user");
			password = props.getProperty("jdbc.password"); 
			inputFile.close(); 
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
Global site tag (gtag.js) - Google Analytics