如何使用JAXB创建和解析XML?

时间:2014-03-29 08:48:02

标签: java xml jaxb

我有三个类在java(项目,图层,图像)中,我希望将所有这个文件解析为XML但我在解析器中遇到问题这个属性通过JAXB类项目对XML具有来自图层的属性和图层中的属性。  最终我需要什么:

  <?xml version="1.0" encoding="UTF-8"?> 
     <project  name="">
       <Layer id="1"> 
         <image id="1">
          </image>
         <image id="2">
          </image>
       </layer>
       <Layer id="2"> 
         <image id="1">
         </image>
         <image id="2">
         </image>
       </layer>
    <project>

项目类:

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Project {
  private String name;
  private Layer[] layer;
 @XmlAttribute(name = "name")
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
  @XmlElement(name = "layer")
  public Layer[] getLayer() {
       return layer;
   }
}

图层类

public class Layer {
  private int id;
  private Image[] images;
  @XmlElementWrapper(name="Layer")
  @XmlAttribute(name="id")
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    @XmlElement(name="images")
    public Image[] getImages() {
    return images;
    }

    public void setImages(Image[] images) {
    this.images = images;
    }
}

图片类:

public class Image {
   private int id;
   @XmlElementWrapper(name="Image")
   @XmlAttribute(name="id")
     public int getId() {
    return id;
    }
     public void setId(int id) {
    this.id = id;
    }
}

2 个答案:

答案 0 :(得分:0)

@XmlElementWrapper用于向集合添加分组元素。在您正在进行的非集合属性上使用它是无效的。如果你删除它应该工作。

如果您仍然无法解散:

  1. 填充模型并查看XML的外观,查看它与输入XML的不同之处并相应地更新映射。
  2. 更新您的问题以包含您获得的任何例外情况。

答案 1 :(得分:0)

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<Project name="">
    <Layer id="1">
        <Image id="1"></Image>
        <Image id="2"></Image>
    </Layer>
    <Layer id="2">
        <Image id="1"></Image>
        <Image id="2"></Image>
    </Layer>
</Project>

项目类:

@XmlRootElement(name="Project")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Project {

  private String name;
  private List<Layer> layers;

  @XmlAttribute(name ="name")
  public String getName() {
    return name;
  }

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

  public List<Layer> getLayers() {
    return layers;
  }

  @XmlElement(name = "Layer")
  public void setLayers(List<Layer> layers) {
    this.layers = layers;
  }

}

图层类:

@XmlAccessorType(XmlAccessType.PROPERTY)
public class Layer {

  private int id;
  private List<Image> images;

  @XmlAttribute(name="id")
  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  @XmlElement(name="Image")
  public List<Image> getImages() {
    return images;
  }

  public void setImages(List<Image> images) {
    this.images = images;
  }
}

图片类:

@XmlAccessorType(XmlAccessType.PROPERTY)
public class Image {

  private int id;

  @XmlAttribute(name="id")
  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }
}

测试类:

public class Main {

  public static void main(String[] args)
  {
    Project project = unmarshal();
    marshal(project);
  }

  public static void marshal(Project pr)
  {

    Project project;
    if(pr==null)
    {
      project = new Project();

      Image image1 = new Image();
      image1.setId(1);

      Image image2 = new Image();
      image2.setId(2);

      List<Image> images=new ArrayList<>();
      images.add(image1);
      images.add(image2);

      Layer layer1 = new Layer();
      layer1.setId(15);
      layer1.setImages(images);

      Layer layer2 = new Layer();
      layer2.setId(15);
      layer2.setImages(images);

      List<Layer> layers = new ArrayList<>();
      layers.add(layer1);
      layers.add(layer2);

      project.setName("peoject123");
      project.setLayers(layers);
    }
    else
    {
      project = pr;
    }

    try {

      File file = new File("src/project-out.xml");
      JAXBContext jaxbContext = JAXBContext.newInstance(Project.class);
      Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

      // output pretty printed
      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

      jaxbMarshaller.marshal(project, file);
      jaxbMarshaller.marshal(project, System.out);

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

  public static Project unmarshal() {
    Project project=null;
    try {
      File file = new File("src/project.xml");
      JAXBContext jaxbContext = JAXBContext.newInstance(Project.class);

      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
      project = (Project) jaxbUnmarshaller.unmarshal(file);

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