亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用java注釋讀取xsi:type

如何使用java注釋讀取xsi:type

BIG陽 2023-09-20 15:33:14
我想將基于 jaxb 的 xml 文件讀入我的面向對象結構??梢哉f這是我的 xml 文件:    <?xml version="1.0" encoding="utf-8" standalone="yes"?>    <children xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">        <child xsi:type="girl">            <age>12</age>            <isdancing>true</isdancing>        </child>        <child xsi:type="boy">            <age>10</age>            <issoccerplayer>true</issoccerplayer>        </child>    </children>Children是某種包含多個子元素的包裝元素。孩子可以是 xsi : type 指定的男孩或女孩。這兩個類有一些共同的元素(如age)和一些不同(排除)的元素(如isdancing或issoccerplayer)要讀取文件,我有這個方法:    public static void main( String[] args ) throws JAXBException    {        JAXBContext jaxbContext;        jaxbContext = JAXBContext.newInstance(Children.class);                     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();        File file = new File("C:/test.xml");        if (!file.exists()) System.out.println("File does not exist");        Children children = (Children) jaxbUnmarshaller.unmarshal(file);        System.out.println(children.toString());    }我的孩子們的班級是這樣的:    @XmlRootElement(name="children")    @XmlAccessorType(XmlAccessType.FIELD)    public class Children {        @XmlElement(name="child")        private List<Child> childrenList;        public List<Child> getChildren() { return childrenList; }        public void setChildren(List<Child> children) {this.childrenList = children;}    @Override        public String toString() {            return ReflectionToStringBuilder.toString(this);        }    }我的孩子班看起來像這樣:    @XmlAccessorType(XmlAccessType.FIELD)    public class Child {    @XmlAttribute(name="xsi:type")    private XsiType xsiType;    private int age;    @XmlElement(name = "isdancing")    private boolean isDancing;        }    }我現在的問題是,輸出正常,但 Child-class 的元素 xsiType 始終為 null,否則最終會出現 IllegalAnnotationExceptions,這與 XmlTest.model.Child.xsiType 相關所以我預計設置任何類型的 @Xml-Annotation 都會出現錯誤。有人可以幫我找出錯誤嗎?目標是迭代孩子列表并在運行時(基于 xsiType)決定這是女孩還是男孩。
查看完整描述

3 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

你不需要你的XsiType課。您可以直接使用String

在你的Child類中xsiType,屬性應該如下所示。

