**读取xml格式的文件**
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class ReadXml {
public static void main(String arge[]) {
long lasting = System.currentTimeMillis();
try {
File f = new File("C:\\Users\\20433\\Desktop\\sxfdemo.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("VALUE");
for (int i = 0; i < nl.getLength(); i++) {
System.out.print("车牌号码:"+ doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());
System.out.println("车主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
读取json格式的数据
param=stu.getSchool();
String result=sendPost(url,param);
//解析json代码
json = JSONObject.fromObject(result);
System.out.println(json.getString(“errno”));
System.out.println(result);
if(Integer.parseInt(json.getString(“errno”))==0) {
System.out.println(“Test Pass”);
}else {
System.out.println(“Test Fail”);
}
读取excel格式的文件
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
public class POIReadExcelTool {
public static void main(String[] args) throws Exception {
List<Student> list = readXls();
for(Student stu : list){
System.out.println(stu.getId());
System.out.println(stu.getName());
System.out.println(stu.getAge());
System.out.println(stu.getBirth());
}
}
public static List<Student> readXls() throws Exception {
InputStream is = new FileInputStream("D:/info.xls");
HSSFWorkbook excel = new HSSFWorkbook(is);
Student stu = null;
List<Student> list = new ArrayList<Student>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < excel.getNumberOfSheets(); numSheet++) {
HSSFSheet sheet = excel.getSheetAt(numSheet);
if (sheet == null)
continue;
// 循环行Row
for (int rowNum = 1; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow row = sheet.getRow(rowNum);
if (row == null)
continue;
stu = new Student();
HSSFCell cell0 = row.getCell(0);
if (cell0 == null)
continue;
stu.setId((int)cell0.getNumericCellValue());
HSSFCell cell1 = row.getCell(1);
if (cell1 == null)
continue;
stu.setName(cell1.getStringCellValue());
HSSFCell cell2 = row.getCell(2);
if (cell2 == null)
continue;
stu.setAge((int)cell2.getNumericCellValue());
HSSFCell cell3 = row.getCell(3);
if (cell3 == null)
continue;
// stu.setBirth(new SimpleDateFormat("yyyy-mm-dd").parse(cell3.getStringCellValue()));
list.add(stu);
}
}
return list;
}
@SuppressWarnings("unused")
private static String getValue(HSSFCell cell) {
if (cell.getCellType() == CellType.BOOLEAN) {
// 返回布尔类型 值
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == CellType.NUMERIC) {
//返回数值类型的值
return String.valueOf(cell.getNumericCellValue());
} else {
//返回字符串类型的值
return cell.getStringCellValue();
}
}
}
import java.util.Date;
public class Student {
private int Id;
private String Name;
private int age;
private Date Birth;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirth() {
return Birth;
}
public void setBirth(Date birth) {
Birth = birth;
}
}