其中要读取的xml文件:(文件名字为data_10k.xml)

<?xml version="1.0" encoding="UTF-8"?>
<RESULT>  
<MODEL>     
  <CLASS>美食</CLASS>     
  <NUMBER>2</NUMBER>
  <WORD>中餐厅</WORD>
  <WORD>西餐厅</WORD>
</MODEL>  
<MODEL>     
  <CLASS>通用设施</CLASS> 
  <NUMBER>3</NUMBER>
  <WORD>wifi</WORD>
  <WORD>电梯</WORD> 
  <WORD>商场</WORD>   
</MODEL>
<MODEL>     
  <CLASS>服务</CLASS>   
  <NUMBER>4</NUMBER>  
  <WORD>会议厅</WORD>
  <WORD>商务中心</WORD>
  <WORD>外币兑换</WORD>
  <WORD>旅游票务</WORD>    
</MODEL>
</RESULT>


利用java读取文件:

package EvaluationModel;

import java.io.File;
import java.net.URLDecoder;

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[] args){
		try{
			String filePath=readXML.class.getResource("").getPath()+"data_10k.xml";    //代码的路径在bin文件夹下,
			File f=new File(URLDecoder.decode(filePath, "utf-8"));                     //如果路径里面有中文的,要转换为utf-8
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
			DocumentBuilder builder = factory.newDocumentBuilder();   
			Document doc = builder.parse(f);   
			NodeList nl = doc.getElementsByTagName("MODEL"); 
			int number=0;
			int j=0;
			for (int i = 0; i < nl.getLength(); i++) {   
				System.out.print("类别:"+ doc.getElementsByTagName("CLASS").item(i).getFirstChild().getNodeValue()+" ");   
				number=number+Integer.parseInt(doc.getElementsByTagName("NUMBER").item(i).getFirstChild().getNodeValue());  //获取个数\
				System.out.print("属性词:");
				while(j<number){
					System.out.print( doc.getElementsByTagName("WORD").item(j).getFirstChild().getNodeValue()+" ");
					j++;
				}
				System.out.println();
			} 
		}catch(Exception e){
			 e.printStackTrace(); 
		}
	}

}


读取的结果显示为:

java:利用Dom读取xml文件_xml