Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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中将xml转换为对象?_Java_Xml_Jaxb - Fatal编程技术网

有没有办法在java中将xml转换为对象?

有没有办法在java中将xml转换为对象?,java,xml,jaxb,Java,Xml,Jaxb,我试图使用XML,以一种简单的方式访问所有字段和数据,因此,我决定使用JaxB,但我不知道如何为对象创建所有类,我像这样手动尝试 @XmlRootElement(name = "Response") public class Response { @XmlElement(ns = "SignatureValue") String signatureValue; } 但是我在@XmlElement上得到一个错误,说这个位置不允许注释 我查看了其他帖子,如果我有像Hellw这样

我试图使用XML,以一种简单的方式访问所有字段和数据,因此,我决定使用JaxB,但我不知道如何为对象创建所有类,我像这样手动尝试

@XmlRootElement(name = "Response")
public class Response {

    @XmlElement(ns = "SignatureValue")
    String signatureValue;

}
但是我在@XmlElement上得到一个错误,说这个位置不允许注释

我查看了其他帖子,如果我有像Hellw这样的东西,它们会很好用,但不适用于更复杂的格式,我的第一部分的示例如下

<?xml version="1.0" encoding="UTF-8"?><DTE xsi:noNamespaceSchemaLocation="http://www.myurl/.xsd" xmlns:gs1="urn:ean.ucc:pay:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

你知道怎么做吗

提前谢谢

编辑:


我忘了说,XML实际上是一个包含整个XML的字符串。

注释在字段上有效。如果您有相应的属性,则应使用
@xmlacessortype(xmlacesstype.FIELD)
注释该类,以避免重复的映射异常

Java模型 注释字段

@XmlRootElement(name = "Response")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {

    @XmlElement(name = "SignatureValue")
    String signatureValue;

    public String getSignatureValue() {
        return signatureValue;
    }

    public void setSignatureValue(String signatureValue) {
        this.signatureValue = signatureValue;
    }

}
注释属性

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "Response")
public class Response {

    String signatureValue;

    @XmlElement(name = "SignatureValue")
    public String getSignatureValue() {
        return signatureValue;
    }

    public void setSignatureValue(String signatureValue) {
        this.signatureValue = signatureValue;
    }

}
了解更多信息

演示代码 下面是一些演示代码,用于读取/写入与
响应
类对应的XML

演示

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Response.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum19713886/input.xml");
        Response response = (Response) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(response, System.out);
    }

}
input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <SignatureValue>Hello World</SignatureValue>
</Response>

你好,世界

您遇到了什么异常?没有异常,因为当我使用注释时,它说是不允许的,您在哪里看到的?JDOM可能更适合您尝试执行的操作。如果做不到这一点,请阅读一些关于如何实际使用JAXB的教程