2013-10-23 140 views
-4

我有一个任务:Java中创建XML输出文件

“的任务是输出与所有统计结果的XML文件,这将是巨大的,如果用于观看所产生的XML很好地使用网页浏览器的XSL文件还提供了“

表示计数结果是这样的:。

Feta Sushi;12.61;5.00;9.22;1.50;60.39;16.43;21.60;2.60;35.81;5.25.72 
Siemak Beata;13.04;4.53;7.79;1.55;64.72;18.74;24.20;2.40;28.20;6.50.76 
Hodson Wind;13.75;4.84;10.12;1.50;68.44;19.18;30.85;2.80;33.88;6.22.75 
Seper Loop;13.43;4.35;8.64;1.50;66.06;19.05;24.89;2.20;33.48;6.51.01 

我不知道输出如何将数据从Java应用程序的XML文件。还应该如何生成XSL文件?

如果有人抽空指导我如何做到这一点,那将会很棒。

+0

你可以通过采取通读[Java API来处理XML(JAXP)](HTTP://开头docs.oracle.com/javase/tutorial/jaxp/) – MadProgrammer

+0

创建Java对象。使用JAXB从java对象生成XML。写一个xsl文件以所需的格式显示XML – Shamse

回答

0
 /* 
     * To change this template, choose Tools | Templates 
     * and open the template in the editor. 
     */ 
     package com.rest.client; 

     import java.util.ArrayList; 
     import java.util.List; 
     import javax.xml.bind.annotation.XmlAttribute; 
     import javax.xml.bind.annotation.XmlElement; 
     import javax.xml.bind.annotation.XmlRootElement; 

     /** 
     * 
     * @author sxalam 
     */ 
     @XmlRootElement(name = "tasks") 
     public class Task { 

      String name; 

      List<Double> task; 

      public String getName() { 
       return name; 
      } 

      public void setName(String name) { 
       this.name = name; 
      } 

      public List<Double> getTask() { 
       return task; 
      } 

      public void setTask(List<Double> task) { 
       this.task = task; 
      } 


     } 

使用下面的类从Java对象生成XML

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 

public class JAXBJavaToXml { 

public static void main(String[] args) { 

    // creating country object 
    Task task = new Task();   
    task.setName("Feta Sushi"); 

    List<Double> counter = new ArrayList<Double>(); 
    counter.add(12.5); 
    counter.add(10.90); 

    task.setTask(counter); 
    try { 

     // create JAXB context and initializing Marshaller 
     JAXBContext jaxbContext = JAXBContext.newInstance(Task.class); 
     Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 

     // for getting nice formatted output 
     jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 

     //specify the location and name of xml file to be created 
     File XMLfile = new File("C:\\task.xml"); 

     // Writing to XML file 
     jaxbMarshaller.marshal(task, XMLfile); 
     // Writing to console 
     jaxbMarshaller.marshal(task, System.out);    

    } catch (JAXBException e) { 
     // some exception occured 
     e.printStackTrace(); 
    } 

} 

}