练习对XML文件的操作

初步练习下java对XML文件的操作,加深下记忆而已,代码如下:
public class Main {
	public static void main(String[] args) throws IOException, ParseException {

	ClassLoader cl=Thread.currentThread().getContextClassLoader();
		System.out.println(cl);
		String clname="cn/com/test/Main.class";
		URL url=cl.getResource(clname);
		System.out.println(url);
		String classPath = "";
		try {
			classPath=(url.toURI().getPath()).replaceFirst("/", "").replaceFirst(clname, "");
			System.out.println(classPath);
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File file=new File(classPath+"HUAWEI_162_20111224000338_2_ADSL.xml");
		SAXReader reader=new SAXReader();
		try {
			
			reader.setEncoding("GB2312");
			Document document=reader.read(file);
			Element element=document.getRootElement();
			Element  deviceInfo=element.element("deviceInfo");
			Long objectTypeId=Long.parseLong(deviceInfo.attributeValue("objectTypeId"));
			Element  measTime=element.element("measTime");
			Date date =new Date();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dt=sdf.format(date);
			System.out.println(dt);
			
			Date measTime1 = sdf.parse(measTime.getText().trim());
			System.out.println(measTime.getText().trim());
			System.out.println(objectTypeId+" "+measTime1);
			Element dataEsle=element.element("datas");
			List<Element>  dataEsles=dataEsle.elements("data");
			for(Element dataEsl:dataEsles){
				String oidStr= dataEsl.attributeValue("oid").trim();
				String tableName= dataEsl.attributeValue("tableName").trim();
				String valuesStr=dataEsl.getStringValue().trim();
				String[] oidValues = valuesStr.split("\\n");
				for(String oidValue : oidValues) {
					String[] index_value_str = oidValue.split("=");
					String oidWithIdx = index_value_str[0].trim();
					String indexValue = index_value_str[1];	// 索引值
					System.out.println(oidWithIdx);
					System.out.println(indexValue);
				}
				
			
				System.out.println(oidStr);
				System.out.println(tableName);
			}
		} catch (DocumentException 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、付费专栏及课程。

余额充值