2015-10-17 77 views
1

我想馬歇爾對象「主」與JAXB,這是根類的屬性:編組站ObservableList與JAXB

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(); 
+0

是否JAXB編組標MarcoOntologicoColectivo和標代理?另外,你是否嘗試將列表聲明爲'List <...>'? – laune

+0

嗨,不,我不想要列表我需要observableList那是我不想改變的東西。我用適配器ObservableList,他們JAXB馬歇爾'ObservableList '正確,但沒有'ObservableList '爲examplo –

+0

是的,我知道你需要一個ObservableList,他們卻看它是否有一個列表的作品,我們會把它從那裏。 – laune

回答

2

下面是關於使用編組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 

我不會進入您的具體問題,因爲您提供的信息是不完整的,你沒有理會提供英文代碼。下一次你需要幫助時你應該考慮這個。