在android中使用sax解析器解析大长度字符串

发布于 2025-01-02 02:11:23 字数 7266 浏览 1 评论 0原文

我在android中使用sax解析器解析xml。我的 xml 结构如下所示,

   <customerlist>
        <Customer>
              <customerId>2</customerId>
              <customerFname>prabhu</customerFname>
              <customerLname>kumar</customerLname>

              <customerImage>
                       iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABHNCSVQICAgIfAhkiAAACJ9JREFUaIHtmllsXFcZx3/n3Due8XgcO3a8xG72tGkTknRJG7qAaEtbhFjVJUpBoBahgkoRUCFe+lCJvkAKCGgFPJQK0VKWqIhShMhL0mahLU1ImjZp0ziZLN5dezxje5Z7z/l4uDOOl9mcOskLf+vTle79fM63nPM/53xn4P+4tFDzrDffkEoKpQxTgAZcoCYvzpT3FxKWwHAD5PLiT3k/DW6JRjQQAupuuadx/VWbYk9b5dcg+uJkQlnR4uaOvjn28J7ticPAOOARODVdtUQTYaDu+7/86GONLfqbvvUiF9DcknB1KJMYtL/a9shrTxA4kWNGFkploAaoa1+t7mxfWhcRqTgULwiUUpG+02N3Aj8jyEBupk4xBxRBBmLJVEqiEznEWgBEBI8MYVVbdHYpFEZ8tNIUT65CrAEFSjmAxZcsIRUp3p7WJFNZAWJAOi/+VJ1SE9IBQmKsgwGsAgth3cCVTZ9FRIFViBXESjC9rCKTG+MjLfcRdRZjJ7+d0/P8LEsab6YxshxjfMK6gU9f/hS+tfk+ZogBMdYhmI8FEpmGcpO4xs8pba1grQCWzcsfYVH0ckbHexkcP8K1nV+jNbaWfad+zliml8+seYpoaCEnh3ZzZfMXuKzhBg50P8v69nuJ1SzmpSPfwPiGnJ+jM7aZtW1fpCW6FusJ6Nk50Br8nNIEQ7posEtlQEGet0QQEayxdMQ2se/U06xsvh3P5NjQvpUzI6+zsul2Wus3MjT+PuO5IXybZVnjzYxMxLFiibhNdCf307HgBqI1bcTcdm5c+m26R/fjKBfBTvYzU+wMm+bkQDaXYiKTJJ1JIlaTM0lWNt7GFc2fIuZ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xEiOdS1baa1bh7FZRJghFovhsvqbuXPVTzmZ2MWO44+SNuNoQhzqf57rFj+Eby3KmTS8qCFV0Wg164BCEx/dTSrTS0hHyPnjGONN61aweGaCNYs+x7qWeznY91v+dex7CIImPKn7Tt+fqXWaaIqspts/CGVONRVZqFS1YFb1QIRUti8oWukQx4b+zgcTx0DVBPyOwVERNrR9hVUL72Lv6SeJJ3ZPDi+Z8uc6Ed4Z3M6G9vtR0wM/tzlQWAOqywBY6wexUoKTj6iIh+NEuabtAcJuI28NPMc7A39BowKdok1rcjZFOpvgjhXb+AX3lex33krlglAXaseK5AesYMSjvqaTu1b9BKVcdp18fJLrJR/bUgKaQ/3PYWRaJXFuK7Hv+3M4EwtLGm6irqaF8dwgrdH13LLsB5wc3sk/3v0WILiqNp+VakKiSOdG2Bn/YVmted2N1jmtLG/4JNFQE50LNrMz/jgj6RPU6DrO75JHIbOvBKZh3uaAo2tAFJs7v8OLR7/E/t5ncHAIqeh5b8eV5AvEZTAvu1GlHPpShzibfI0lDTfSWreOROZEsJW4wKiYganPYtBoDvT8hsMDf8TVtRwZ2s7nr3iGjDdCd/JNlDp/nhCBCgkoz0KFCVye/2Eo/T5KhYLmxOFg3++5uu1BLFJkFZ6DWCoWB0s5MOl3ZQcMa1u25HsEJYoTwzs4PrKDluhVGJMtzZXVyjmbqt4LCQTF1cIZtbTAkoabWNl0B6IEFLhOLceH/8nGtq+indC5s9QcRcFU8prTXsgAmQMHDox0dXUlfd/XgBKRElyoEFnO290vTWtirPllrGyma3gHag40KiBoRCtsZlxGgAxFrliDnou/a8rLJmA50AhECO6qLgY8AqMTQBx4ExgGkvlv04wthhhQB7QCC4B6gpvL2bcQFwYGyAIpAqMHCO6Jx6nynrgQgWGCq80kgfEX+mcGBVgCJwpXqxlmRL6AUhnQBAa7U54X43cSBRROYD6BI4XnLFL9H0iaJNCEw0eHAAAAAElFTkSuQmCC

        </customerImage>


        </Customer>

