Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
JAXB无法从XML获取对象的名称_Jaxb - Fatal编程技术网

JAXB无法从XML获取对象的名称

JAXB无法从XML获取对象的名称,jaxb,Jaxb,当运行java文件将XML转换为java类对象时,它给出的是这个输出,而不是XML文件 import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class XMLToObject { public static void main(String[] args) {

当运行java文件将XML转换为java类对象时,它给出的是这个输出,而不是XML文件

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class XMLToObject {
    public static void main(String[] args) {

     try {

        File file = new File("file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
        System.out.println(customer);

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

    }
}
输出: Customer@2c2c14f9


请帮助我获取对象中的XML文件输出

您当前看到的是在Customer实例上调用toString的结果。您需要使用封送拆收器将对象输出到XML

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(customer, System.out);

嘿,我找到了解决办法。这是一个愚蠢的错误。我需要调用类的公共getter方法来访问真实数据。。。谢谢你的帮助

但是有XML文件,那么需要再次封送它吗?它已经被编组。我需要客户对象像它的字符串方法一样打印。。不是XML文件。。我有XML文件。。。我需要将其转换为对象,以便在java代码中使用。。谢谢你的回复,但我需要一些不同的东西。