web-dev-qa-db-fra.com

convertir xml en objet Java utilisant jaxb (unmarshal))

J'ai le XML suivant et je dois le convertir en un objet Java.

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

Je ne suis pas sûr de ce que sera mon Pjo lorsque j'utiliserai JAXB pour le convertir.

J'ai créé les POJO suivants selon ma compréhension:

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;
    }
}

Aidez-moi, s'il vous plaît. Merci d'avance.

36
user1484781

Tests

Sur la classe Tests, nous ajouterons une annotation @XmlRootElement. Cela permettra à votre implémentation JAXB de savoir que lorsqu'un document commence par cet élément, il doit instancier cette classe. JAXB étant une configuration par exception, cela signifie que vous devez uniquement ajouter des annotations lorsque votre mappage diffère de celui par défaut. Puisque la propriété testData diffère du mappage par défaut, nous utiliserons l'annotation @XmlElement. Vous pouvez trouver le tutoriel suivant 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

Sur cette classe, j'ai utilisé l'annotation @XmlType Pour spécifier l'ordre dans lequel les éléments doivent être classés. J'ai ajouté une propriété testData qui semblait manquante. J'ai également utilisé une annotation @XmlElement Pour la même raison que dans la 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;
    }
}

Démo

Vous trouverez ci-dessous un exemple d'utilisation des API JAXB pour lire (désarmer) le code XML et renseigner votre modèle de domaine, puis réécrire (marshaler) le résultat en 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);
    }

}
128
bdoughan