有没有办法通过Intent和Parcelable传递DOM对象? [英] Is there any way to pass a DOM Object via an Intent and Parcelable?

查看:149
本文介绍了有没有办法通过Intent和Parcelable传递DOM对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过Intent和Parcelable传递JAXP节点或文档? JAXP不执行Parcelable,所以答案可能是--no。有没有DOM库实现Parcelable?有人可以提供一个例子吗?



序列化不是一个选择;令人讨厌的表现受到打击。将数据存储在res / xml中不是一个选项:必须最终(通过项目结束)在磁盘上加密。 Android的编译XML访问工具不支持解密整个XML。当然,我可以自己做一个课程。



这是我的启动程序代码,充斥着XML。我的目标是将节点或文档从一个ListView传递给另一个,通过列表有效地钻取DOM。



我的文档包含所有活动需要共享的信息。每个活动访问不同的节点,并提取新的信息。我考虑过通过全球曝光文档,但我认为这不会对多个活动进行访问是安全的。



另外,在工作中下面的代码我打算通过一个节点到第二个ListActivity而不是一个String,只是没有得到那么远。

 包com.example 

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget。*;
import android.widget.AdapterView.OnItemClickListener;

public class JAXPListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

文档doc = null;
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(new File(/ sdcard / example.xml));

} catch(Exception e){
e.printStackTrace(); // TO-DO:异常处理
}

NodeList nodes = doc.getChildNodes();
String [] nodeList = new String [nodes.getLength()]; (int i = 0; i< nodes.getLength(); i ++){
Node node = nodes.item(i);


nodeList [i] = nodes.item(i).getNodeName();
}

this.setListAdapter(new ArrayAdapter< String>(this,
R.layout.list_item,
R.id.label,nodeList));

ListView lv = getListView();

lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view,
int position,long id){

String nodeName =((TextView)视图).getText()。toString();

Intent i = new Intent(getApplicationContext(),
JAXPNodeListActivity.class);

i.putExtra(nodeName,nodeName);
startActivity(i);
}
});
}
}


解决方案

你可以简单地使用Singleton类。喜欢:

  public class DomObjectManager {

private static DomObjectManager INSTANCE;
私人文件doc;

public static DomObjectManager getInstance(){

if(INSTANCE == null){
INSTANCE = new DomObjectManager();
}
return INSTANCE;
}
private DomObjectManager(){
}
//设置共享文档;
public void setDocument(Document doc){
this.doc = doc;
}
public void getDocument(){
retunr doc;
}
//当您不再需要单例时,调用以前销毁。
public void destroy(){
INSTANCE == null;
}

}


Is there any way to pass a JAXP Node or Document via an Intent and Parcelable? JAXP doesn't implement Parcelable, so the answer is probably --no. Is there any DOM library that implements Parcelable? Can someone provide a working example of that?

Serialization isn't an option; nasty performance hit. Storing the data in res/xml is not an option: it must eventually (by end of project) be encrypted on disk. Android's "compiled" XML Access Tools do not support decrypting the whole XML. Of course I can do it myself with a class.

Here's my starter code that inflates the XML. My goal is to pass a Node or Document from one ListView to another, effectively drilling down the DOM via Lists.

My Document contains information that all activities need to share. Each activity accesses different Nodes, and extracts new information. I've considered exposing the Document via a global, but I don't think it will be safe for multiple Activities to access it that way.

Also, in the working code below I intend to pass a Node to the second ListActivity rather than a String, just haven't gotten that far.

package com.example

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class JAXPListActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Document doc = null;
        try {
            DocumentBuilderFactory factory = 
                        DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            doc = builder.parse(new File("/sdcard/example.xml"));

        } catch (Exception e) {
            e.printStackTrace(); //TO-DO: exception handling
        }

        NodeList nodes = doc.getChildNodes();
        String[] nodeList = new String[nodes.getLength()];

        for(int i = 0; i<nodes.getLength(); i++) {
            Node node = nodes.item(i);
            nodeList[i] = nodes.item(i).getNodeName();
        }

        this.setListAdapter(new ArrayAdapter<String>(this, 
                R.layout.list_item, 
                R.id.label, nodeList));

        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, 
                        int position, long id) {

                String nodeName = ((TextView) view).getText().toString();

                Intent i = new Intent(getApplicationContext(), 
                                JAXPNodeListActivity.class);

                i.putExtra("nodeName", nodeName);
                startActivity(i);
            }
        });
    }
}

解决方案

You can simply use a Singleton class. Like:

public class DomObjectManager{

private static DomObjectManager INSTANCE;
private  Document doc;

public static DomObjectManager getInstance(){

if(INSTANCE==null){
INSTANCE = new DomObjectManager();
}
return INSTANCE;
}
private DomObjectManager(){
}
//set the shared document;
public void setDocument(Document doc){
this.doc = doc;
}
public void getDocument(){
retunr doc;
}
//call to destroy before when you do not need the singleton any more.
public void destroy(){
INSTANCE == null;
}

}

这篇关于有没有办法通过Intent和Parcelable传递DOM对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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