`

采用Dom4j读取XML 并把相应数据存放到VO中 最后存入到集合中

阅读更多

首先我把xml中的内容贴出来

 

<?xml version="1.0" encoding="UTF-8"?>
<globle-info>
    <info-1>
	<Devtype>0</Devtype>
	<IPAddress>192.168.0.225</IPAddress>
	<ServerPort>5050</ServerPort>
	<Password>admin</Password>
	<Username>admin</Username>
	</info-1>
	
	<info-2>
	<Devtype>1</Devtype>
	<IPAddress>192.168.0.225</IPAddress>
	<ServerPort>5050</ServerPort>
	<Password>admin</Password>
	<Username>admin</Username>
	</info-2>

    <info-3>
	<Devtype>2</Devtype>
	<IPAddress>192.168.0.225</IPAddress>
	<ServerPort>5050</ServerPort>
	<Password>admin</Password>
	<Username>admin</Username>
    </info-3>
    
    <info-4>
	<Devtype>3</Devtype>
	<IPAddress>192.168.0.225</IPAddress>
	<ServerPort>5050</ServerPort>
	<Password>admin</Password>
	<Username>admin</Username>
	</info-4>

     <info-5>
	<Devtype>4</Devtype>
	<IPAddress>192.168.0.225</IPAddress>
	<ServerPort>5050</ServerPort>
	<Password>admin</Password>
	<Username>admin</Username>
     </info-5>

    <info-6>
	<Devtype>5</Devtype>
	<IPAddress>192.168.0.225</IPAddress>
	<ServerPort>5050</ServerPort>
	<Password>admin</Password>
	<Username>admin</Username>
	</info-6>
	
	<info-7>
	<Devtype>6</Devtype>
	<IPAddress>192.168.0.225</IPAddress>
	<ServerPort>5050</ServerPort>
	<Password>admin</Password>
	<Username>admin</Username>
	</info-7>
	
	<info-8>
	<Devtype>7</Devtype>
	<IPAddress>192.168.0.225</IPAddress>
	<ServerPort>5050</ServerPort>
	<Password>admin</Password>
	<Username>admin</Username>
    </info-8>

</globle-info>

 

 

 

 

导入dom4j.jar后 开始编写vo类 与测试入口类

 

vo类:

 

package com.bh.readxml;

public class ConnectionInfo {
	private String devType;
	private String ipAddress;
	private String serverPort;
	private String password;
	private String username;

	public String getDevType() {
		return devType;
	}

	public void setDevType(String devType) {
		this.devType = devType;
	}


	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getServerPort() {
		return serverPort;
	}

	public void setServerPort(String serverPort) {
		this.serverPort = serverPort;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}

 

 

程序入口类:

 

package com.bh.readxml;



import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class ReadXML
{
	public ReadXML()
	{
	}


	public List readTestXML()
	{
		List list = new ArrayList();
		SAXReader reader = new SAXReader();
		URL xmlpath = this.getClass().getClassLoader().getResource("Connetion.xml");
		try
		{
			Document document = reader.read(xmlpath);
			Element root = document.getRootElement();
		  //遍历子元素
		
			for (Iterator i = root.elementIterator(); i.hasNext();) {
				Element employee = (Element) i.next();
				ConnectionInfo connectionInfo = new ConnectionInfo();
				connectionInfo.setDevType(employee.element("Devtype").getText());
				connectionInfo.setIpAddress(employee.element("IPAddress").getText());
				connectionInfo.setPassword(employee.element("Password").getText());
				connectionInfo.setServerPort(employee.element("ServerPort").getText());
				connectionInfo.setUsername(employee.element("Username").getText());
				list.add(connectionInfo);
			}
			
		   /* for(int i = 0;i < list.size();i++){
		    	ConnectionInfo vo = (ConnectionInfo)list.get(i);
		    	System.out.println(vo.getDevType());
		    	System.out.println(vo.getIPAddress());
		    	System.out.println(vo.getUsername());
		    	System.out.println(vo.getIPAddress());
		    	System.out.println(vo.getServerPort());
		    }*/
		
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
		}
		return list;
	}

	 public static void main(String[] args) {
		ReadXML r = new ReadXML();
		r.readTestXML();
	}
}

 

 

就ok了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics