Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB映射到对象而不是字符串_Java_Xml_Jaxb - Fatal编程技术网

Java JAXB映射到对象而不是字符串

Java JAXB映射到对象而不是字符串,java,xml,jaxb,Java,Xml,Jaxb,我有以下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> <manufa

我有以下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>

这是一场淘汰赛(梅盖)(M1A/B)(第24集)
199?
梅盖
今天,我在游戏类中将制造商作为字符串,但我需要映射到制造商类,我应该怎么做?可能吗?谢谢


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

对XML结构进行了更改

XML

manufacturer.java

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多个条目)
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;
    }

    }  
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;
  }
}
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(); 
         }    
     }

}