2012-02-24 3 views
-1
public class LocalXMLActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); // set the layout.. it refers main.xml 
             // from the layout folder. 
     InputStream is; 
     DocumentBuilderFactory factory; 
     DocumentBuilder builder; 
     Document doc; 
     NodeList allTitles = null; 
     String dataAll; 
     try { 

      // Input Stream is used to read the bytes. getResources refers to 
      // the android resources(res)folder and open RawResource to the raw 
      // folder under res. (res/raw) 
      is = this.getResources().openRawResource(R.raw.data); 
      // Is to read the parser that prodcues from XML DOM. 
      factory = DocumentBuilderFactory.newInstance(); 
      // Once an instance of this class is obtained, XML can be parsed 
      // from a variety of input sources. These input sources are 
      // InputStreams, Files, URLs, and SAX InputSources. There are many 
      // sources to take data from internet. 
      builder = factory.newDocumentBuilder(); 
      // the Document represents the entire HTML or XML document. 
      // Conceptually, it is the root of the document tree, and provides 
      // the primary access to the document's data. 
      doc = builder.parse(is); 
      // Retrieving the "company" node from xml 
      allTitles = doc.getElementsByTagName("Company"); 

     } catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     dataAll = ""; 
     for (int i = 0; i < allTitles.getLength(); i++) { 
      dataAll += allTitles.item(i).getChildNodes().item(0).getNodeValue() + "\n"; 
     } 

     TextView tv = (TextView)findViewById(R.id.myTextView); // finds the 
                   // widget"myTextView" 
                   // from main.xml 
     tv.setText(dataAll); // setting the dataAll value to myTextView. 
    } 
} 

xmlファイル:RES /生/ data.xmlにこれは私のコードです。読んで書いて、アンドロイドのソースコード内のローカルXMLファイルの更新方法を書き留めていますか?

<?xml version="1.0" ?> 
<content> 
    <item> 
     <Company>Google</Company> 
     <Feature>Search Engine/Mail/Social Media</Feature> 
     <url>www.google.com</url> 
    </item> 
    <item> 
     <Company>Yahoo</Company> 
     <Feature>Search Engine/Mail</Feature> 
     <url>www.yahoo.com</url> 
    </item> 
</content> 

上記のコードはコードのみを読んで、私はちょうど読んでいるのres /生/ data.xmlに格納されたXMLファイルを持っていますしかし、ローカルxmlファイル

答えて

1

あなたは、このようなSDカード上に新しいファイルを作成することができますuの再生のためにあなたthak

File Directory= new File(Environment.getExternalStorageDirectory() 
         + File.separator + "working" +File.separator+"accounts"); 


File masterXMLfile = new File(Directory,"MasterCategories.xml"); 
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
        //Get the DocumentBuilder 
        DocumentBuilder docBuilder = null; 
       try { 
        docBuilder = factory.newDocumentBuilder(); 
       } catch (ParserConfigurationException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
        //Create blank DOM Document 
        Document doc = docBuilder.newDocument(); 


        //create the root element 
        Element root = doc.createElement("Categories"); 
        //all it to the xml tree 
        doc.appendChild(root); 
        for(int i =0; i < nodeArray.length;i++) 
        { 
         System.out.println("entered into if loop"); 
         Element subroot = doc.createElement("Category"); 
         root.appendChild(subroot); 
         Element nameNode = doc.createElement("Name"); 
         nameNode.appendChild(doc.createTextNode(nodeArray[i])); 
         subroot.appendChild(nameNode); 

         Element thumbNailNode = doc.createElement("thumbnail"); 
         thumbNailNode.appendChild(doc.createTextNode(nodeArray[i]+".jpg")); 
         subroot.appendChild(thumbNailNode); 
         Element descriptionNode = doc.createElement("description"); 
         descriptionNode.appendChild(doc.createTextNode(nodeArray[i]+" data")); 
         subroot.appendChild(descriptionNode); 
        } 
        TransformerFactory transfactory = TransformerFactory.newInstance(); 
        Transformer transformer= null; 
        try { 
         transformer = transfactory.newTransformer(); 
        } catch (TransformerConfigurationException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 

        // create string from xml tree 
        // StringWriter sw = new StringWriter(); 
        // StreamResult result = new StreamResult(sw); 
        FileWriter categoriesFW = null; 
        try { 
         categoriesFW = new FileWriter(masterXMLfile); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        StreamResult result = new StreamResult(categoriesFW); 
        DOMSource source = new DOMSource(doc); 
        try { 
         transformer.transform(source, result); 
        } catch (TransformerException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

       if(!masterXMLfile.exists()) 
       { 
        try { 
         masterXMLfile.createNewFile(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
+0

あなたがu再生をありがとう:このコードはme.iを使用していないxmlファイルとjavaファイル、javaファイルの読み書きにローカルXMLファイルを書き込む方法、コードを書き込むためにローカルXMLファイルを自動的に更新する方法。 plz私にコードを与える – Satya

+0

他の場所にしたい場合は、他のコードが同じになるファイルのパスを変更する場合は、SDカードにローカルファイルを作成するためのコードを与えた – user1203673

+0

iamは同じコードを使用してxmlファイルを作成フォルダは私がどこでエラーを持っているのか知ることができますし、エラーは何ですか – user1203673

0

まあの書き込みや更新に内部の読み取りソースコードの書き込みとローカルxmlファイルへの書き込みや更新を書く方法を、正直にそれは非常に紛らわしい書かれていますが、ここでは私の考えです:

FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/path/file.xml"); 
fos.write(byte_array_from_input); 
fos.close(); 

これは、あなたのsdcardに/path/file.xmlにファイルを書き込みます。小さなファイルの場合は1つの処理で、大きなデータセットの場合はループ処理を行うことができます。

+0

を。上記の私の上記のコードは読んでいますが、私のローカルXMLファイルに物を書き込んだり更新したりする方法はありません。 – Satya

+0

ありがとうございます。このコードはme.iを使用していません。ローカルのxmlファイルとjavaファイル、javaファイルの読み書き方法、ローカルXMLファイルの自動更新方法。 plz私にコードを与える – Satya

+0

ローカルxmlファイルは、あなたが資産または他の生のフォルダまたは他のデータフォルダまたは他のsdカードに保存したいと思う場所を意味する – user1203673

関連する問題