使用JAXB将Java对象编组为XML [英] Marshalling Java Objects into XML using JAXB

查看:127
本文介绍了使用JAXB将Java对象编组为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB将Java对象编组为XML。我正在使用的ide是Windows OS中的JDeveloper 11.1.1.7.0。每当我运行特定的java代码时,我会收到一条错误消息,上面写着

I'm using JAXB to marshall Java Objects into XML. The ide i'm using is JDeveloper 11.1.1.7.0 in windows OS. Whenever i run that particular java code i get an error message which says

"Error(1,1): file:/C:/JDeveloper/mywork/bugdashboard/Model/src/model/BugReport.xml<Line 1, Column 1>: XML-20108: (Fatal Error) Start of root element expected."

即使代码中似乎没有错误仍然无法获得所需输出..请帮助..

Even though there seem to be no errors in the code still i'm not able to get the desired output..Please Help..

我的JAVA代码..

My JAVA code..

     package model;


     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 JavaObvjecttoXml {



     public void xmlGenerator() {

    //super();
    JavaServiceFacade fcd = new JavaServiceFacade();
    bugvalue track, track1, track2;
    List<ReportDto> bugReport, bugrePort1, bugrePort2;
    List<bugvalue> reportMetaData= new ArrayList<bugvalue>();    
    ReportMetaData rmd = new ReportMetaData();



    try {
        rmd.setBugreportmetadata(new ArrayList<bugvalue> ());
        JAXBContext context = JAXBContext.newInstance(ReportMetaData.class);

        Marshaller marshaller;
        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        track = new bugvalue();
        bugReport = fcd.getBugSeverityReport();
        track.setBugReport(bugReport);
        track.setLabel("Bug by severity");
        track.setTile("severity");
        track.setChatType("PIE");
        track.setXAxisLabel("Label");
        track.setYAxisLAbel("Label Count");

        track1 = new bugvalue();
        bugrePort1 = fcd.getBugStatusReport();
        track1.setBugReport(bugrePort1);
        track1.setLabel("Bug by Status");
        track1.setTile("status");
        track1.setChatType("bar");
        track1.setXAxisLabel("count");
        track1.setYAxisLAbel("Label");

        track2 = new bugvalue();
        bugrePort2 = fcd.getBugCategoryReport();
        track2.setBugReport(bugrePort2);
        track2.setLabel("Bug by Category");
        track2.setTile("category");
        track2.setChatType("PIE");
        track2.setXAxisLabel("count");
        track2.setYAxisLAbel("Label"); 

        reportMetaData.add(track);
        reportMetaData.add(track1);
        reportMetaData.add(track2);
        rmd.setBugreportmetadata(reportMetaData);
        File output = new File("C:\\JDeveloper\\mywork\\bugdashboard\\Model\\src\\model\\BugReport.xml");
        marshaller.marshal(rmd, output);

        }
    catch(JAXBException e){
        e.printStackTrace();

    }







    }


     /**
     * @param args
     */

     public static void main(String[] args) {

    JavaObvjecttoXml obj = new JavaObvjecttoXml();
       obj.xmlGenerator(); 
   }


  }

文件ReportMetaData。 java

File ReportMetaData.java

    package model;

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

    @ XmlRootElement(name = "ReportMetaData")

    public class ReportMetaData {

    private List<bugvalue> bugreportmetadata = new ArrayList<bugvalue>();

    public List<bugvalue> getBugreportmetadata() {
      return bugreportmetadata;
    }

    */ @param bugreportmetadata
    */
    public void setBugreportmetadata(List<bugvalue> bugreportmetadata) {
       this.bugreportmetadata = bugreportmetadata;
    }

     public ReportMetaData() {
    super();
    }
    }

错误信息..

文件BugReport.xml

The file BugReport.xml

从上面的屏幕截图中可以看到,正在生成文件BugReport.xml但它是空的..

As you can see from the above screen shot the file BugReport.xml is being generated but it's empty..

推荐答案

我怀疑当您将对象设置为XML时,需要注释要输出的字段。 Java和贸易;我使用的JAX-B模式编译器生成的源将以下注释应用于对象属性定义:

I suspect you need to annotate the fields you intend to output when you mashall the object to XML. Java™ sources generated by the JAX-B schema compiler I use apply the following annotation to the object attribute definitions:

@XmlElement(required = true)

在这种情况下,ReportMetaData类的相关部分如下所示:

In this case, the relevant section of your ReportMetaData class would look like this:

@XmlElement(required = true)
private List<bugvalue> bugreportmetadata = new ArrayList<bugvalue>();

JAX-B模式编译器还会在@XmlRootElement注释之前生成两个额外的注释:

The JAX-B schema compiler also generates two additional annotations before the @XmlRootElement annotation:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
   "bugreportmetadata"
})

在JAX-B生成的类结构中,每个类定义都有这些注释。此外,嵌套类定义的@XmlType注释包含类型的名称,因此bugvalue类的注释可能如下所示:

In a JAX-B generated class structure, each class definition has these annotations. In addition, the @XmlType annotations for the nested class definitions contain the name of the type, so that the annotation for the bugvalue class might look like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "bugvalue", propOrder = {
  // TODO: list the fields for BugValue as defined
})

bug值的全小写定义,取决于JAX-B编组的编码,也可能造成问题。在编组对象时,JAX-B处理器必须动态访问和定义类,并从中生成元素标识符。如果JAX-B处理器依赖于Java命名约定来执行此操作,则不遵守这些约定的代码可能会失败。

The all lower case definition of bugvalue, depending on the coding for the JAX-B marshaller, may also create a problem. When marshalling an object, the JAX-B processor has to dynamically access and class definitions and generate element identifiers from them. If the JAX-B processor relies on Java naming conventions to do this, code that does not adhere to these conventions may potentially fail.

这篇关于使用JAXB将Java对象编组为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