</customerlist>

我可以获得 customerIdcustomerFname customerLname,但对于 customerImage 我没有得到完整的字符串,我只得到了字符串的一部分,即(iVBORw0KGgoAAAANSUhEUgAADAAAAAwCAYAAABXAvmHAAABHNCSVQICAgIfAhkiAAACJ9JREFUaIHtmllsXFcZx3/n3Due8XgcO3a8xG72tGkTknRJG7qAa EtbhFjVJUpBoBahgkoRUCFe+lCJvkAKCGgFPJQK0VKWqIhShMhL0mahLU1ImjZp0ziZLN5dezxje5Z7z/l4uDOOl9mcOskLf+vTle79fM63nPM/53xn4P+4tFDzrDffkE okpQxTgAZcoCYvzpT3FxKWwHAD5PLiT3k/DW6JRjQQAupuadx/VWbYk9b5dcg+uJkQlnR4uaOvjn28J7ticPAOOARODVdtUQTYaDu+7/86GONLFQBVVUiF9DcknB1KJM YTL/a9shrTxA4kWNGFkploAaoa1+t7mxfWhcRqTgULwiUUpG+02N3Aj8jyEBupk4xBxRBBmLJVEqiEznEWgBEBI8MYVVbdHYpFEZ8tNIUT65CrAEFSjmAxZcsIRUp3p7W JFNZAWJAOi/+VJ1SE9IBQmKsgwGsAgth3cCVTZ9FRIFViBXESjC9rCKTG+MjLfcRdRZjJ7+d0/P8LEsab6YxshxjfMK6gU9f/hS+tfk+ZogBMdYhmI8FEpmGcpO4xs8pb a1grQCWzcsfYVH0ckbHexkcP8K1nV+jNbaWfad+zliml8+seYpoaCEnh3ZzZfMXuKzhBg50P8v69nuJ1SzmpSPfwPiGnJ+jM7aZtW1fpCW6FusJ6Nk50Br8nNIEQ7posE tlQEGet0QQEayxdMQ2se/U06xsvh3P5NjQvpUzI6+zsul2Wus3MjT+PuO5IXybZVnjzYxMxLFiibhNdCf307HgBqI1bcTcdm5c+m26R/fjKBfBTvYzU+wMm+bkQDaXYiK TJJ1JIlaTM0lWNt7GFc2fIuZ2MJB6l8M9L+JZD7GKkx/syTDVRSY7Qcab4N8nf01qYpDB1HFOD7+OlhqyuVE8P4OIomvwFTw/O9nHTJnIJMnmulNtqnoIAbChfQurlrVj

我的 xmlHandler 代码如下,

import java.util.ArrayList;

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

import android.util.Log;

import com.bvbi.invoicing.client.android.customer.model.CustomerPojoInList;

public class CustomerListParser extends DefaultHandler {

    Boolean currentElement = false;
    String tempValue = null;


    CustomerPojoInList customer = null;
    public static ArrayList<CustomerPojoInList> customers = null;

    @Override
    public void startDocument() throws SAXException {
         customers = new ArrayList<CustomerPojoInList>();

    }

    /** 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("Customer"))
        {
            /** Start */ 
            customer = new CustomerPojoInList();

        }


    }

    /** Called when tag closing */
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;
        String currentValue = tempValue;
        tempValue = "";
        /** set value */ 

        if (localName.equalsIgnoreCase("customerId"))
            customer.setCustomerId(currentValue.toString());

        else if (localName.equalsIgnoreCase("customerFname"))
            customer.setCustomerFname(currentValue.toString());

        else if (localName.equalsIgnoreCase("customerLname"))
            customer.setCustomerLname(currentValue.toString());
        else if (localName.equalsIgnoreCase("customerImage"))
        {
            Log.d("prabhu","Customer image in parser......"+currentValue);
            customer.setCustomerImage(currentValue.toString());
        }
        else if (localName.equalsIgnoreCase("Customer"))
            customers.add(customer);

    }

    /** Called to get tag characters */
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            tempValue = new String(ch,start, length);

            if(tempValue.equals(null))
                tempValue = "";
            currentElement = false;

        }

    }
      @Override
      public void endDocument() throws SAXException {


        }

}

