1

I am trying to parse a webpage, the values i want to parse are included in certain TD tags. Can someone please help me to get along? I am getting a syntax error line1 column62.

Goal is to pass the values to a listview later on, so that it shows Nieuw beltegoed: € 2,50

Any help would be appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
<TABLE class=personaltable cellSpacing=0 cellPadding=0>
 <TBODY>
  <TR class=alternativerow>
   <TD>Nieuw beltegoed:</TD>
   <TD>€ 2,50</TD>
  </TR>
  <TR>
   <TD>Tegoed vorige periode:</TD>
   <TD>€ 3,62</TD>
  </TR>
  <TR class=alternativerow>
   <TD>Tegoed tot 09-11-2011:</TD>
   <TD>€ 1,12</TD>    
  </TR>
  <TR>
   <TD>
   <TD height=25></TD>
  <TR class=alternativerow>
   <TD>Verbruik sinds nieuw tegoed:</TD>
   <TD>€ 3,33</TD>
  </TR>
  <TR>
   <TD>Ongebruikt tegoed:</TD>
   <TD>€ 1,79</TD>
  </TR>
  <TR class=alternativerow>
   <TD class=f-Orange>Verbruik boven bundel:</TD>
   <TD class=f-Orange>€ 0,00</TD>
  </TR>
  <TR>
   <TD>Verbruik dat niet in de bundel zit*:</TD>
   <TD>€ 0,00</TD>
  </TR>
 </TBODY>
</TABLE>
</head>

My Saxhandler so far:

// ===========================================================
    // Fields
    // ===========================================================

    private boolean in_TABLE = false;
    private boolean in_TBODY = false;
    private boolean in_TR = false;
    private boolean in_TD = false;

    private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    public ParsedExampleDataSet getParsedData() {
            return this.myParsedExampleDataSet;
    }

    // ===========================================================
    // Methods
    // ===========================================================
    @Override
    public void startDocument() throws SAXException {
            this.myParsedExampleDataSet = new ParsedExampleDataSet();
    }

    @Override
    public void endDocument() throws SAXException {
            // Nothing to do
    }

    /** Gets be called on opening tags like: 
     * <tag> 
     * Can provide attribute(s), when xml was like:
     * <tag attribute="attributeValue">*/
    @Override
    public void startElement(String namespaceURI, String localName,
                    String qName, Attributes atts) throws SAXException {
            if (localName.equals("TABLE class=personaltable cellSpacing=0 cellPadding=0")) {
                    this.in_TABLE = true;
            }else if (localName.equals("TBODY")) {
                    this.in_TBODY = true;
            }else if (localName.equals("TR class=alternativerow")) {
                    this.in_TR = true;
            }else if (localName.equals("TD")) {
                    // Extract an Attribute
                    String attrValue = atts.getValue("TD");
                    int i = Integer.parseInt(attrValue);
                    myParsedExampleDataSet.setExtractedInt(i);
            }
    }

    /** Gets be called on closing tags like: 
     * </tag> */
    @Override
    public void endElement(String namespaceURI, String localName, String qName)
                    throws SAXException {
            if (localName.equals("TABLE class=personaltable cellSpacing=0 cellPadding=0")) {
                    this.in_TABLE = false;
            }else if (localName.equals("TBODY")) {
                    this.in_TBODY = false;
            }else if (localName.equals("TR class=alternativerow")) {
                    this.in_TR = false;
            }else if (localName.equals("TD")) {
                    // Nothing to do here
            }
    }

    /** Gets be called on the following structure: 
     * <tag>characters</tag> */
    @Override
public void characters(char ch[], int start, int length) {
            if(this.in_TD){
            myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
    }
}
Lars
  • 915
  • 4
  • 18
  • 27
  • 1
    The SAX parser is designed to parse XML, not HTML. Most HTML pages contain errors regarding their XML structure. For parsing HTML files you should better use a dedicated HTML parser. – Robert Oct 23 '11 at 12:23
  • Hi Robert, thx for answering. I am going to use the JSoup parser, ithink it is the best way to achieve my goal. – Lars Oct 23 '11 at 12:40

1 Answers1

1

to parse HTML page it is easier to use dom than SAX.

Also you can check this post it is answered how to parse html page

Community
  • 1
  • 1
Alex
  • 1,068
  • 8
  • 23
  • Hi ahmed, ok thx for the answer. I already saw the JSoup parser and i also have some code for it but i am strugling to get the 2 elements in a listview. Can you help? – Lars Oct 23 '11 at 12:39
  • Submit another question that represent your situation (the jSoup issue) and provide enough code to help others help you – ariefbayu Oct 23 '11 at 12:57
  • But DOM processing requires a lot of RAM. For larger pages this costs RAM and speed on Android. – Robert Oct 23 '11 at 16:31
  • it depend on the case and size of files. – Alex Oct 23 '11 at 21:49