2013-04-23 44 views
0

我想在使用Dom的Android中生成XML文件,但是當我執行應用程序時,不會生成XML文件,雖然我也沒有收到錯誤。請幫助我。在Android中使用DOM創建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 
+0

這是什麼, StreamResult result = new StreamResult(new File(「C:\\ f.xml」)); Android上是否存在C:\\? – 2013-04-23 16:50:21

+0

我使用我的系統的C驅動器來存儲文件,同時在模擬器上執行應用程序 – user2044296 2013-04-24 18:58:31

+0

我不認爲你可以使用模擬器訪問計算機的驅動器。 嘗試保存在SD卡上 - 使用getExternalStorageDirectory() – 2013-04-25 06:54:35

回答

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; 
    } 

} 
+0

我想保存在SD卡你可以說怎麼樣? – 2014-02-20 21:07:02

0

似乎要包括

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

有沒有這樣的目錄爲C:\ f.xml在android系統。使用Android的環境類訪問SDCard並在其中寫入文件