Saxparser语法错误

时间:2011-10-19 19:23:56

标签: android saxparser

我正在尝试使用saxparser解析HTML / XML UTF-8文件,但我收到以下解析错误:

ERROR/ParseError:(26854): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 62: syntax error

以下是XML示例:

我想解析TD元素中的数据。

<!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">
<TABLE class=personaltable cellSpacing=0 cellPadding=0>
 <TBODY>
  <TR class=alternativerow>
   <TD>Nieuw beltegoed:</TD>
   <TD>€ 1,00</TD></TR>
  <TR>
   <TD>Tegoed vorige periode: 
   <TD>€ 2,00</TD></TD></TR>
  <TR class=alternativerow>
   <TD>Tegoed tot 09-11-2011: 
   <TD>€ 10,00</TD></TD></TR>
  <TR>
   <TD>
   <TD height=25></TD>
  <TR class=alternativerow>
   <TD>Verbruik sinds nieuw tegoed:</TD>
   <TD>€ 0,33</TD></TR>
  <TR>
   <TD>Ongebruikt tegoed:</TD>
   <TD>€ 12,00</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>
</html>

有人可以帮忙吗?

也可能有人检查代码,因为这是我的第一个saxparser代码。

提前谢谢。

这是我的代码:

try{    
                SAXParserFactory spf = SAXParserFactory.newInstance(); 
                SAXParser sp = spf.newSAXParser(); 
                XMLReader xr = sp.getXMLReader(); 

                ExampleHandler myExampleHandler = new ExampleHandler();
                xr.setContentHandler(myExampleHandler);

                // Create handler to handle XML Tags ( extends DefaultHandler )  
                xr.parse(new InputSource(getAssets().open("File parse.htm")));

                ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();

                tv.setText("");
                tv.setText(parsedExampleDataSet.toString());

            } catch (Exception e) {
                /* Display any Error to the GUI. */
                tv.setText("Error: " + e.getMessage());
                Log.e("ParseError: ", "WeatherQueryError", e);
            }

处理程序本身:

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

1 个答案:

答案 0 :(得分:1)

简单回答

由于以下原因,您的XML格式不正确:

  • 您有不带引号的属性。必须引用所有属性值。
  • 您有未公开的TDTR元素。必须关闭所有元素(即必须具有匹配的结束标记)。

您需要在继续之前修复这些错误。

教人解答

您需要投入(您的时间)在XML工具中,以产生更好的错误消息。将您的文档放入任何体面的格式良好的验证器都会立即揭示这些问题。