从XMLResourceParser获取org.w3c.dom.Document [英] Get org.w3c.dom.Document from XMLResourceParser

查看:199
本文介绍了从XMLResourceParser获取org.w3c.dom.Document的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划将一些XML文件放入res / xml目录中,并希望将其加载到Document对象中(因为我目前正在应用程序中使用这些文件,并且将它们大量传递)。

I'm planning on putting some XML files in my res/xml directory, which I want to load into Document objects (as I'm using these currently in my application and am passing them around a lot).

这样做:

XMLResourceParser parser = getResources().getXml(R.xml.my_xml_file)

似乎是唯一的起点,并且XMLResourceParser实现了XmlPullParser,这似乎是针对

seems to be the only starting point and the XMLResourceParser implements XmlPullParser, which seems to be aimed at iterating through the tree of nodes.

我尝试使用xpath提取文档的根节点,根据此答案中的代码:使用XMLpullParser来浏览XML文档,但这给了我以下问题的例外:< a href = https://stackoverflow.com/questions/34833310/evaluating-xpath-an-xmlresourceparser-causes-exception>评估XPath和XmlResourceParser导致异常

I've tried using xpath to pull out the root node of the document, as per the code in this answer: navigate through XML document with XMLpullParser, but this gives me an exception as per this question: Evaluating XPath an XmlResourceParser causes Exception

理想情况下,我需要将xml保留在res / xml文件夹中,因为我想对内容进行本地化,因此将其移至res / raw或res / assets是不可行的

Ideally, I need to keep the xml in the res/xml folder, as I'll want to localise the content, so moving it to res/raw or res/assets isn't an option.

在实现通过XmlResourceParser迭代并生成Document的东西之前,有没有更简单的方法?

Before I implement something which iterates through the XmlResourceParser and builds the Document, is there a simpler way of doing this?

推荐答案

编写要通过 XMLResourceParser 进行迭代的代码,表明您是一个态度很好的开发人员。

Writing the code to iterate through the XMLResourceParser shows that you are a nice well-mannered developer.

但是,您可能会想使用邪恶的天才选项。

However, you may be tempted to use the evil genius option.

首先,创建一个 XmlPullParser 实现在构造函数中采用 XmlResourceParser 的实现,将其包装并将其所有方法委托给它-除了所有 setInput 类型的方法。您希望所有这些都是无操作存根,因为 XmlResourceParser 会引发异常(请参见 XmlBlock.java ,第108行左右)。 Android Studio可以自动创建委托方法,因此您无需手动编写所有代码。

First, create an XmlPullParser implementation that takes an XmlResourceParser in the constructor, wraps it and delegates all its methods to it -- except all the setInput-type methods. You want all those to be no-op stubs, because XmlResourceParser will throw exceptions (see XmlBlock.java, line 108 or so). Android studio can automate creating the delegate methods so you don't have to hand-code all that.

    PullParserWrapper wrapper = new PullParserWrapper(
            getResources().getXml(R.xml.my_xml_file));

注意:您的包装器类可能必须具有一些方法实现来关闭名称空间处理和其他分类

Note: Your wrapper class might have to have some method implementations to handle turning off namespace processing and other assorted things.

接下来,您将使用 org.xmlpull.v1.sax2.Driver 包裹解析器并将其转换为 XMLReader

Next, you will use a org.xmlpull.v1.sax2.Driver to wrap your parser and convert it into an XMLReader:

    XMLReader xmlReader = new Driver(wrapper);

现在设置一个虚拟输入源( XmlResourceParser 已经知道从哪里获取输入了:)

Now set up a dummy input source (the XmlResourceParser already knows where it's getting its input):

    InputSource inputSource = new InputSource(new StringReader(""));

然后您使用 Transformer 转换您的将SAX输入到DOM输出并获得结果:

Then you use a Transformer to convert your SAX input to a DOM output and get your result:

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        DOMResult domResult = new DOMResult();
        transformer.transform(new SAXSource(xmlReader, inputSource), domResult);
        Document document = (Document) domResult.getNode();

MWAH哈哈哈哈哈哈哈!

MWAH Ha Ha Ha Ha Ha Ha!

这篇关于从XMLResourceParser获取org.w3c.dom.Document的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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