【问题标题】:JAXB marshalling Java to output XML fileJAXB 编组 Java 以输出 XML 文件
【发布时间】:2012-11-27 03:19:15
【问题描述】:

问题是我如何生成 XML 文件输出而不是 system.out?

package jaxbintroduction;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        itemorder.Book quickXML = new itemorder.Book();

        quickXML.setAuthor("Sillyme");
        quickXML.setDescription("Dummie book");
        quickXML.setISBN(123456789);
        quickXML.setPrice((float)12.6);
        quickXML.setPublisher("Progress");
        quickXML.setTitle("Hello World JAVA");

        try {            
            javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(quickXML.getClass().getPackage().getName());
            javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(quickXML, System.out);
            OutputStream os = new FileOutputStream( "nosferatu.xml" );
            marshaller.marshal( quickXML, os );

        } catch (javax.xml.bind.JAXBException ex) {
            // XXXTODO Handle exception
            java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
        }
    }

}

【问题讨论】:

  • 你查看过 API 吗?

标签: java xml jaxb marshalling


【解决方案1】:

您已经在编组到nosferatu.xml。只需删除或评论该行:

marshaller.marshal(quickXML, System.out);

如果您不想显示输出并关闭OutputStream

os.close();

【讨论】:

【解决方案2】:

Marshaller#marshall(...) 方法将 OutputStream 或 Writer 作为参数。如果你看过的话,你肯定会在 API 中找到这个。

【讨论】:

    【解决方案3】:

    如果您使用的是 JAXB 2.1 或更高版本,则可以直接编组到 java.io.File 对象:

     File file = new File( "nosferatu.xml" );
     marshaller.marshal( quickXML, file );
    

    对应的Javadoc

    【讨论】:

      【解决方案4】:

      这只是一个从java对象到xml文件的转换过程。 它与序列化非常相似,您必须确定序列化和编组。 在这里,我已经完成了编组的示例。您可以以类似的方式进行解组。

      带有 jaxp 注释的 bean 类:

      package com.ofs.swinapps;
      
      import javax.xml.bind.annotation.XmlRootElement;
      
          @XmlRootElement
          public class Employee {
          private String name;
          private String id;
          private String department;
          private int age;
          private int salary;
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
          public String getId() {
              return id;
          }
          public void setId(String id) {
              this.id = id;
          }
      
          public String getDepartment() {
              return department;
          }
      
          public void setDepartment(String department) {
              this.department = department;
          }
      
          public int getAge() {
              return age;
      }
      
          public void setAge(int age) {
              this.age = age;
          }
      
          public int getSalary() {
              return salary;
          }
          public void setSalary(int salary) {
              this.salary = salary;
          }
          }
      

      编组:

      import java.io.File;
      import javax.xml.bind.JAXBContext;
      import javax.xml.bind.JAXBException;
      import javax.xml.bind.Marshaller;
      
      public class Java2XMLExample {
          public static void main(String[] args) throws JAXBException {
       Employee employee = new Employee();
       employee.setName("Kowthal ganesh");
       employee.setAge(23);
       employee.setDepartment("Chola-ccms");
       employee.setId("947");
       employee.setSalary(8333);
       File file = new File("D:/build.xml");
       JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
       Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
       jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
       jaxbMarshaller.marshal(employee, file);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多