【问题标题】:JAXB map to object insteadof stringJAXB 映射到对象而不是字符串
【发布时间】:2015-06-28 19:27:44
【问题描述】:

我有以下 XML:

<game name="m1itskow" sourcefile="maygay1bsw.c" ismechanical="yes" cloneof="m1itsko" romof="m1itsko">
    <description>It's A Knockout (Maygay) (M1A/B) (set 24)</description>
    <year>199?</year>
    <manufacturer>Maygay</manufacturer>
</game>

今天我在游戏类中有制造商作为字符串,但我需要映射到制造商类,我应该怎么做?可能吗?谢谢。

编辑:无法更改 XML,因为这是由第三方工具生成的文件。

【问题讨论】:

  • 看看 JaxB 绑定

标签: java xml jaxb


【解决方案1】:

修改了xml结构

XML

<?xml version="1.0" encoding="UTF-8"?>
<game name="m1itskow" sourcefile="maygay1bsw.c" ismechanical="yes" cloneof="m1itsko" romof="m1itsko">
    <description>It's A Knockout (Maygay) (M1A/B) (set 24)</description>
    <year>199?</year>
    <manufacturer>
    <manufacturer-name>Maygay</manufacturer-name>
    </manufacturer>
</game>

POJO 游戏和制造商...

game.java

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
    public class game {
        private String name;
        private String sourcefile;
        private String ismechanical;
        private String cloneof;
        private String romof;
        private String description;
        private String year;
        private Manufacturer manufacturer=new Manufacturer();
        public game() {}
    @XmlAttribute  
    public String getIsmechanical() {
          return ismechanical;
        }
        public void setIsmechanical(String ismechanical) {
          this.ismechanical = ismechanical;
        }
        @XmlAttribute
        public String getCloneof() {
          return cloneof;
        }
        public void setCloneof(String cloneof) {
          this.cloneof = cloneof;
        }
        @XmlAttribute
        public String getRomof() {
          return romof;
        }
        public void setRomof(String romof) {
          this.romof = romof;
        }
    @XmlAttribute  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }
    @XmlAttribute
    public String getSourcefile() {
      return sourcefile;
    }
    public void setSourcefile(String sourcefile) {
      this.sourcefile = sourcefile;
    }
    @XmlElement
    public String getDescription() {
      return description;
    }
    public void setDescription(String description) {
      this.description = description;
    }
    @XmlElement
    public String getYear() {
      return year;
    }
    public void setYear(String year) {
      this.year = year;
    }
    @XmlElement(name="manufacturer")
    public Manufacturer getManufacturer() {
      return manufacturer;
    }
    public void setManufacturer(Manufacturer manufacturer) {
      this.manufacturer=manufacturer;
    }

    }  

manufacturer.java

我

mport java.io.Serializable;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 @XmlRootElement(name="manufacturer")    
public class Manufacturer implements Serializable {

  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  private String Name;
  @XmlElement(name="manufacturer-name")
  public String getName() {
    return Name;
  }
  public void setName(String name) {
    Name = name;
  }
}

main.java

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

public class SampleTest {  
public static void main(String[] args) {  
     try {    
            File file = new File("employee.xml");    
            JAXBContext jaxbContext = JAXBContext.newInstance(game.class);    
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();    
            game e=(game) jaxbUnmarshaller.unmarshal(file);    
            System.out.println(e.getManufacturer().getName());  

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

}

【讨论】:

  • 只是忘了提到我无法更改 XML。这是一个生成的文件,我无法手动更改它(它有超过 30000 个条目)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 2021-02-24
  • 2012-10-28
  • 2018-07-04
  • 2018-09-30
相关资源
最近更新 更多