使用 JAXB 编组 ObservableList

Marshalling ObservableList with JAXB

我想用 JAXB 编组一个对象 "main",这是根 class 的属性:

    private StringProperty mensaje;
    private bd database;
    private ObservableList<MarcoOntologicoColectivo> Inteligencia_colectiva=FXCollections.observableArrayList();
    private ObservableList<agent> agentData = FXCollections.observableArrayList();
    private ObservableList<MarcoOntologicoColectivo> Colectivo=FXCollections.observableArrayList();
    private ObservableList<MarcoOntologicoColectivo> Belongs=FXCollections.observableArrayList();

但出于某种原因(我不知道为什么)JAXB 只采用属性数据库和 mensaje,我也需要保存 observableList 这是输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<main>
    <database>
        <a_mecanismo>Hebbiano</a_mecanismo>
        <a_tecnicas>Redes Neuronales</a_tecnicas>
        <a_tecnicas>Arboles de Decision</a_tecnicas>
        <a_tecnicas>Reglas</a_tecnicas>            <a_tipo>Supervisado</a_tipo>
        <a_tipo>No supervisado</a_tipo>
        <a_tipo>Reforzamiento</a_tipo>
        <actos_habla>Requerimiento de Procesamiento</actos_habla>
        <caracterizacion>Concepto</caracterizacion>
        <caracterizacion>Propiedad</caracterizacion>
        <r_estrategia>Deductivo</r_estrategia>
        <r_estrategia>Inductivo</r_estrategia>
        <r_estrategia>Abductivo</r_estrategia>
        <r_lenguaje>OWL</r_lenguaje>
        <r_lenguaje>RDF</r_lenguaje>
        <servicio>Interno</servicio>
        <servicio>Externo</servicio>
        <servicio>Dual</servicio>
        <tipo_datos>byte</tipo_datos>
        <tipo_datos>short</tipo_datos>
        <tipo_datos>int</tipo_datos>
    </database>
    <mensaje/>
</main>

所以,我哪里错了?我该怎么办?

我编辑了项目并为可观察列表添加了适配器:

public class ObservableListAdapter<T> extends XmlAdapter<LinkedList<T>, ObservableList<T>> {
        @Override
        public ObservableList<T> unmarshal(LinkedList<T> v) throws Exception {
            return FXCollections.observableList(v);
        }

        @Override
        public LinkedList<T> marshal(ObservableList<T> v) throws Exception {
            LinkedList<T> list = new LinkedList<T>();
            list.addAll(v);
            return list; // Or whatever the correct method is
        }
}

现在在XML文件中出现:

<belongs/>
 <colectivo/>
 <inteligencia_colectiva/>

但是没有编组它们的内容,我该怎么办?

我这样声明 JAXB 上下文:

File file = new File("file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(MyClass.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

下面是一个使用 JAXB 编组 ObservableList 的示例:

MyObject.java

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "Object")
public class MyObject {

    private String value;

    @XmlAttribute (name = "value", required = false)
    public String getvalue() {
        return value;
    }

    public void setvalue(String value) {
        this.value = value;
    }


    public String toString() {
        return "value=" + value;
    }
}

MyContainer.java

import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name = "Container")
public class MyContainer extends MyObject {

    private ObservableList<MyObject> children = FXCollections.observableArrayList();

    @XmlElements({ @XmlElement(name = "Object", type = MyObject.class) })
    public List<MyObject> getChildren() {
        return children;
    }


    public String toString() {

        StringBuilder sb = new StringBuilder();
        sb.append("children:");

        for (MyObject node : children) {
            sb.append("\n");
            sb.append("  " + node.toString());
        }
        return sb.toString();

    }
}

Example.java

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

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

        // create container with list
        MyContainer container = new MyContainer();

        // add objects
        MyObject object;

        object = new MyObject();
        object.setvalue("A");
        container.getChildren().add( object);

        object = new MyObject();
        object.setvalue("B");
        container.getChildren().add( object);

        // marshal
        String baseXml = marshal( container);

        // unmarshal
        container = unmarshal(baseXml);

        System.out.println("Container:\n" + container);


        System.exit(0);
    }

    public static String marshal( MyContainer base) {

        try {

            JAXBContext jaxbContext = JAXBContext.newInstance(MyContainer.class);

            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            StringWriter stringWriter = new StringWriter();
            jaxbMarshaller.marshal(base, stringWriter);
            String xml = stringWriter.toString();

            System.out.println("XML:\n" + xml);

            return xml;

        } catch (Exception e) {
            throw new RuntimeException( e);
        }

    }

    public static MyContainer unmarshal( String xml) {

        try {

            JAXBContext jaxbContext = JAXBContext.newInstance(MyContainer.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            StringReader stringReader = new StringReader(xml);

            MyContainer container = (MyContainer) jaxbUnmarshaller.unmarshal(stringReader);

            return container;

        } catch (Exception e) {
            throw new RuntimeException( e);
        }

    }
}

控制台输出:

XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Container>
    <Object value="A"/>
    <Object value="B"/>
</Container>

Container:
children:
  value=A
  value=B

我不会深入探讨你的具体问题,因为你提供的信息不完整,而且你懒得提供英文代码。下次你需要帮助时,你应该考虑这个。