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>

 

 

 

你可能感兴趣的:(java,xml,jdom)