开发者

while parse the xml, not full data is getting after some word line end with read more

I'm trying to parse this:

while parse the xml, not full data is getting after some word line end with read more

I get the following, when I parse.

title = Workshop Metropolis description = Workshop Metropolis groot succes Vlak voor de...Read more

Read more is not any text on Description, which tag of xml you may see on above ? I don't understand why it is coming.

Here's the code:

///********************** XMLParsingExample.java **********************//

public class XMLParsingExample extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setCo开发者_开发百科ntentView(R.layout.main);

    try {

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

// /** Send URL to parse XML Tags */

        URL sourceUrl = new URL("http://192.168.0.30/ibis.rss");

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);

        xr.parse(new InputSource(sourceUrl.openStream()));


    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    /** Get result from MyXMLHandler SitlesList Object */
    sitesList = MyXMLHandler.sitesList;
    for (int i = 0; i < sitesList.getLat().size(); i++) {
        System.out.println("title = "+ sitesList.getLat().get(i).toString());
        System.out.println("description = "+ sitesList.getLong().get(i).toString());
    }

}

}


////MyXMLHandler


public class MyXMLHandler extends DefaultHandler {

Boolean currentElement = false;
String currentValue = null;
public static SitesList sitesList = null;

public static SitesList getSitesList() {
    return sitesList;
}

public static void setSitesList(SitesList sitesList) {
    MyXMLHandler.sitesList = sitesList;
}

/** Called when tag starts ( ex:- <name>AndroidPeople</name> 
 * -- <name> )*/
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    currentElement = true;

    if (localName.equals("channel"))
    {
        /** Start */ 
        sitesList = new SitesList();
    }
}

/** Called when tag closing ( ex:- <name>AndroidPeople</name> 
 * -- </name> )*/
@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    currentElement = false;

    /** set value */ 
    if (localName.equalsIgnoreCase("title"))
        sitesList.setLat(currentValue);
    else if (localName.equalsIgnoreCase("description"))
        sitesList.setLong(currentValue);
}

/** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
 * -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (currentElement) {
        //currentValue = StringEscapeUtils.unescapeHtml4(new String(ch, start, length).trim()); //new String(ch, start, length).trim()
        currentValue = new String(ch, start, length);
        currentElement = false;
    }

}

}


public class SitesList {

/** Variables */
private ArrayList<String> mlat = new ArrayList<String>();
private ArrayList<String> mlong = new ArrayList<String>();
private ArrayList<String> mradiuslat = new ArrayList<String>();
private ArrayList<String> mradiuslong = new ArrayList<String>();
private ArrayList<String> minfo = new ArrayList<String>();
private ArrayList<String> msound = new ArrayList<String>();

/** In Setter method default it will return arraylist 
 *  change that to add  */

public ArrayList<String> getLat() {
    return mlat;
}

public void setLat(String lat) {
    this.mlat.add(lat);
}

public ArrayList<String> getLong() {
    return mlong;
}

public void setLong(String longs) {
    this.mlong.add(longs);
}

}


first make pojo class(getter setter method for your all tag)

for parsing [CData] take a sax parser. put your link.

public class XMlStorePrasing extends DefaultHandler{
    public ArrayList<StoreLocator> Locator = new ArrayList<StoreLocator>();
    private StoreLocator wrapper ;
    private String record  = "";    
    private Context context;

    public static Map<String, String> QAns = new LinkedHashMap<String, String>();

    public ArrayList<StoreLocator> parseData(Context context)
    {
        try {
            try
            {       
                this.context = context;     
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();



    URL url = new URL("your link");
                    sp.parse(url.openStream(), this);
            }
            catch (UnknownHostException e) {
                e.printStackTrace();
            }               
    } catch (Exception e ) {
        e.printStackTrace();
    }
    return Locator;

    }

    @Override
    public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
        record = "";
        if(localName.equalsIgnoreCase("Record"))
        {
            wrapper = new StoreLocator();
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)  throws SAXException {


        if(localName.equalsIgnoreCase("store"))
        {
            wrapper.setStore(record);

        }
        else if(localName.equalsIgnoreCase("latitude"))
        {

            wrapper.setLatitude(record);


        }
        else if(localName.equalsIgnoreCase("longitude"))
        {

            wrapper.setLongitude(record);

        }
        else if(localName.equalsIgnoreCase("distance"))
        {
//          wrapper.setAnswerOptions(record);
            wrapper.setDistance(record);

        }
        else if(localName.equalsIgnoreCase("address"))
        {

            wrapper.setAddress(record);


        }
        else if(localName.equalsIgnoreCase("city"))
        {
//          wrapper.setAnswerOptions(record);
            wrapper.setCity(record);

        }
        else if(localName.equalsIgnoreCase("state"))
        {
            wrapper.setState(record);


        }
        else if(localName.equalsIgnoreCase("zip"))
        {           
            wrapper.setZip(record);     

        }

        else if(localName.equalsIgnoreCase("Record"))
        {           
            Locator.add(wrapper);   
        }

    }

    @Override
    public void characters(char[] ch, int start, int length)throws SAXException {

        record += String.valueOf(ch,start,length);


    }

from calling this:

ArrayList<StoreLocator> ss = new ArrayList<StoreLocator>();

XMlStorePrasing prasing = new XMlStorePrasing();
        ss = prasing.parseData(getApplicationContext()); 

I think this will help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