简体   繁体   English

如果元素相同但元素值不同,则用jaxb解组xml

[英]Unmarshalling xml with jaxb if the elements are the same, but different element values

Hello help please have the xml file in it some of the same elements, the elements differ in the content of the "name" attribute. 您好帮助,请在其中包含xml文件中的某些相同元素,这些元素的区别在于“名称”属性的内容。 These elements have different sets of items, how to perform unmarshal? 这些元素具有不同的项目集,如何执行元帅? Xml: Xml:

        <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
        <service-broker>
            <adapters>
                <adapter name=\"cdb\">
                        <protocol>JDBC</protocol>
                        <datasource>CdbAdapterDS</datasource>
                </adapter>

                <adapter name=\"fmss-sc\">
                    <protocol>BBBC</protocol>
                    <fleet-uc18-reason-codes>
                            <code>2</code>
                            <code>3</code>
                    </fleet-uc18-reason-codes>
                </adapter>
            </adapters>  
        </service-broker>

I Need to perform the unmarshalling of the object 2 in the Cdb and FmssSc My attempts: General element - ServiceBroker.class 我需要在Cdb和FmssSc中执行对象2的解组。我的尝试:常规元素-ServiceBroker.class

package jax;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "service-broker")
@XmlAccessorType(XmlAccessType.FIELD)
public class ServiceBroker {

    @XmlElement(name = "adapters")
    private Adapters adapters;

    public Adapters getAdapters() {
        return adapters;
    }

    public void setAdapters(Adapters adapters) {
        this.adapters = adapters;
    }
}

Adapters.class Adapters.class

package jax;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;

@XmlAccessorType(XmlAccessType.FIELD)
class Adapters {

    @XmlElement
    private Cdb cdb;

    @XmlElement
    private FmssSc fmssSc;

    public Cdb getCdb() {
        return cdb;
    }

    public void setCdb(Cdb cdb) {
        this.cdb = cdb;
    }

    public FmssSc getFmssSc() {
        return fmssSc;
    }

    public void setFmssSc(FmssSc fmssSc) {
        this.fmssSc = fmssSc;
    }
}

Cdb.class Cdb.class

package jax;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.FIELD)
class Cdb {

    @XmlElement(name = "protocol")
    private String protocol;

    @XmlElement(name = "datasource")
    private String datasource;

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getDatasource() {
        return datasource;
    }

    public void setDatasource(String datasource) {
        this.datasource = datasource;
    }
}

FmssSc.class FmssSc.class

package jax;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;

@XmlAccessorType(XmlAccessType.FIELD)
class FmssSc {

    @XmlElement(name = "protocol")
    private String protocol;

    @XmlElementWrapper(name = "fleet-uc18-reason-codes")
    @XmlElement(name = "code")
    private List<String> code = new ArrayList<>();

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public List<String> getCode() {
        return code;
    }

    public void setCode(List<String> code) {
        this.code = code;
    }
}

Demo: 演示:

package jax;

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

public class Jax {

    public static final String adapter =
            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
            "<service-broker>" + 
                "<adapters>" +
                    "<adapter name=\"cdb\">" +
                            "<protocol>JDBC</protocol>" +
                            "<datasource>CdbAdapterDS</datasource>" +
                    "</adapter>" +

                    "<adapter name=\"fmss-sc\">" +
                        "<protocol>BBBC</protocol>" +
                        "<fleet-uc18-reason-codes>" +
                                "<code>2</code>" +
                                "<code>3</code>" +
                        "</fleet-uc18-reason-codes>" +
                    "</adapter>" +
                "</adapters>"  +
            "</service-broker>";

    public static void main(String[] args) throws JAXBException {
        Jax jax = new Jax();
        jax.shouldUnmarshallAdapter();
    }

    public void shouldUnmarshallAdapter() throws JAXBException {
        StringReader reader = new StringReader(adapter);
        JAXBContext context = JAXBContext.newInstance(ServiceBroker.class);
        Unmarshaller u = context.createUnmarshaller();
        ServiceBroker sb = (ServiceBroker) u.unmarshal(reader);
        System.out.println(sb.getAdapters().getCdb().getProtocol());
    }
}

Error: 错误:

Exception in thread "main" java.lang.NullPointerException
    at jax.Jax.shouldUnmarshallAdapter(Jax.java:39)
    at jax.Jax.main(Jax.java:31)

The tagname "adapter" in the XML has no equivalent in your Java classes, ie there is no member of the class "Adapters" with that name, so in fact all XML with that tag will be ignored, hence the variable cdb in the Adapters instance is null, therefore .. NPE. XML中的标记名“ adapter”在您的Java类中没有等效项,即,没有具有该名称的类“ Adapters”的成员,因此实际上所有带有该标记的XML都将被忽略,因此Adapters中的变量cdb instance为null,因此.. NPE。 And: The attribute "name" in your XML will by no way be used by Jaxb to map all sub-tags either to the cdb or fmssSc member. 而且:Jaxb绝不会使用XML中的属性“名称”将所有子标签映射到cdb或fmssSc成员。 Your Java code would map to an XML of that structure: 您的Java代码将映射到该结构的XML:

<service-Broker>
   <adapters>
      <cdb>
        <protocol>
        <datasource>
      <fmssSc>
        <protocol>
        ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM