3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
你不需要你的XsiType
課。您可以直接使用String
。
在你的Child
類中xsiType
,屬性應(yīng)該如下所示。
@XmlAttribute(name?=?"type",?namespace?=?"? private?String?xsiType;
注意:在@XmlAttribute
注釋中
使用
name = "type"
(不帶前綴xsi:
)指定
namespace
XML 中給定的參數(shù)xmlns:xsi="..."
順便說(shuō)一句:?您最好使用常量,
而不是鍵入字符串。所以你的改進(jìn)代碼會(huì)是這樣的:"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;

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
xsi 類型通常用于表達(dá)對(duì)具體類型的引用。Jaxb 可以使用 xsi 類型,無(wú)需進(jìn)一步的解決方法。
創(chuàng)建一個(gè)Boy和 一個(gè)Girl擴(kuò)展的類Children。(您可能需要使用 調(diào)整類型名稱@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 {
}
完整的獨(dú)立示例:
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 {
}
}

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
第二種方法的干凈解決方案(基于單獨(dú)的類文件):
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; }
}
輸出應(yīng)該是:
de.home.myproject.XmlTest.model.Girl@12edcd21[isDancing=true,age=12]
de.home.myproject.XmlTest.model.Boy@27bc2616[isSoccerPlayer=true,age=10]
添加回答
舉報(bào)