创建 XML 的 Java 类有效,但嵌入到 android 应用程序时出现异常 [英] A java class creating XML works, but gets exception when embedded to android app

查看:41
本文介绍了创建 XML 的 Java 类有效,但嵌入到 android 应用程序时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在插入我写到我的 android 应用程序中的 java 类时遇到了麻烦.作为一个独立的 Java 应用程序,它运行良好.一旦我接受了这些课程并将它们放入我的 android 应用程序中,我在尝试操作 xml 对象时就会出现异常.我使用以下内容:

Im having troubles inserting a java classes I wrote into my android application. As a standalone java application, it works fine. once I take the classes and put them inside my android app, I have an exception when trying to manipulate xml object. I use the following:

import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;

我所做的基本上是这样的:

What I do is basically this:

Document doc;
Element root = GetRootElement("root");
Element nameElement;
nameElement = doc.createElement("x");
nameElement.setTextContent("y");
root.appendChild(nameElement);

DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
return lsSerializer.writeToString(doc); //sends the xml's string somewhere else in the code

现在,将此代码作为常规 Java 应用程序运行是可行的,但在 android 应用程序中,它会在 doc.getImplementation() 行中失败,并出现以下错误:

Now, running this code as a regular java application works, but in an android app, it will fail in the doc.getImplementation() line, with the following error:

Caused by: java.lang.ClassCastException: org.apache.harmony.xml.dom.DOMImplementationImpl cannot be cast to org.w3c.dom.ls.DOMImplementationLS

我尽量提供尽可能多的信息,希望对您有所帮助.

I tried to give as much as info as possible, I hope it helps.

推荐答案

DOMImplementationLS 至少在 Android 4.1 上似乎不可用(我的设备正在运行该版本,因此我无法确认更新的版本).接口存在,但不是由您从 getImplementation() 获得的对象实现的,正如您所提到的.

DOMImplementationLS doesn't seem to be available on Android 4.1 at least (my device is running that version, so I can't confirm about newer ones). The interface exists, but isn't implemented by the object you get from getImplementation(), as you mention.

除了演员表,另一种官方获取这个对象的方法是这样的:

Apart from the cast, another official way to get this object is like this:

(DOMImplementationLS)doc.getImplementation().getFeature("LS", "3.0");

但是,这在 Android 4.1 上返回 null.

However, this returns null on Android 4.1.

我环顾四周,但不幸的是,我发现解决此问题的唯一方法是使用 Transformer 以旧"方式序列化 XML.这确实适用于我的设备,并且自 API 级别 8 (Android 2.2) 起就存在该界面.

I did some looking around but unfortunately, the only way I found to solve this problem is to serialize XML the 'old' way using a Transformer. This does work on my device, and the interface is present since API level 8 (Android 2.2).

Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(stringWriter));
String xml = stringWriter.toString();

这是一种较旧的方法,您也可以将其用于桌面 Java.但是,它的行为可能与 LSSerializer 不同,并且通常运行速度较慢.

This is an older method, and you can also rely on it for desktop Java. However, it might behave differently from the LSSerializer, and normally runs slower.

(注意:我上面的代码示例展示了如何序列化整个文档,因为这就是我所需要的.如果将单个元素放入 DOMSource 构造函数中,它可能会序列化单个元素,否则您可能必须创建一个新文档,在其中插入元素,然后将其序列化...)

(Note: My code example above shows how to serialize the entire document, because that's all I needed. It might work to serialize a single element if you put that in the DOMSource constructor, otherwise you might have to create a new document, insert the element in it, and serialize that...)

这篇关于创建 XML 的 Java 类有效,但嵌入到 android 应用程序时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