2015-03-03 45 views
-2
//The import files in use 

import java.io.FileWriter; 
import java.io.IOException; 
import javax.xml.stream.*; 

public class WriteXML { 

    public static void main(String[] args) { 
     WriteXML t = new WriteXML(); 
     t.m(); 
    } 

    private void m() { 

     XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); 

     try { 
     // Create the XMLStreamWriter object 
      // this is where it should create my file for xml 

      FileWriter fileWriter = new FileWriter("products.xml"); 
      XMLStreamWriter writer = outputFactory.createXMLStreamWriter(fileWriter); 

      //Here is the actual xml data   
      writer.writeStartDocument("1.0") 
      writer.writeComment("Product data"); 
      writer.writeStartElement("Products"); 

      writer.writeStartElement("Product"); 
      writer.writeAttribute("Code", "Java"); 

      writer.writeStartElement("Description"); 
      writer.writeCharacters("Java SE 6.0"); 
      writer.writeEndElement(); 
      // for the Product element 

      writer.writeEndElement(); 
      writer.writeStartElement("Product"); 
      writer.writeAttribute("Code", "C++"); 

      writer.writeStartElement("Description"); 
      writer.writeCharacters("C++ Programming"); 
      writer.writeEndElement(); 
      // for the Product element 

      writer.writeEndElement(); 
      writer.flush(); 
      writer.close(); 
     } //catch statements 
     catch (IOException e) { 
      e.printStackTrace(); 
     } catch (XMLStreamException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

A碼轉儲,使沒有問題。 'products.xml'將寫入當前工作目錄。您可以使用'new File(「。」)。getAbsolutePath()'並將其打印到控制檯以確定程序從何處執行... – MadProgrammer 2015-03-03 00:48:25

+0

您將如何將它合併到當前代碼中? – 2015-03-03 07:31:31

回答

相關問題