Saxparser语法错误

发布于 2024-12-10 14:04:47 字数 5324 浏览 1 评论 0原文

我正在尝试使用 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));
    }

I am trying to parse a HTML/XML UTF-8 file with saxparser but i am getting the following parse error:

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

Here is the XML example:

I want to parse the data in the TD elements.

<!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>

Can somebody please help?

Could also someone check the code, as this is my first code for the saxparser.

Thank you in advance.

Here is my code:

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

The handler itself:

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

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

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

发布评论

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

评论(1

落墨 2024-12-17 14:04:47

简单回答

您的 XML 格式不正确,原因如下:

  • 您有未加引号的属性。所有属性值都必须加引号。
  • 您有未封闭的 TDTR 元素。所有元素都必须闭合(即必须具有匹配的结束标记)。

您需要先修复这些错误,然后才能继续。

授人以渔 答案

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

Simple Answer

Your XML is not well-formed for the following reasons:

  • You have unquoted attributes. All attribute values must be quoted.
  • You have unclosed TD and TR elements. All elements must be closed (i.e. must have a matching end tag).

You need to fix those errors before continuing.

Teach-a-man-to-fish Answer

You need to invest (your time) in an XML tool that produces better error messages. Dropping your document into any decent well-formedness validator would have revealed these issues immediately.

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