0

我正在尝试为XMLAdapter我的一个对象创建一个类。我需要Getters从另一个类访问,以便可以从该类的 Getters 填充一些对象,但我无法这样做。

Child基本上,我想GetterXMLAdapter. 我可以创建一个Child类的对象并访问,但它会导致NULL. 最初,我ObjectsChildandParent类中填充。我想在其中访问相同的值,XML Adapter所以我想知道是否有办法将类的引用传递给我的XMLAdapter.

我有以下Parent将具有值的类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
@XmlSeeAlso({Child.class})
public class Parent{
    private String eventID;
}

以下是我的Child课程,将在编组期间用于创建XML

@XmlRootElement(name = "child")
@XmlType(name = "child", propOrder = { "name","extension"})
public class Child extends Parent
{
    
    @XmlElement (name = "name") 
    private String name;
    
    @JsonIgnore
    @XmlJavaTypeAdapter (ExtensionAdapter.class)
    @XmlElement (name = "extension") 
    private Extension extension;

}

以下是我的扩展类:

@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Extension {
    private String eventID;
}

以下是我的XMLAdapter课:

public class ExtensionAdapter extends XmlAdapter<Child, Extension> {

    @Override
    public Extension unmarshal(Child valueType) throws Exception {
        System.out.println("UN-MARSHALLING");
        return null;
    }

    @Override
    public Child marshal(Extension boundType) throws Exception {
        System.out.println("MARSHALLING");
        System.out.println(boundType);
        //If I create the New child type then I am unable to access the eventID from its getter as Child class is nulll
        Child be = new Child();
        be.setEventID(boundType.getEventID());
        return be;
    }
}

基本上,我想访问其中的ChildglassGetter方法,XMLAdapter以便我可以用它来填充我的ExtensionEventID。我尝试了很多东西,但没有任何效果,所以我在这里发帖。

有没有办法可以将我的Child类作为参数传递给它,XMLAdapter以便我可以从中访问Getter方法?因为如果我创建一个新的Child类对象,那么它的所有Getter方法都是空的。但是,一开始我要填充ParentChild类(名称和事件ID)中的所有字段。我只想Extension使用eventID.XMLAdapter

我希望我能够解释需要。如果您有任何建议或示例代码,那么它将非常有用。

请注意:我在这里使用了非常示例的代码。我知道无论我想要实现什么,都可以使用正确的 POJO 和 JAXB 注释来实现,但我的实际类很复杂,我不允许修改它。我只能添加新的注释。因此,我正在尝试使用XMLAdapter.

4

1 回答 1

0

我想实现这样的目标,所以我能够做到这一点而无需访问课程。发布答案,以便将来对某人有用。

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.lang3.NotImplementedException;

import io.openepcis.model.jaxb.MyAdapter.MapWrapper;

public class MyAdapter extends XmlAdapter<MapWrapper, Map<String, Object>> {

    private DocumentBuilder documentBuilder;

    public MyAdapter() throws Exception {
        documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    }

    @Override
    public Map<String, Object> unmarshal(MapWrapper valurType) throws Exception {
        throw new NotImplementedException();
    }

    @SuppressWarnings("unchecked")
    @Override
    public MapWrapper marshal(Map<String, Object> m) throws Exception {
        if (m == null) {
            return null;
        }
        MapWrapper wrapper = new MapWrapper();
        List elements = new ArrayList();
        for (Map.Entry<String, Object> property : m.entrySet()) {
            if (property.getValue() instanceof Map) {
                elements.add(new JAXBElement<MapWrapper>(new QName(getCleanLabel(property.getKey())), MapWrapper.class, marshal((Map) property
                                    .getValue())));

            } else {
                elements.add(new JAXBElement<String>(new QName(getCleanLabel(property.getKey())), String.class, property.getValue().toString()));
            }

        }
        wrapper.elements = elements;
        return wrapper;

    }

    // Return a XML-safe attribute. Might want to add camel case support
    private String getCleanLabel(String attributeLabel) {
        attributeLabel = attributeLabel.replaceAll("[()]", "").replaceAll("[^\\w\\s]", "_").replaceAll(" ", "_");
        return attributeLabel;
    }

    public static class MapWrapper {
        @XmlAnyElement
        List elements;
    }
于 2021-05-09T11:35:17.423 回答