JAXB XSD元素(基本)

JAXB XSD元素(基本),xsd,jaxb,uri,Xsd,Jaxb,Uri,我想将传入的XML绑定到JavaType <?xml version="1.0" encoding="UTF-8"?> <apiKeys uri="http://api-keys"> <apiKey uri="http://api-keys/1933"/> <apiKey uri="http://api-keys/19334"/> </apiKeys> 我希望使用JAXB,因此我定义了一个XSD。我的XSD不正确,创建的对象在创建

我想将传入的XML绑定到JavaType

<?xml version="1.0" encoding="UTF-8"?>
<apiKeys uri="http://api-keys">
<apiKey uri="http://api-keys/1933"/>
<apiKey uri="http://api-keys/19334"/>
</apiKeys>

我希望使用JAXB,因此我定义了一个XSD。我的XSD不正确,创建的对象在创建时是空的

我的XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="apiKeys" type="ApiKeysResponseInfo2" />

<xsd:complexType name="ApiKeysResponseInfo2">
<xsd:sequence>
<xsd:element name="uri" type="xsd:anyURI" minOccurs="1" maxOccurs="1">
</xsd:element>
<xsd:element name="apiKey" type="xsd:anyURI" minOccurs="1" maxOccurs="unbounded">
</xsd:element>
</xsd:sequence>
</xsd:complexType>  
</xsd:schema>

显然,我的元素定义是错误的。非常感谢您的帮助。谢谢

我希望使用JAXB,因此我定义了一个XSD

JAXB不需要XML模式。我们将其设计为从Java对象开始,添加了从XML模式生成带注释模型的功能,作为一种方便的机制


我想将传入的XML绑定到JavaType

<?xml version="1.0" encoding="UTF-8"?>
<apiKeys uri="http://api-keys">
<apiKey uri="http://api-keys/1933"/>
<apiKey uri="http://api-keys/19334"/>
</apiKeys>
您可以使用以下对象模型:

ApiKeys

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class ApiKeys {

    private String uri;
    private List<ApiKey> apiKey;

    @XmlAttribute
    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public List<ApiKey> getApiKey() {
        return apiKey;
    }

    public void setApiKey(List<ApiKey> apiKey) {
        this.apiKey = apiKey;
    }

}
演示

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum14643483/input.xml");
        ApiKeys apiKeys = (ApiKeys) unmarshaller.unmarshal(xml);

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

}
我希望使用JAXB,因此我定义了一个XSD

JAXB不需要XML模式。我们将其设计为从Java对象开始,添加了从XML模式生成带注释模型的功能,作为一种方便的机制


我想将传入的XML绑定到JavaType

<?xml version="1.0" encoding="UTF-8"?>
<apiKeys uri="http://api-keys">
<apiKey uri="http://api-keys/1933"/>
<apiKey uri="http://api-keys/19334"/>
</apiKeys>
您可以使用以下对象模型:

ApiKeys

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class ApiKeys {

    private String uri;
    private List<ApiKey> apiKey;

    @XmlAttribute
    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public List<ApiKey> getApiKey() {
        return apiKey;
    }

    public void setApiKey(List<ApiKey> apiKey) {
        this.apiKey = apiKey;
    }

}
演示

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum14643483/input.xml");
        ApiKeys apiKeys = (ApiKeys) unmarshaller.unmarshal(xml);

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

}

喔,谢谢你的布莱斯-我会试试的。我的动机实际上是使用Json,所以我实际上想要的只是Jaxb对象,并且不会维护XSD。我当前的Jaxb对象确实看起来像你的,但我会做一些事情并重新测试。我不理解的(翻译)部分与>-相关,但是如果XML中的@Damo-By
translation
部分是JSON吗?我更习惯于使用XML而不是JSON,我的计划是创建JAXB对象,并通过将XML绑定到它们来验证它们的正确性,然后将传入的数据表示形式交换为JSON(我正在编写一个rest客户端)我的翻译注释与的时髦语法有关;其中该元素有一个“apiKey”“part”和一个“uri”“part”-对于这些“part”的Jaxb字段是什么感到困惑映射到看起来像。也就是说,我现在有了一些工作。我调整了我的XSD,它创建了更像你的Jaxb对象。我想我有足够的信息来回顾和了解这一切是如何发生的,除非你能在我的翻译查询方面帮我更多。谢谢。吴,谢谢你的Blaise-我会尝试一下。我的动机是实际上,我需要使用Json,所以我实际上只需要Jaxb对象,并且不会维护XSD。我当前的Jaxb对象确实看起来像你的对象,但我会做一些事情并重新测试。翻译我不理解的部分与>-相关,但是如果XML中的@Damo-By
translation
部分指的是JSON吗?我更习惯于使用XML而不是JSON,我的计划是创建JAXB对象并通过将XML绑定到它们来验证它们的正确性,然后将传入的数据表示形式交换为JSON(我正在编写一个rest客户端)我的翻译注释与的时髦语法有关;其中该元素有一个“apiKey”“part”和一个“uri”“part”-对于这些“part”的Jaxb字段是什么感到困惑映射到看起来像。这就是说,我现在有了一些工作。我调整了我的XSD,它创建了更像你的Jaxb对象。我想我有足够的信息来回顾和弄清楚这一切是如何发生的,除非你能在我的翻译查询方面帮我更多。谢谢。