使用JAXB编组ObservableList [英] Marshalling ObservableList with JAXB

查看:101
本文介绍了使用JAXB编组ObservableList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JAXB对一个对象main进行编组,这是根类的属性:

I want to marshall an object "main" with JAXB, this are the attributes of the root 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,这也是输出:

But for some reason (I don´t know why) JAXB only takes the attributes database and mensaje,I need to save the observableList too this is the output:

<?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>

那么,我哪里错了?我该怎么办?

So, where I'm wrong? what should I do?

我编辑了项目并添加了适用于Observable List的适配器:

I edited the project and added adapters for Observable List putting:

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文件:

now in the XML file appears:

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

但不会封送它们的内容,我该怎么办?

but doesnt marshal the content of them, what should I do?

我声明了这样的JAXB上下文:

I declared the JAXB Context like this:

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


推荐答案

以下是使用JAXB编组ObservableList的示例:

Here's an example about marshalling an ObservableList using JAXB:

MyObject.java

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

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

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

我不会解决您的确切问题,因为您提供的信息不完整,您也无需提供英文代码。下次需要帮助时,您应该考虑这一点。

这篇关于使用JAXB编组ObservableList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