请帮助我解决问题。

I parsing the xml using sax parser in android. My xml structure is as given below

   <customerlist>
        <Customer>
              <customerId>2</customerId>
              <customerFname>prabhu</customerFname>
              <customerLname>kumar</customerLname>

              <customerImage>
                       iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABHNCSVQICAgIfAhkiAAACJ9JREFUaIHtmlls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lQWAOqywBY6wexUoKTj6iIh+NEuabtAcJuI28NPMc7A39BowKdok1rcjZFOpvgjhXb+AX3lex33krlglAXaseK5AesYMSjvqaTu1b9BKVcdp18fJLrJR/bUgKaQ/3PYWRaJXFuK7Hv+3M4EwtLGm6irqaF8dwgrdH13LLsB5wc3sk/3v0WILiqNp+VakKiSOdG2Bn/YVmted2N1jmtLG/4JNFQE50LNrMz/jgj6RPU6DrO75JHIbOvBKZh3uaAo2tAFJs7v8OLR7/E/t5ncHAIqeh5b8eV5AvEZTAvu1GlHPpShzibfI0lDTfSWreOROZEsJW4wKiYganPYtBoDvT8hsMDf8TVtRwZ2s7nr3iGjDdCd/JNlDp/nhCBCgkoz0KFCVye/2Eo/T5KhYLmxOFg3++5uu1BLFJkFZ6DWCoWB0s5MOl3ZQcMa1u25HsEJYoTwzs4PrKDluhVGJMtzZXVyjmbqt4LCQTF1cIZtbTAkoabWNl0B6IEFLhOLceH/8nGtq+indC5s9QcRcFU8prTXsgAmQMHDox0dXUlfd/XgBKRElyoEFnO290vTWtirPllrGyma3gHag40KiBoRCtsZlxGgAxFrliDnou/a8rLJmA50AhECO6qLgY8AqMTQBx4ExgGkvlv04wthhhQB7QCC4B6gpvL2bcQFwYGyAIpAqMHCO6Jx6nynrgQgWGCq80kgfEX+mcGBVgCJwpXqxlmRL6AUhnQBAa7U54X43cSBRROYD6BI4XnLFL9H0iaJNCEw0eHAAAAAElFTkSuQmCC

        </customerImage>


        </Customer>

</customerlist>

I am able to get customerId, customerFname, customrLname, but for customerImage I am not getting complete string I am only getting part of the string i.e (iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABHNCSVQICAgIfAhkiAAACJ9JREFUaIHtmllsXFcZx3/n3Due8XgcO3a8xG72tGkTknRJG7qAaEtbhFjVJUpBoBahgkoRUCFe+lCJvkAKCGgFPJQK0VKWqIhShMhL0mahLU1ImjZp0ziZLN5dezxje5Z7z/l4uDOOl9mcOskLf+vTle79fM63nPM/53xn4P+4tFDzrDffkEoKpQxTgAZcoCYvzpT3FxKWwHAD5PLiT3k/DW6JRjQQAupuuadx/VWbYk9b5dcg+uJkQlnR4uaOvjn28J7ticPAOOARODVdtUQTYaDu+7/86GONLfqbvvUiF9DcknB1KJMYtL/a9shrTxA4kWNGFkploAaoa1+t7mxfWhcRqTgULwiUUpG+02N3Aj8jyEBupk4xBxRBBmLJVEqiEznEWgBEBI8MYVVbdHYpFEZ8tNIUT65CrAEFSjmAxZcsIRUp3p7WJFNZAWJAOi/+VJ1SE9IBQmKsgwGsAgth3cCVTZ9FRIFViBXESjC9rCKTG+MjLfcRdRZjJ7+d0/P8LEsab6YxshxjfMK6gU9f/hS+tfk+ZogBMdYhmI8FEpmGcpO4xs8pba1grQCWzcsfYVH0ckbHexkcP8K1nV+jNbaWfad+zliml8+seYpoaCEnh3ZzZfMXuKzhBg50P8v69nuJ1SzmpSPfwPiGnJ+jM7aZtW1fpCW6FusJ6Nk50Br8nNIEQ7posEtlQEGet0QQEayxdMQ2se/U06xsvh3P5NjQvpUzI6+zsul2Wus3MjT+PuO5IXybZVnjzYxMxLFiibhNdCf307HgBqI1bcTcdm5c+m26R/fjKBfBTvYzU+wMm+bkQDaXYiKTJJ1JIlaTM0lWNt7GFc2fIuZ2MJB6l8M9L+JZD7GKkx/sYTDVRSY7Qcab4N8nf01qYpDB1HFOD7+OlhqyuVE8P4OIomvwFTw/O9nHTJnIJMnmUlNtqnoIAbChfQurlrVj
)

