background image

  InputStreamReader doc =
  

new

 InputStreamReader( conn.openInputStream() );

  XmlParser parser = 

new

 XmlParser( doc );

 
  得到 parser 实例后我们就可以调用 read 方法进行解析了,read 方法会返回一个
ParseEvent,通过判断他的类型我们就可以解析 xml 了。
  

try

 {

  

boolean

 keepParsing = 

true

;

  

while

( keepParsing ){

  ParseEvent event = parser.read();
  

switch

( event.getType() ){

  

case

 Xml.START_TAG:

  ..... 

// handle start of an XML tag

 
  

break

;

  

case

 Xml.END_TAG:

  ..... 

// handle end of an XML tag

 
  

break

;

  

case

 Xml.TEXT:

  ..... 

// handle text within a tag

 
  

break

;

  

case

 Xml.WHITESPACE:

  ..... 

// handle whitespace

 
  

break

;

  

case

 Xml.COMMENT:

  ..... 

// handle comment

 
  

break

;

  

case

 Xml.PROCESSING_INSTRUCTION:

  ..... 

// handle XML PI

 
  

break

;

  

case

 Xml.DOCTYPE:

  ..... 

// handle XML doctype

 
  

break

;

  

case

 Xml.END_DOCUMENT:

  ..... 

// end of document;

 
  keepParsing = 

false

;

  

break

;