java读取XML(JDOM)

Jdom包,见附件。

import java.io.FileInputStream;
import java.util.List;

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

public class ReadXml {

	
	public static void main(String[] args) {
		try {
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(new FileInputStream("d:\\data\\Area.xml"));
			Element root = doc.getRootElement(); //最顶层元素
			
			List oneList = root.getChildren("Row");  //第二层元素列表
	          
	        for(int i = 0 ;i < oneList.size(); i++)  
	        {  
	        	Element one = (Element)oneList.get(i);  //得到一个row元素。
	            String str1= one.getChild("SQ_M").getTextTrim();
	            String str2= one.getChild("SQ_CM").getTextTrim();
	            String str3= one.getChild("SQ_YD").getTextTrim();
	            String str4= one.getChild("SQ_FT").getTextTrim();
	            
	            System.out.println(str1 );
	            System.out.println(str2 );
	            System.out.println(str3 );
	            System.out.println(str4 );
	        }  
	        
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

}

 XML代码样式,可从附件下载

<?xml version="1.0" encoding="gb2312"?><!-- edited with XML Spy v3.5 NT (http://www.xmlspy.com) by tk (tk) -->
<Import>
	<Row>
		<SQ_M>1</SQ_M>
		<SQ_CM>10000</SQ_CM>
		<SQ_YD>1.196</SQ_YD>
		<SQ_FT>10.7639</SQ_FT>
		<SQ_IN>1550</SQ_IN>
		<SQ_CHI>9</SQ_CHI>
	</Row>
	<Row>
		<SQ_M>.0001</SQ_M>
		<SQ_CM>1</SQ_CM>
		<SQ_YD>.00012</SQ_YD>
		<SQ_FT>.00108</SQ_FT>
		<SQ_IN>.155</SQ_IN>
		<SQ_CHI>.0009</SQ_CHI>
	</Row>
	<Row>
		<SQ_M>.8661</SQ_M>
		<SQ_CM>8361</SQ_CM>
		<SQ_YD>1</SQ_YD>
		<SQ_FT>9</SQ_FT>
		<SQ_IN>1296</SQ_IN>
		<SQ_CHI>7.525</SQ_CHI>
	</Row>
	<Row>
		<SQ_M>.0929</SQ_M>
		<SQ_CM>929</SQ_CM>
		<SQ_YD>.1111</SQ_YD>
		<SQ_FT>1</SQ_FT>
		<SQ_IN>144</SQ_IN>
		<SQ_CHI>.836</SQ_CHI>
	</Row>
	<Row>
		<SQ_M>.00065</SQ_M>
		<SQ_CM>6.45</SQ_CM>
		<SQ_YD>.00077</SQ_YD>
		<SQ_FT>.00694</SQ_FT>
		<SQ_IN>1</SQ_IN>
		<SQ_CHI>.0058</SQ_CHI>
	</Row>
	<Row>
		<SQ_M>.111</SQ_M>
		<SQ_CM>1111</SQ_CM>
		<SQ_YD>.133</SQ_YD>
		<SQ_FT>1.196</SQ_FT>
		<SQ_IN>172.2</SQ_IN>
		<SQ_CHI>1</SQ_CHI>
	</Row>
</Import>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值