My xmlHandler code is below

import java.util.ArrayList;

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

import android.util.Log;

import com.bvbi.invoicing.client.android.customer.model.CustomerPojoInList;

public class CustomerListParser extends DefaultHandler {

    Boolean currentElement = false;
    String tempValue = null;


    CustomerPojoInList customer = null;
    public static ArrayList<CustomerPojoInList> customers = null;

    @Override
    public void startDocument() throws SAXException {
         customers = new ArrayList<CustomerPojoInList>();

    }

    /** 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("Customer"))
        {
            /** Start */ 
            customer = new CustomerPojoInList();

        }


    }

    /** Called when tag closing */
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;
        String currentValue = tempValue;
        tempValue = "";
        /** set value */ 

        if (localName.equalsIgnoreCase("customerId"))
            customer.setCustomerId(currentValue.toString());

        else if (localName.equalsIgnoreCase("customerFname"))
            customer.setCustomerFname(currentValue.toString());

        else if (localName.equalsIgnoreCase("customerLname"))
            customer.setCustomerLname(currentValue.toString());
        else if (localName.equalsIgnoreCase("customerImage"))
        {
            Log.d("prabhu","Customer image in parser......"+currentValue);
            customer.setCustomerImage(currentValue.toString());
        }
        else if (localName.equalsIgnoreCase("Customer"))
            customers.add(customer);

    }

    /** Called to get tag characters */
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            tempValue = new String(ch,start, length);

            if(tempValue.equals(null))
                tempValue = "";
            currentElement = false;

        }

    }
      @Override
      public void endDocument() throws SAXException {


        }

}

Please help me to fix the issue.

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

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

发布评论

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

评论(3

快乐很简单 2025-01-09 02:11:23

在 sax 解析器中,characters() 方法每次最多只能解析 1024 个字符。所以我们需要追加字符串,直到解析完所有字符。

我将上面的代码更改如下

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


    Log.d("prabhu","Customer image length in parser......"+length);
    if (currentElement ) {

        tempValue = new String(ch,start, length);

        if(tempValue.equals(null))
            tempValue = "";



    }
        tempValue = tempValue+new String(ch,start, length);
 }

In sax parser, characters() method parses only maximum of 1024 characters each time. So we need to append the strings until all the characters are parsed.

I changed the above code as follows

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


    Log.d("prabhu","Customer image length in parser......"+length);
    if (currentElement ) {

        tempValue = new String(ch,start, length);

        if(tempValue.equals(null))
            tempValue = "";



    }
        tempValue = tempValue+new String(ch,start, length);
 }
旧伤还要旧人安 2025-01-09 02:11:23

您发布的输出正好是 1024 个字符。这看起来像是特定的缓冲区大小。你如何得到这个输出?也许检查该方法和/或您的 CustomerPojoInList

我非常相信,涉及到的缓冲区最多有 1024 个字符......

祝你好运!

The output you posted is exactly 1024 characters. This looks like a certain buffer size. How do you get this output? Maybe check that method and / or your CustomerPojoInList.

I very much believe, that there is some buffer involved that has a maximum of 1024 characters...

Good luck!

梦与时光遇 2025-01-09 02:11:23

第一次发帖。用可能对其他人有帮助的内容更新了答案。我希望它对我的特定问题不太具体。我正在解析一个我自己创建的 RSS 提要,它的描述非常长,但您感兴趣的其他标签(即提要标题、日期和 URL)总是很短。描述包含有关社交活动的信息。在描述中,我使用稍后解析的标签来提供有关事件的信息,例如事件日期(与 RSS pubDate 不同)、(位置)、(ticketDetails)、(电话)等,您明白了。

