2011-11-23 2 views
0
<description> 
SEBI : Decision taken by a listed investment company to dispose of a part of its 
     investment is not “price sensitive information” within meaning of SEBI 
     (Prohibition of Insider Trading) Regulations, 1992<br>; 
     By <b> [2011] 15 taxmann.com 229 (SAT)</b> 
</description> 

후 SAX를 사용하여 XML을 구문 분석. 내가 할 수 구문 분석 해요 전에이 내 핸들 클래스 코드 <br>오류이 내가 <code><br></code> 후 데이터를 파싱 할 XML입니다 <br>

후 구문 분석 할 수 <br> 아니라 :

package com.exercise; 

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

public class RSSHandler extends DefaultHandler { 

    final int state_unknown = 0; 
    final int state_title = 1; 
    final int state_description = 2; 
    final int state_link = 3; 
    final int state_pubdate = 4; 
    int currentState = state_unknown; 

    RSSFeed feed; 
    RSSItem item; 

    boolean itemFound = false; 

    RSSHandler(){ 
    } 

    RSSFeed getFeed(){ 
     return feed; 
    } 

    @Override 
    public void startDocument() throws SAXException { 
     // TODO Auto-generated method stub 
     feed = new RSSFeed(); 
     item = new RSSItem(); 

    } 



    @Override 
    public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 
     // TODO Auto-generated method stub 

     if (localName.equalsIgnoreCase("item")){ 
      itemFound = true; 
      item = new RSSItem(); 
      currentState = state_unknown; 
     } 
     else if (localName.equalsIgnoreCase("title")){ 
      currentState = state_title; 
     } 
     else if (localName.equalsIgnoreCase("description")){ 
      currentState = state_description; 
     } 
     else if (localName.equalsIgnoreCase("link")){ 
      currentState = state_link; 
     } 
     else if (localName.equalsIgnoreCase("pubdate")){ 
      currentState = state_pubdate; 
     } 
     else{ 
      currentState = state_unknown; 
     } 

    } 


    @Override 
    public void endElement(String uri, String localName, String qName) 
      throws SAXException { 
     // TODO Auto-generated method stub 
     currentState = state_unknown; 
     if (localName.equalsIgnoreCase("item")){ 
      feed.addItem(item); 
     } 


    } 

    @Override 
    public void characters(char ch[], int start, int length) 
      throws SAXException { 
     //super.characters(ch, start, length); 
     // TODO Auto-generated method stub 
     StringBuilder buf=new StringBuilder(); 


     if (buf!=null) { 
      for (int i=start; i<start+length; i++) { 
       buf.append(ch[i]); 


      } 

      String strCharacters=buf.toString(); 





       if (itemFound==true){ 
     // "item" tag found, it's item's parameter 
      switch(currentState){ 
      case state_title: 
       item.setTitle(strCharacters); 
       break; 
      case state_description: 
       item.setDescription(strCharacters); //here data coming 
       break; 
      case state_link: 
       item.setLink(strCharacters); 
       break; 
      case state_pubdate: 
       item.setPubdate(strCharacters); 
       break; 
      default: 
       break; 
      } 

     } 

     else{ 
     // not "item" tag found, it's feed's parameter 
      switch(currentState){ 
      case state_title: 
       feed.setTitle(strCharacters); 
       break; 
      case state_description: 
       feed.setDescription(strCharacters); 
       break; 
      case state_link: 
       feed.setLink(strCharacters); 
       break; 
      case state_pubdate: 
       feed.setPubdate(strCharacters); 
       break; 
      default: 
       break; 
      } 
     } 

     currentState = state_unknown; 
    } 


} 


} 
+0

에 초기화하려고합니다. 'br'는 스스로 폐쇄해야합니다. –

답변

0

&amp;는 XML 엔티티 참조이며, &을 의미한다.

기본적으로 SAX가 변환을 수행하므로 원본 XML이 안녕 & 안녕이라고 말하면 안녕 & 작별 인사를보아야합니다. This 링크로 이동하십시오. 문제를 해결할 수도 있습니다

0

붙여 넣은 첫 번째 텍스트에 문제가 있습니다. 코드 모드에서 XML을 다시 게시 해보십시오 (각 줄의 시작 부분에 4 공백).

제 생각에는 xml이 URL 인코딩 형식이므로 처리하기 전에 디코딩해야합니다.

0

게시 된 XML이 유효하지 않으므로 문서의 따옴표도 이스케이프해야합니다.

나는 그것이 귀하의 문제인지는 모르겠지만 기여자가 될 것입니다.

문제가 당신이 매번 생성 된 characters() 그래서 새로운 객체 내부의 StringBuilder를 초기화하는 것입니다

0

내가 귀하의 경우 생각 (따옴표는 주변에 "가격에 민감한 정보"이다). 대신 characters()에서 그것을 intializing의는 기본적으로 당신이 XML 파서 SGML 구문 분석하려고하는 startElement()

@Override 
    public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 

     StringBuilder buf=new StringBuilder() 
.......... 
}