如何在android的xml文件 [英] how to create xml file in android

查看:123
本文介绍了如何在android的xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

家伙,我有一个问题,我的code给出了一个例外的权限,当我们写的android一个XML拒绝。任何一个可以告诉大家,如何将被删除。

 包com.ex.createXml;

进口android.app.Activity;
进口android.os.Bundle;
进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口org.xmlpull.v1.XmlSerializer;
进口android.os.Environment;
进口android.util.Log;
进口android.util.Xml;
进口android.widget.TextView;


公共类createXml延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        文件newxmlfile =新的文件(/数据/ new.xml);
        尝试{
            newxmlfile.createNewFile();
        }赶上(IOException异常E)
        {
            Log.e(IOException异常,异常中创建新的文件();
        }
        FileOutputStream中fileos = NULL;
        尝试{
            fileos =新的FileOutputStream(newxmlfile);

        }赶上(FileNotFoundException异常E)
        {
            Log.e(FileNotFoundException异常,e.toString());
        }
        XmlSerializer的序列化= Xml.newSerializer();
        尝试{
        serializer.setOutput(fileos,UTF-8);
        serializer.startDocument(NULL,通过Boolean.valueOf(真));
        serializer.setFeature(http://xmlpull.org/v1/doc/features.html#indent-output,真正的);
        serializer.startTag(NULL,根);
        serializer.startTag(NULL,Child1);
        serializer.endTag(NULL,Child1);
        serializer.startTag(NULL,CHILD2);
        serializer.attribute(NULL,属性,价值);
        serializer.endTag(NULL,CHILD2);
        serializer.startTag(NULL,Child3);
        serializer.text(里子3部分文字);
        serializer.endTag(NULL,Child3);
        serializer.endTag(NULL,根);
        serializer.endDocument();
        serializer.flush();
        fileos.close();
        // TextView的电视=(TextView中)findViewById(R);

        }赶上(例外五)
        {
            Log.e(异常,异常发生在wroting);
        }
    }
}
 

解决方案

试试这个创建或Android的读取文件。 处理文件

和创建/读取XML文件尝试使用XML 工作

如果您需要与XML相关的任何示范项目,比让我知道,我会尽力给你。

Guys i have a problem that my code gives an Exception as Permission Denied when we write a xml in android. can any one tell that How it will be removed.

package com.ex.createXml;

import android.app.Activity;
import android.os.Bundle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.xmlpull.v1.XmlSerializer;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;


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

        File newxmlfile = new File("/data/new.xml");
        try{
            newxmlfile.createNewFile();
        }catch(IOException e)
        {
            Log.e("IOException", "Exception in create new File(");
        }
        FileOutputStream fileos = null;
        try{
            fileos = new FileOutputStream(newxmlfile);

        }catch(FileNotFoundException e)
        {
            Log.e("FileNotFoundException",e.toString());
        }
        XmlSerializer serializer = Xml.newSerializer();
        try{
        serializer.setOutput(fileos, "UTF-8");
        serializer.startDocument(null, Boolean.valueOf(true));
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, "root");
        serializer.startTag(null, "Child1");
        serializer.endTag(null, "Child1");
        serializer.startTag(null, "Child2");
        serializer.attribute(null, "attribute", "value");
        serializer.endTag(null, "Child2");
        serializer.startTag(null, "Child3");
        serializer.text("Some text inside child 3");
        serializer.endTag(null,"Child3");
        serializer.endTag(null,"root");
        serializer.endDocument();
        serializer.flush();
        fileos.close();
        //TextView tv = (TextView)findViewById(R.);

        }catch(Exception e)
        {
            Log.e("Exception","Exception occured in wroting");
        }
    }
}

解决方案

Try this to create or read file in android. working with files

and to create/read XML file try Working with XML

If you need any demo project related to xml than let me know, i'll try to give you

这篇关于如何在android的xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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