dom4j解析xml文件

需要的jar包

import java.io.FileInputStream; import java.io.InputStream; import java.util.Iterator;

import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader;

 

public class OtherXmlJava {  

 public static void main(String[] args) {  

 new OtherXmlJava().jieXml();

 }  

public void jieXml(){   

try {    

InputStream is = new FileInputStream("E:\\zyn\\weather.xml");   

 //SAXReader就是一个管道,用一个流的方式,把xml文件读出来    
SAXReader reader = new SAXReader();   
 //Document 相当于整个xml文件    
Document doc = reader.read(is);    
// 获得root根节点,引用类为 org.dom4j.Element    
Element eleRoot = doc.getRootElement();   
 //迭代根节点下子节点   
 Iterator<Element> iter = eleRoot.elementIterator();    
for(;iter.hasNext();){    
 //得到根节点下子节点    
 Element eleWeather = (Element)iter.next();    
 System.out.println("eleWeather.getName():"+eleWeather.getName());    
 Iterator<Element> iterW = eleWeather.elementIterator();     
for(;iterW.hasNext();){      
//weather下的子节点     
 Element eleAll = iterW.next();      
//判断weather下的子节点有没有forecast_information名字的      
if("forecast_information".equals(eleAll.getName())){       
 Iterator<Element> information = eleAll.elementIterator();      
 //forecast_information下的节点      
 for(;information.hasNext();){       
 Element informationAll = (Element)information.next();       
 //查看forecast_information下有没有名字是city的        
if("city".equals(informationAll.getName())){        
 //根据属性名字得到属性         
Attribute data = informationAll.attribute("data");        
 //得到属性值         
String cityString = data.getValue();        
 System.out.println("city的名字 :"+informationAll.getName());        
 System.out.println("city的值 :"+cityString);        }       
 if("postal_code".equals(informationAll.getName())){        

 Attribute postal_codeData = informationAll.attribute("data");       

  String postal_codeString = postal_codeData.getValue();        
 System.out.println("postal_codeData 的value:"+postal_codeString);      
  }       }             }     
 if("current_conditions".equals(eleAll.getName())){      
 Iterator<Element> conditions = eleAll.elementIterator();       
for(;conditions.hasNext();){       
 Element conditionsAll = (Element)conditions.next();       
 if("condition".equals(conditionsAll.getName())){        
 Attribute condition = conditionsAll.attribute("data");        
 String conditionString = condition.getValue();         
System.out.println("condition:"+conditionString);       
 }       
 if("day_of_week".equals(conditionsAll.getName())){         
Attribute day_of_week = conditionsAll.attribute("data");         
String day_of_weekString = day_of_week.getValue();         
System.out.println("condition:"+day_of_weekString);        
}       }      }     
 if("forecast_conditions".equals(eleAll.getName())){      
 Iterator<Element> forecast_conditions = eleAll.elementIterator();       
for(;forecast_conditions.hasNext();){       
 Element conditionsAll = (Element)forecast_conditions.next();       
 if("condition".equals(conditionsAll.getName())){        
 Attribute condition = conditionsAll.attribute("data");         
String conditionString = condition.getValue();         
System.out.println("condition:"+conditionString);        
}       
 if("day_of_week".equals(conditionsAll.getName())){         
Attribute day_of_week = conditionsAll.attribute("data");        
 String day_of_weekString = day_of_week.getValue();        
 System.out.println("condition:"+day_of_weekString);       
 }       }      }     }    }   } catch (Exception e) {    e.printStackTrace();   }  } }

 

xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<xml_api_reply version="1">
- <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
- <forecast_information>
  <city data="Shenzhen, Guangdong" />
  <postal_code data="shenzhen" />
  <latitude_e6 data="" />
  <longitude_e6 data="" />
  <forecast_date data="2011-07-06" />
  <current_date_time data="2011-07-06 22:30:00 +0000" />
  <unit_system data="SI" />
  </forecast_information>
- <current_conditions>
  <condition data="局部多云" />
  <temp_f data="91" />
  <temp_c data="33" />
  <humidity data="湿度: 52%" />
  <icon data="/ig/images/weather/partly_cloudy.gif" />
  <wind_condition data="风向: 西南、风速:8 米/秒" />
  </current_conditions>
- <forecast_conditions>
  <day_of_week data="周三" />
  <low data="24" />
  <high data="34" />
  <icon data="/ig/images/weather/mostly_sunny.gif" />
  <condition data="晴间多云" />
  </forecast_conditions>
- <forecast_conditions>
  <day_of_week data="周四" />
  <low data="24" />
  <high data="32" />
  <icon data="/ig/images/weather/mostly_sunny.gif" />
  <condition data="以晴为主" />
  </forecast_conditions>
- <forecast_conditions>
  <day_of_week data="周五" />
  <low data="24" />
  <high data="34" />
  <icon data="/ig/images/weather/mostly_sunny.gif" />
  <condition data="以晴为主" />
  </forecast_conditions>
- <forecast_conditions>
  <day_of_week data="周六" />
  <low data="23" />
  <high data="33" />
  <icon data="/ig/images/weather/mostly_sunny.gif" />
  <condition data="以晴为主" />
  </forecast_conditions>
  </weather>
  </xml_api_reply>

 

posted on 2013-05-31 17:39  明天521  阅读(193)  评论(0)    收藏  举报