2012-06-27 15 views
32

Ho il seguente XML e ho bisogno di convertirlo in un oggetto java.convertito da xml a java object usando jaxb (unmarshal)

<tests> 
    <test-data> 
     <title>BookTitle</title> 
     <book>BookName</book> 
     <count>64018</count> 
     <test-data> 
      <title>Book title1</title> 
      <book>Book Name1</book> 
      <count>5</count> 
     </test-data> 
     <test-data> 
      <title>Book title2</title> 
      <book>Book Name3</book> 
      <count>5</count> 
     </test-data> 
     <test-data> 
      <title>Book title3</title> 
      <book>Book Name3</book> 
      <count>4</count> 
     </test-data> 
    </test-data> 
</tests> 

Non sono sicuro di quale sarà il mio pojo quando uso JAXB per convertirlo.

ho creato il seguente POJO come per la mia comprensione:

public class Tests { 

    TestData testData; 

    public TestData getTestData() { 
     return testData; 
    } 

    public void setTestData(TestData testData) { 
     this.testData = testData; 
    } 
} 

public class TestData { 
    String title; 
    String book; 
    String count; 

    public String getTitle() { 
     return title; 
    } 
    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getBook() { 
     return book; 
    } 
    public void setBook(String book) { 
     this.book = book; 
    } 
    public String getCount() { 
     return count; 
    } 
    public void setCount(String count) { 
     this.count = count; 
    } 
} 

Ti prego, aiutami. Grazie in anticipo.

+2

Non sei sicuro, ok. Ma hai provato qualcosa? È più semplice aiutare se hai già fatto qualcosa. – buc

+2

Se si dispone di XSD per questo XML, è possibile generare classi annotate JAXB con IDE come Eclipse. –

risposta

116

Test

sulla classe Tests l'aggiungeremo un @XmlRootElement annotazione. Ciò consentirà alla tua implementazione JAXB di sapere che quando un documento inizia con questo elemento dovrebbe creare un'istanza di questa classe. JAXB è la configurazione per eccezioni, questo significa che devi solo aggiungere annotazioni in cui il tuo mapping è diverso da quello predefinito. Poiché la proprietà testData differisce dalla mappatura predefinita, useremo l'annotazione @XmlElement. Si possono trovare il seguente tutorial utile:. http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted

package forum11221136; 

import javax.xml.bind.annotation.*; 

@XmlRootElement 
public class Tests { 

    TestData testData; 

    @XmlElement(name="test-data") 
    public TestData getTestData() { 
     return testData; 
    } 

    public void setTestData(TestData testData) { 
     this.testData = testData; 
    } 

} 

TestData

In questa classe ho usato il @XmlType annotazioni per specificare l'ordine in cui gli elementi devono essere ordinati in Ho aggiunto una proprietà testData che sembrava mancare. Ho anche utilizzato un'annotazione @XmlElement per lo stesso motivo della classe Tests.

package forum11221136; 

import java.util.List; 
import javax.xml.bind.annotation.*; 

@XmlType(propOrder={"title", "book", "count", "testData"}) 
public class TestData { 
    String title; 
    String book; 
    String count; 
    List<TestData> testData; 

    public String getTitle() { 
     return title; 
    } 
    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getBook() { 
     return book; 
    } 
    public void setBook(String book) { 
     this.book = book; 
    } 
    public String getCount() { 
     return count; 
    } 
    public void setCount(String count) { 
     this.count = count; 
    } 
    @XmlElement(name="test-data") 
    public List<TestData> getTestData() { 
     return testData; 
    } 
    public void setTestData(List<TestData> testData) { 
     this.testData = testData; 
    } 
} 

Demo

Di seguito è riportato un esempio di come utilizzare le API JAXB per leggere (unmarshal) l'XML e popolare il modello di dominio e poi scrivere (maresciallo) il risultato di nuovo in XML.

package forum11221136; 

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Tests.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     File xml = new File("src/forum11221136/input.xml"); 
     Tests tests = (Tests) unmarshaller.unmarshal(xml); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(tests, System.out); 
    } 

} 
+24

Dovresti accettare questa risposta se la leggi. –

+0

Unmaralling XML nell'oggetto Java senza schema - utilizza Reflection? Se sì, qual è il ruolo svolto da Reflection in Unmarshalling? – Viswa

Problemi correlati