8

私が設計したいくつかのクラスをマーシャリングしようとしています。標準のJAXBを使用すると、クラスはすべてvoidコンストラクターを持ちます。これは、JAXBまたはマーシャリング/アンマーシャリングを任意の言語で使用する最初の試みですが、私が理解しているように、JAXBはマーシャリングできるはずです。 XSDなしでそれら。

クラスは次のとおりです。

@XmlRootElement(name="place")
class Place {
    @XmlAttribute
    //various fields and get set methods
    public Place() {          
    }
}

@XmlRootElement(name="Arc")
class Arc {
    // various fields and get set methods
    @XmlAttribute
    Place p;
    public setPlace(Place p) {
        // ...
    }

    public Arc() { 
    }
}

@XmlRootElement(name="Transition")
class Transition {
   Arc[] a;

   public Transition() {        
   }
}

クラスをマーシャリングすることはできますが、Placeクラスをマーシャリングすることはできません。試していません。クラスにはArcタグがありますが、ネストされたクラスに到達すると、JAXBはどのXMLオブジェクトをマップするかを理解していないようです。Transition@XMLProprietyPlace

ネストされたクラスに使用する必要のある別のタグがある場合、または見落としている別のエラーがある場合はどうなりますか?

4

2 に答える 2

8

JAXB(JSR-222)実装でネストされたクラスを処理するために特別なことは何もする必要はありません。以下は、1つの@XmlRootElement注釈のみが使用される完全な例です。

遷移

package forum13159089;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
class Transition {

    Arc[] a;

    public Arc[] getA() {
        return a;
    }

    public void setA(Arc[] a) {
        this.a = a;
    }

}

アーク

package forum13159089;

class Arc {

    Place p;

    public Place getPlace() {
        return p;
    }

    public void setPlace(Place p) {
        this.p = p;
    }

}

場所

package forum13159089;

class Place {

}

デモ

package forum13159089;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Transition.class);
        
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum13159089/input.xml");
        Transition transition = (Transition) unmarshaller.unmarshal(xml);
        
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(transition, System.out);
    }

}

input.xml / Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<transition>
    <a>
        <place/>
    </a>
    <a>
        <place/>
    </a>
</transition>

詳細については


注: @XMLPropertyこれはJAXBアノテーションではありません。

于 2012-10-31T15:19:53.793 に答える
5

このコードは私のために働いています。チェックしてください。

 @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class First {

        @XmlElement
        private String name;
        @XmlElement
        private String surname;
        @XmlElement
        private String address;
            getters and setters
    }


    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Second {

        @XmlElement
        private String address1;
        @XmlElement
        private String address2;
        @XmlElement
        private String address3;
        @XmlElement
        private First first;
            getters and setters
    }

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Forth {

    @XmlElement
    private String address6;
    @XmlElement
    private String address7;
    @XmlElement
    private Second[] array = new Second[2];
        getters and setters
}

    public static void main(String arsg[]) throws Exception 
        {
            First first =  new First();
        first.setName("Kshitij");
        first.setSurname("Solanki");
        first.setAddress("Nadiad");

        Second second = new Second();
        second.setAddress1("Kshiutij_1");
        second.setAddress2("Kshiutij_2");
        second.setAddress3("Kshiutij_3");
        second.setFirst(first);

        Second second1 = new Second();
        second1.setAddress1("Kshiutij_1");
        second1.setAddress2("Kshiutij_2");
        second1.setAddress3("Kshiutij_3");
        second1.setFirst(first);

        Second[] arra = {second, second1};

        Forth forth = new Forth();
        forth.setAddress6("kjhgaksjfsadf");
        forth.setAddress7("sdlkfsdf");
        forth.setArray(arra);

        JAXBContext context = JAXBContext.newInstance(Forth.class);
        Marshaller marshaller = context.createMarshaller();
        StringWriter stringWriter = new StringWriter();
        marshaller.marshal(forth, stringWriter);
        System.out.println(stringWriter.toString());
        }

私があなたの問題を理解していなかったらごめんなさい。

于 2012-10-31T13:49:41.767 に答える