解析 xml 时,在某些字行结尾处未获取完整数据,并以 read more 结尾

发布于 2024-12-06 06:31:14 字数 4151 浏览 0 评论 0原文

我正在尝试解析此内容:

this is xml file which I Want to Read

当我解析时,我得到以下内容。

标题 = 工坊大都市 描述 = Workshop Metropolis groot succes Vlak voor de...阅读更多

阅读更多 描述中没有任何文字,您可能会在上面看到哪个 xml 标签? 我不明白为什么它会来。

代码如下:

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

public class XMLParsingExample extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {

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

// /** 发送 URL 来解析 XML 标签 */

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

}

I'm trying to parse this:

this is xml file which I want to read

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);
    setContentView(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);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ可否回来 2024-12-13 06:31:14

首先创建用于解析 [CData] 的 pojo 类(所有标记的 getter setter 方法)

采用 sax 解析器。把你的链接。

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


    }

致电此:

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

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

我认为这会对您有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文