“无法找到合适的 SAX2 解析器”当我尝试使用 XOM 教程解析 XML 时

发布于 2024-11-25 19:30:51 字数 3419 浏览 2 评论 0原文

我正在尝试找到一种简单的方法来在我的 Android 应用程序中获取 XML 内容。我尝试了一下XOM。创建 XML 没有问题,但是当我尝试解析某些内容时...模拟器因强制关闭而崩溃。我不知道该去哪里看。

我在清单中设置了互联网权限。

在调试/变量中,我可以看到“找不到合适的SAX2解析器”,在logcat中也可以看到相同的内容,

07-22 12:27:51.565: INFO/System.out(683): debugger has settled (1337)
07-22 12:27:52.034: INFO/dalvikvm(683): Could not find method org.apache.xerces.impl.Version.getVersion, referenced from method nu.xom.Builder.<clinit>
07-22 12:27:52.034: WARN/dalvikvm(683): VFY: unable to resolve static method 2371: Lorg/apache/xerces/impl/Version;.getVersion ()Ljava/lang/String;
07-22 12:27:52.044: DEBUG/dalvikvm(683): VFY: replacing opcode 0x71 at 0x000f
07-22 12:27:52.044: DEBUG/dalvikvm(683): VFY: dead code 0x0012-0049 in Lnu/xom/Builder;.<clinit> ()V
07-22 12:27:52.094: WARN/dalvikvm(683): Unable to resolve superclass of Lnu/xom/XML1_0Parser; (402)
07-22 12:27:52.094: WARN/dalvikvm(683): Link of class 'Lnu/xom/XML1_0Parser;' failed
07-22 12:27:52.104: ERROR/dalvikvm(683): Could not find class 'nu.xom.XML1_0Parser', referenced from method nu.xom.Builder.findParser
07-22 12:27:52.104: WARN/dalvikvm(683): VFY: unable to resolve new-instance 191 (Lnu/xom/XML1_0Parser;) in Lnu/xom/Builder;
07-22 12:27:52.104: DEBUG/dalvikvm(683): VFY: replacing opcode 0x22 at 0x0000
07-22 12:27:52.114: DEBUG/dalvikvm(683): VFY: dead code 0x0002-0007 in Lnu/xom/Builder;.findParser (Z)Lorg/xml/sax/XMLReader;
07-22 12:27:52.554: WARN/dalvikvm(683): Unable to resolve superclass of Lnu/xom/JDK15XML1_0Parser; (8)
07-22 12:27:52.554: WARN/dalvikvm(683): Link of class 'Lnu/xom/JDK15XML1_0Parser;' failed
07-22 12:27:58.441: WARN/ActivityManager(64): Launch timeout has expired, giving up wake lock!
07-22 12:27:58.996: WARN/ActivityManager(64): Activity idle timeout for HistoryRecord{44fdcb80 com.xb.xomtest/.main}

但我没有得到的一件事是,当我将jar添加到构建路径时会出现警告。不知道是否与此有关:

[2011-07-22 14:38:13 - XOM] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class
(nu.xom.Element$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.

代码:

package com.xb.xomtest;

import java.io.IOException;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.ParsingException;
import android.app.Activity;
import android.os.Bundle;

public class main extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
              Builder parser = new Builder();
              Document doc = parser.build("http://www.cafeconleche.org/");
            }
            catch (ParsingException ex) {
              //System.err.println("Cafe con Leche is malformed today. How embarrassing!");
            }
            catch (IOException ex) {
              //System.err.println("Could not connect to Cafe con Leche. The site may be down.");
            }


    }
}

I'm trying to find a simple way to get XML content in my Android app. I gave XOM a try. Creating XML is no problem, but when i try to parse some... the emulator crashes with a force close. I have no idea where to look.

I have the internet permissions set in the manifest.

In debug/variables i can see "Could not find a suitable SAX2 parser" and in logcat something the same

