在Android中使用DOM创建XML文件

时间:2013-04-23 16:49:08

标签: android xml dom

我正在尝试使用Dom在Android中生成XML文件但是当我执行应用程序时,虽然我也没有收到错误,但是没有生成XML文件。请帮帮我。

package com.example.samplexml;


import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.w3c.dom.Document;
import org.w3c.dom.Element;


import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    TextView  myTextView;
     EditText E1;
     EditText E2;
     EditText E3;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);


   Button b= (Button) findViewById(R.id.button1);
    E1 = (EditText) findViewById(R.id.editText1);
    E2 = (EditText) findViewById(R.id.editText2);
    E3 = (EditText) findViewById(R.id.editText3);

   b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) { 




            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder;
            try {
                docBuilder = docFactory.newDocumentBuilder();


            // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("Class");
            doc.appendChild(rootElement);

            // staff elements
            Element student = doc.createElement("Student");
            rootElement.appendChild(student);


            // firstname elements
            Element firstname = doc.createElement("firstname");
            firstname.appendChild(doc.createTextNode(E1.getText().toString()));
            student.appendChild(firstname);

            Element Email = doc.createElement("Email");
            Email.appendChild(doc.createTextNode(E2.getText().toString()));
            student.appendChild(Email);

            // nickname elements
            Element Roll = doc.createElement("Roll_No");
            Roll.appendChild(doc.createTextNode(E3.getText().toString()));
            student.appendChild(Roll);


                    // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();



                Transformer transformer = transformerFactory.newTransformer();


            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("C:\\f.xml"));





                    transformer.transform(source, result);

                }
             catch (ParserConfigurationException e) {

                e.printStackTrace();
            }
             catch (TransformerException e) {

                    e.printStackTrace();}
            Toast.makeText(getApplicationContext(), 
                    "XML generated", Toast.LENGTH_LONG).show();


                                }
    }); }
}


    enter code here

2 个答案:

答案 0 :(得分:0)

这是你的解决方案

package com.example.dom_tutorial;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import android.widget.Toast;
import java.io.FileOutputStream;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CreateBlankDocument();
    }
    public void CreateBlankDocument(){
        Toast.makeText(getBaseContext(),
                "Creating Blank Document",
                Toast.LENGTH_LONG).show();
        try{
            //Create instance of DocumentBuilderFactory
            DocumentBuilderFactory factory = 
                    DocumentBuilderFactory.newInstance();
             //Get the DocumentBuilder
            DocumentBuilder parser = factory.newDocumentBuilder();
             //Create blank DOM Document
            Document doc=parser.newDocument();
            //create the root element
            Element root=doc.createElement("root");
            //all it to the xml tree
            doc.appendChild(root);
            //create a comment
            Comment comment=doc.createComment("This is a comment");
            //add in the root element
            root.appendChild(comment);
            //creat child element
            Element childelement=doc.createElement("child");
            //Add the attribute to the child
            childelement.setAttribute("value", "1");
            root.appendChild(childelement);

            TransformerFactory transformerfactory=
                    TransformerFactory.newInstance();
            Transformer transformer=
                    transformerfactory.newTransformer();

            DOMSource source=new DOMSource(doc);
            FileOutputStream _stream=getApplicationContext().openFileOutput("NewDom.xml", getApplicationContext().MODE_WORLD_WRITEABLE);
            StreamResult result=new StreamResult(_stream);
            transformer.transform(source, result);
            Toast.makeText(getBaseContext(),
                    "Done File created",
                    Toast.LENGTH_LONG).show();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

答案 1 :(得分:0)

好像你要包括

    StreamResult result = new StreamResult(new File("C:\\f.xml"));

android中没有C:\ f.xml这样的目录。使用android的Environment类来访问SDCard并在那里写你的文件