Android使用DOM来编辑XML时遇到的问题——无法保存

我一开始使用的是crimson.jar,是使用这个网址提供的方法。

可是,它一直会出现一个错误:请看这里

也就是说,如果我让它放在libs文件夹底下,让系统自动导入,就会说crimson使用到了系统库,它本身却不是系统库。如果我通过libaray导入,同时勾选“系统库”,那么android就会报运行时错误:找不到我要的那个函数(XMLDocument的强制类型转换)

最后似乎发现,我第一段说的那个文章,是开发web工程?也就是android根本就不支持crimson?

最后,我换了一种保存的方法,就可以运行了。


 

原来的方法:

    /**
     * 将一个Document文件写入指定的文件
     * @param doc 一个XMLCollection
     * @param path 保存路径
     */
    public static void writeAndSave(XMLCollection collection,String path){
        try{
            Document docu=collection.getDocument();
            ((XmlDocument)docu).write(new FileOutputStream(path));
        }catch(Exception e){
            e.printStackTrace();
        }
    }

现在的方法:

    /**
     * 将一个Document文件写入指定的文件
     * @param doc 一个XMLCollection
     * @param path 保存路径
     */
    public static void writeAndSave(XMLCollection collection,String path){
        String xmlWriter=null;
        
        try{
            Properties properties = new Properties();    
            properties.setProperty(OutputKeys.INDENT, "yes");    
            properties.setProperty(OutputKeys.MEDIA_TYPE, "xml");    
            properties.setProperty(OutputKeys.VERSION, "1.0");    
            properties.setProperty(OutputKeys.ENCODING, "GB2312");    
            properties.setProperty(OutputKeys.METHOD, "xml");    
            properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");    
                      
            TransformerFactory transformerFactory = TransformerFactory.newInstance();    
            Transformer transformer = transformerFactory.newTransformer();    
            transformer.setOutputProperties(properties);    
                      
            DOMSource domSource = new DOMSource(collection.getDocument().getDocumentElement());    
            OutputStream output = new ByteArrayOutputStream();    
            StreamResult result = new StreamResult(output);    
            transformer.transform(domSource, result);    
                      
            xmlWriter = output.toString();
        }catch(Exception e){
            e.printStackTrace();
        }
        //判断SDCard是否存在,是否允许读取或者写入文件
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            try{
                //使用SDCard的绝对路径
                //File file = new File("/mnt/sdcard", "test.txt");
                //获取SDCard所在路径,建议使用此方式
                Log.v(ACTIVITY_TAG,"输出路径:"+path);
                File file = new File(path);
                FileOutputStream outStream = new FileOutputStream(file);
                outStream.write(xmlWriter.getBytes());
                outStream.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }else{
            //TODO 没有sd卡
        }
    }

 

顺便把错误也贴出来:

放到libs文件夹底下的错误:

[2013-03-09 16:29:02 - SelectToDo] Dx
trouble processing "javax/xml/parsers/DocumentBuilder.class":

Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.

This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.

However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.

If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.

[2013-03-09 16:29:02 - SelectToDo] Dx 1 error; aborting
[2013-03-09 16:29:02 - SelectToDo] Conversion to Dalvik format failed with error 1
[2013-03-09 16:29:21 - SelectToDo] Dx
trouble processing "javax/xml/parsers/DocumentBuilder.class":

Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.

This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.

However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.

If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.

[2013-03-09 16:29:21 - SelectToDo] Dx 1 error; aborting
[2013-03-09 16:29:21 - SelectToDo] Conversion to Dalvik format failed with error 1
[2013-03-09 16:37:52 - SelectToDo] Dx
trouble processing "javax/xml/parsers/DocumentBuilder.class":

Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.

This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.

However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.

If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.

 

[2013-03-09 16:37:52 - SelectToDo] Dx 1 error; aborting
[2013-03-09 16:37:52 - SelectToDo] Conversion to Dalvik format failed with error 1

通过libaray导入的错误:

Could not find class 'org.apache.crimson.tree.XmlDocument', referenced from method com.turtle.selecttodo.XMLHelper.writeAndSave 


最后来点关键字,让百度能搜的到:

crimson crimson.jar jar android 出错 找不到 core libaray 导入库

posted @ 2013-03-10 13:14  fzyzcjy  阅读(1094)  评论(0编辑  收藏  举报