07-22 12:27:51.565: INFO/System.out(683): debugger has settled (1337)
07-22 12:27:52.034: INFO/dalvikvm(683): Could not find method org.apache.xerces.impl.Version.getVersion, referenced from method nu.xom.Builder.<clinit>
07-22 12:27:52.034: WARN/dalvikvm(683): VFY: unable to resolve static method 2371: Lorg/apache/xerces/impl/Version;.getVersion ()Ljava/lang/String;
07-22 12:27:52.044: DEBUG/dalvikvm(683): VFY: replacing opcode 0x71 at 0x000f
07-22 12:27:52.044: DEBUG/dalvikvm(683): VFY: dead code 0x0012-0049 in Lnu/xom/Builder;.<clinit> ()V
07-22 12:27:52.094: WARN/dalvikvm(683): Unable to resolve superclass of Lnu/xom/XML1_0Parser; (402)
07-22 12:27:52.094: WARN/dalvikvm(683): Link of class 'Lnu/xom/XML1_0Parser;' failed
07-22 12:27:52.104: ERROR/dalvikvm(683): Could not find class 'nu.xom.XML1_0Parser', referenced from method nu.xom.Builder.findParser
07-22 12:27:52.104: WARN/dalvikvm(683): VFY: unable to resolve new-instance 191 (Lnu/xom/XML1_0Parser;) in Lnu/xom/Builder;
07-22 12:27:52.104: DEBUG/dalvikvm(683): VFY: replacing opcode 0x22 at 0x0000
07-22 12:27:52.114: DEBUG/dalvikvm(683): VFY: dead code 0x0002-0007 in Lnu/xom/Builder;.findParser (Z)Lorg/xml/sax/XMLReader;
07-22 12:27:52.554: WARN/dalvikvm(683): Unable to resolve superclass of Lnu/xom/JDK15XML1_0Parser; (8)
07-22 12:27:52.554: WARN/dalvikvm(683): Link of class 'Lnu/xom/JDK15XML1_0Parser;' failed
07-22 12:27:58.441: WARN/ActivityManager(64): Launch timeout has expired, giving up wake lock!
07-22 12:27:58.996: WARN/ActivityManager(64): Activity idle timeout for HistoryRecord{44fdcb80 com.xb.xomtest/.main}

One thing that i don't get is the warning when i add the jar to my buildpath. Don't know if something has to do with that:

[2011-07-22 14:38:13 - XOM] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class
(nu.xom.Element$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.

The code:

package com.xb.xomtest;

import java.io.IOException;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.ParsingException;
import android.app.Activity;
import android.os.Bundle;

public class main extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
              Builder parser = new Builder();
              Document doc = parser.build("http://www.cafeconleche.org/");
            }
            catch (ParsingException ex) {
              //System.err.println("Cafe con Leche is malformed today. How embarrassing!");
            }
            catch (IOException ex) {
              //System.err.println("Could not connect to Cafe con Leche. The site may be down.");
            }


    }
}

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

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

发布评论

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

评论(1

卖梦商人 2024-12-02 19:30:51

xom jar 本身并不包含您需要的所有内容 - 并且显然在其合适的解析器列表中也不包含标准的 android 解析器。

摘自 http://www.xom.nu/install.xhtml

“Java 1.3 及更早版本没有内置的 XML 解析器,因此在这些环境中您还需要安装 XOM 的支持库,这些库包括 xalan.jar、xercesImpl.jar、normalizer.jar 和 xml-apis.jar,可在以下位置找到。 lib 目录。与 JDK 捆绑的版本相比,XOM 附带的版本要快得多,而且错误更少,因此即使在 Java 1.4 及更高版本中,您也可能希望使用它们”

对于基本解析,请添加 xercesImpl.jar。使用 xom-1.2.7.jar 似乎就足够了 - 我的代码,
有同样的问题(抱怨缺少 sax2 解析器)现在解析输入正常。

我收到关于内部类的相同 Dx 警告,因此这似乎不是致命的:-)。

The xom jar doesn't, by itself, include everything you need - and apparently doesn't include the standard android parser in its list of suitable ones.

excerpt from http://www.xom.nu/install.xhtml:

"Java 1.3 and earlier do not have a built-in XML parser so in these environments you'll also need to install XOM's supporting libraries. These include xalan.jar, xercesImpl.jar, normalizer.jar, and xml-apis.jar, and are found in the lib directory. The versions shipped with XOM are quite a bit faster and less buggy than the ones bundled with the JDK, so you may well want to use them even in Java 1.4 and later. "

For basic parsing, adding xercesImpl.jar along with xom-1.2.7.jar appears to be sufficient - my code,
which had the same problem (complaining about a missing sax2 parser) now parses the input OK.

I get the same Dx warning about inner classes, so that doesn't appear to be fatal :-).

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