如果您使用的unmarshal
方法,需要一個Class
參數,您將收到的JAXBElement
的實例之一。
演示
package forum13709611;
import java.io.StringReader;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Book.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
String code = "<book><title>Harry Potter</title></book>";
StreamSource source = new StreamSource(new StringReader(code));
JAXBElement<Book> jaxbElement = unmarshaller.unmarshal(source, Book.class);
}
}
書
package forum13709611;
public class Book {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}