处理这个问题的一个好方法是稍微修改这篇文章中的答案。我在 (Event) 和 (EndEvent) 的描述中添加了标签,并不断附加到我的字符串生成器,直到获得“(EndEvent)”。这样我就知道我有完整的字符串。如果您不控制提要,它可能不适用于您的情况,除非您知道 RSS 描述末尾始终有某个字符串。

发帖以防万一(咳嗽、黑客)对任何人都有帮助。代码如下:

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

    strBuilder =  new StringBuilder();

    if ("item".equals(qName)) {
        currentItem = new RssItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;
    } else if ("link".equals(qName)) {
        parsingLink = true;
    }
     else if ("pubDate".equals(qName)) {
            parsingDate = true;
        }
     else if ("description".equals(qName)) {
            strBuilder =  new StringBuilder(); //reset the strBuilder variable to null
            parsingDescription = true;
        }
}


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

    String descriptionTester = strBuilder.toString();

    if ("item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {
        parsingTitle = false;
    } else if ("link".equals(qName)) {
        parsingLink = false;
    }
    else if ("pubDate".equals(qName)) {
        parsingDate = false;
    }
    //else 
    //  currentItem.setDescription(descriptionTester);
    else if ("description".equals(qName) && descriptionTester.contains("(EndEvent)")) {
        parsingDescription = false;
    }
}


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

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

    if (parsingTitle) {
        if (currentItem != null)

            currentItem.setTitle(new String(ch, start, length));
            parsingTitle = false;

    }

    else if (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;        
    }
    }
    else if (parsingDate) {
        if (currentItem != null) {

            currentItem.setDate(new String(ch, start, length));
            parsingDate = false;

    }
    }

    else if (parsingDescription) {
        if (currentItem != null && strBuilder.toString().contains("(EndEvent)" )) {

                   String descriptionTester = strBuilder.toString();

            currentItem.setDescription(descriptionTester);

            parsingDescription = false;

            }
    } 
} 

正如我所说,希望对某人有所帮助,因为我为此困惑了一段时间!

first time post. Updated answer with something that might help others. I hope it is not too specific to my particular problem. I am parsing an RSS feed that I create myself with a really long description but the other tags you are interested in, i.e. feed title, date and URL are always short. The description contains information about social events. Within the description, I use tags that I later parse to give me information about the event such as event date (different from RSS pubDate), (Location), (ticketDetails), (Phone), etc, you get the idea.

A good way to handle this is with a slight modification of the answer in this post. I added tags to the description for (Event) and (EndEvent) and I keep appending to my String Builder until I get "(EndEvent)". That way i know i have the full string. It might not work for your situation if you dont control the feed unless you know there is always a certain string at the end of your RSS description.

Posting in case this (cough, hack) helps anyone. Code is as follows:

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

    strBuilder =  new StringBuilder();

    if ("item".equals(qName)) {
        currentItem = new RssItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;
    } else if ("link".equals(qName)) {
        parsingLink = true;
    }
     else if ("pubDate".equals(qName)) {
            parsingDate = true;
        }
     else if ("description".equals(qName)) {
            strBuilder =  new StringBuilder(); //reset the strBuilder variable to null
            parsingDescription = true;
        }
}


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

    String descriptionTester = strBuilder.toString();

    if ("item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {
        parsingTitle = false;
    } else if ("link".equals(qName)) {
        parsingLink = false;
    }
    else if ("pubDate".equals(qName)) {
        parsingDate = false;
    }
    //else 
    //  currentItem.setDescription(descriptionTester);
    else if ("description".equals(qName) && descriptionTester.contains("(EndEvent)")) {
        parsingDescription = false;
    }
}


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

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

    if (parsingTitle) {
        if (currentItem != null)

            currentItem.setTitle(new String(ch, start, length));
            parsingTitle = false;

    }

    else if (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;        
    }
    }
    else if (parsingDate) {
        if (currentItem != null) {

            currentItem.setDate(new String(ch, start, length));
            parsingDate = false;

    }
    }

    else if (parsingDescription) {
        if (currentItem != null && strBuilder.toString().contains("(EndEvent)" )) {

                   String descriptionTester = strBuilder.toString();

            currentItem.setDescription(descriptionTester);

            parsingDescription = false;

            }
    } 
} 

As I said, hope that helps someone as I was stumped on this for a while!

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