@XmlAttribute(name?=?"type",?namespace?=?"?
private?String?xsiType;

注意:在@XmlAttribute注釋中

  • 使用name = "type"(不帶前綴xsi:

  • 指定namespaceXML 中給定的參數xmlns:xsi="..."


順便說一句:?您最好使用常量,
而不是鍵入字符串。所以你的改進代碼會是這樣的:"http://www.w3.org/2001/XMLSchema-instance"XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI

@XmlAttribute(name?=?"type",?namespace?=?XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)
private?String?xsiType;


查看完整回答
反對 回復 2023-09-20
?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

xsi 類型通常用于表達對具體類型的引用。Jaxb 可以使用 xsi 類型,無需進一步的解決方法。


創建一個Boy和 一個Girl擴展的類Children。(您可能需要使用 調整類型名稱@XmlType)。這樣,所有具有的元素都xsi:type=Girl將綁定到該類Girl


@XmlAccessorType(XmlAccessType.FIELD)

@XmlSeeAlso({ Boy.class, Girl.class }) // Either use @XmlSeeAlso to register classes in the JaxbContext

                                       //  or add them to the context directly

public class Child {


    private int age;


    @XmlElement(name = "isdancing")

    private boolean isDancing;


    @XmlElement(name = "issoccerplayer")

    private boolean isSoccerPlayer;


    // Getter and setter for all fields


}


@XmlType(name = "boy") // can be omitted if default value matches with the default value

public class Boy extends Child {


}


@XmlType(name = "girl")

public class Girl extends Child {


}

完整的獨立示例:


package jaxb;


import java.io.File;

import java.io.StringReader;

import java.util.List;


import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlSeeAlso;

import javax.xml.bind.annotation.XmlType;


public class Inheritance {


    public static void main(String[] args) throws JAXBException {

        JAXBContext jaxbContext;

        jaxbContext = JAXBContext.newInstance(Children.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();


        String x = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n"

                + "    <children xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n"

                + "        <child xsi:type=\"girl\">\r\n" + "            <age>12</age>\r\n"

                + "            <isdancing>true</isdancing>\r\n" + "        </child>\r\n"

                + "        <child xsi:type=\"boy\">\r\n" + "            <age>10</age>\r\n"

                + "            <issoccerplayer>true</issoccerplayer>\r\n" + "        </child>\r\n" + "    </children>";


        Children children = (Children) jaxbUnmarshaller.unmarshal(new StringReader(x));

        System.out.println(children.getChildren().toString());

    }


    @XmlRootElement(name = "children")

    @XmlAccessorType(XmlAccessType.FIELD)

    public static class Children {


        @XmlElement(name = "child")

        private List<Child> childrenList;


        public List<Child> getChildren() {

            return childrenList;

        }


        public void setChildren(List<Child> children) {

            this.childrenList = children;

        }


    }


    @XmlAccessorType(XmlAccessType.FIELD)

    @XmlSeeAlso({ Boy.class, Girl.class })

    public static class Child {


        private int age;


        @XmlElement(name = "isdancing")

        private boolean isDancing;


        @XmlElement(name = "issoccerplayer")

        private boolean isSoccerPlayer;


        // Getter and setter for all fields


    }


    @XmlType(name = "boy")

    public static class Boy extends Child {


    }


    @XmlType(name = "girl")

    public static class Girl extends Child {


    }

}



查看完整回答
反對 回復 2023-09-20
?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

第二種方法的干凈解決方案(基于單獨的類文件):


public class App

{

    public static void main(String[] args) throws JAXBException

    {

        JAXBContext jaxbContext = JAXBContext.newInstance(Children.class);             

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        File file = new File("C:/test2.xml");

        Children children = (Children) jaxbUnmarshaller.unmarshal(file);


        for (Child c : children.getChildren()) {

            if (c instanceof Boy) {

                System.out.println(((Boy)c).toString());

            } else if (c instanceof Girl){

                System.out.println(((Girl)c).toString());

            }

        }

    }

}

Children.java


@XmlRootElement(name="children")

@XmlAccessorType(XmlAccessType.FIELD)

public class Children {


    @XmlElement(name="child")

    private List<Child> childrenList;


    public List<Child> getChildren() { return childrenList; }

    public void setChildren(List<Child> children) {this.childrenList = children;}


    @Override

    public String toString() { return ReflectionToStringBuilder.toString(this); }

}

Boy.java


@XmlType(name="boy")

public class Boy extends Child {


    @XmlElement(name = "issoccerplayer")

    private boolean isSoccerPlayer;


    public boolean isSoccerPlayer() { return isSoccerPlayer; }

    public void setSoccerPlayer(boolean isSoccerPlayer) { this.isSoccerPlayer = isSoccerPlayer; }


    @Override

    public String toString() { return ReflectionToStringBuilder.toString(this); }

}

Girl.java


@XmlType(name="girl")

public class Girl extends Child {


    @XmlElement(name = "isdancing")

    private boolean isDancing;


    public boolean isDancing() { return isDancing; }

    public void setDancing(boolean isDancing) { this.isDancing = isDancing; }


    @Override

    public String toString() { return ReflectionToStringBuilder.toString(this); }

}

Child.java


@XmlAccessorType(XmlAccessType.FIELD)

@XmlSeeAlso({ Boy.class, Girl.class }) 

public abstract class Child {


    private int age;


    public int getAge() { return age; }

    public void setAge(int age) { this.age = age; }

}

輸出應該是:


de.home.myproject.XmlTest.model.Girl@12edcd21[isDancing=true,age=12]

de.home.myproject.XmlTest.model.Boy@27bc2616[isSoccerPlayer=true,age=10]


查看完整回答
反對 回復 2023-09-20
  • 3 回答
  • 0 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號