错误后&LT使用SAX解析XML; BR> [英] Error parsing an XML using SAX after <br>

查看:119
本文介绍了错误后&LT使用SAX解析XML; BR>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <描述>
印度证券交易委员会:采取一个上市的投资公司决定处置的一部分,其
       投资是不是股价敏感资料SEBI的所指
      条例规定,1992年&LT(内幕交易的禁止); BR取代;
      以< B> [2011] 15 taxmann.com 229(SAT)< / B>
< /描述>
 

这是XML我想解析后&LT数据; BR> 。我能够解析之前< BR> ,但不能经过解析< BR>

这是我的把手类code:

 包com.exercise;

进口org.xml.sax.Attributes;
进口org.xml.sax.SAXException;
进口org.xml.sax.helpers.DefaultHandler中;

公共类位于RSSHandler扩展的DefaultHandler {

    最终诠释state_unknown = 0;
    最终诠释state_title = 1;
    最终诠释state_description = 2;
    最终诠释state_link = 3;
    最终诠释state_pubdate = 4;
    INT currentState的= state_unknown;

    RSSFeed中饲料;
    的RSSItem项目;

    布尔itemFound = FALSE;

    位于RSSHandler(){
    }

    RSSFeed中getFeed(){
        返回饲料;
    }

    @覆盖
    公共无效startDocument()抛出的SAXException {
        // TODO自动生成方法存根
        饲料=新的RSSFeed();
        项目=新的RSSItem();

    }



    @覆盖
    公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,
            属性属性)抛出的SAXException {
        // TODO自动生成方法存根

        如果(localName.equalsIgnoreCase(项目)){
            itemFound =真;
            项目=新的RSSItem();
            currentState的= state_unknown;
        }
        否则,如果(localName.equalsIgnoreCase(标题)){
            currentState的= state_title;
        }
        否则,如果(localName.equalsIgnoreCase(说明)){
            currentState的= state_description;
        }
        否则,如果(localName.equalsIgnoreCase(链接)){
            currentState的= state_link;
        }
        否则如果(localName.equalsIgnoreCase(pubdate的)){
            currentState的= state_pubdate;
        }
        其他{
            currentState的= state_unknown;
        }

    }


    @覆盖
    公共无效的endElement(URI字符串,字符串的localName,字符串QNAME)
            抛出的SAXException {
        // TODO自动生成方法存根
        currentState的= state_unknown;
        如果(localName.equalsIgnoreCase(项目)){
            feed.addItem(项目);
        }


    }

    @覆盖
    公共无效字符(字符CH [],诠释开始,诠释长度)
            抛出的SAXException {
        //super.characters(ch,开始,长度);
        // TODO自动生成方法存根
        StringBuilder的BUF =新的StringBuilder();


        如果(BUF!= NULL){
            的for(int i =启动; I<启动+长度;我++){
                buf.append(CH [i]);


            }

            串strCharacters = buf.toString();





                如果(itemFound ==真){
        //发现项目的标签,这是项目的参数
            开关(currentState的){
            案例state_title:
                item.setTitle(strCharacters);
                打破;
            案例state_description:
                item.setDescription(strCharacters); //这里的数据到来
                打破;
            案例state_link:
                item.setLink(strCharacters);
                打破;
            案例state_pubdate:
                item.setPubdate(strCharacters);
                打破;
            默认:
                打破;
            }

        }

        其他{
        //不是商品的标签发现,它养活的参数
            开关(currentState的){
            案例state_title:
                feed.setTitle(strCharacters);
                打破;
            案例state_description:
                feed.setDescription(strCharacters);
                打破;
            案例state_link:
                feed.setLink(strCharacters);
                打破;
            案例state_pubdate:
                feed.setPubdate(strCharacters);
                打破;
            默认:
                打破;
            }
        }

        currentState的= state_unknown;
    }


}


}
 

解决方案

我想在你的情况下,问题是,你正在初始化里面的字符的StringBuilder()等等创建新对象每次。相反,intializing它字符()的尝试初始化它在的startElement()

  @覆盖
    公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,
            属性属性)抛出的SAXException {

         StringBuilder的BUF =新的StringBuilder()
..........
}
 

<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>

This is xml I want to parse data after <br>. I'm able parse before <br> but not able to parse after <br>

This is my handle class code :

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;
    }


}


}

解决方案

I think in your case the problem is that you are initializing the StringBuilder inside the characters() so new object is created everytime. Instead of intializing it in characters() try to initialize it in the startElement()

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

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

这篇关于错误后&LT使用SAX解析XML; BR&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