我一直在寻找有关如何使用package-info.java文件中的注释@XMLSchema实现Unmarshalling的示例。
我的理解是该理论假设非常简单。但是,在查看以下链接后,我仍然不知道为什么注释不起作用。
以下是代码:
package-info.java:
@XmlSchema(
namespace="http://test.org/url1",
elementFormDefault = XmlNsForm.QUALIFIED,
attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED,
xmlns={
@XmlNs(prefix="ns6", namespaceURI="http://test.org/url6"),
@XmlNs(prefix="ns5", namespaceURI="http://test.org/url5"),
@XmlNs(prefix="ns7", namespaceURI="http://test.org/url7"),
@XmlNs(prefix="ns1", namespaceURI="http://test.org/url1"),
@XmlNs(prefix="ns3", namespaceURI="http://test.org/url3"),
@XmlNs(prefix="ns2", namespaceURI="http://test.org/url2"),
@XmlNs( prefix="ns4", namespaceURI="http://test.org/url4"),
}
)
package simpleJAXB;
import javax.xml.bind.annotation.*;
用作输入的XML:
<?xml version="1.0" encoding="UTF-8"?>
<ns6:RequestShort
xmlns:ns2="http://test.org/url2"
xmlns:ns1="http://test.org/url1"
xmlns:ns4="http://test.org/url4"
xmlns:ns3="http://test.org/url3"
xmlns:ns5="http://test.org/url5"
xmlns:ns6="http://test.org/url6"
xmlns:ns7="http://test.org/url7">
<ns3:IndividualRequestShort>
<ns2:Person>Name1</ns2:Person>
<ns2:Title>TEST</ns2:Title>
</ns3:IndividualRequestShort>
<ns3:IndividualRequestShort>
<ns2:Person>Name2</ns2:Person>
<ns2:Title>TEST2</ns2:Title>
</ns3:IndividualRequestShort>
</ns6:RequestShort>
以下是课程:
RequestShort:
package simpleJAXB;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement ( name = "RequestShort")
public class RequestShort {
ArrayList<IndividualRequestShort> individualRequestListShort;
public RequestShort(){}
@XmlElement(name = "IndividualRequestShort", type=IndividualRequestShort.class)
public ArrayList<IndividualRequestShort> getIndividualRequestListShort() {
return individualRequestListShort;
}
public void setIndividualRequestListShort(
ArrayList<IndividualRequestShort> individualRequestList) {
this.individualRequestListShort = individualRequestList;
}
}
IndividualRequestShort:
package simpleJAXB;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name= "IndividualRequestShort")
public class IndividualRequestShort {
String title;
String person;
public IndividualRequestShort(){}
@XmlElement(name = "Title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@XmlElement(name = "Person")
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
最后,主要的void方法是:
try{
File file = new File("XML_test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(RequestShort.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
RequestShort requestShort = (RequestShort) jaxbUnmarshaller.unmarshal(file);
ArrayList<IndividualRequestShort> contentRequests = new ArrayList<IndividualRequestShort>();
contentRequests = requestShort.getIndividualRequestListShort();
if(contentRequests != null){
Iterator<IndividualRequestShort> requestsIterator = contentRequests.iterator();
IndividualRequestShort temp = null;
while (requestsIterator.hasNext()) {
temp = (IndividualRequestShort)requestsIterator.next();
System.out.println("Data: " + temp.getTitle() + temp.getPerson());
}
} catch (Exception e) {
System.out.println(e.toString() + " " + e.getStackTrace().toString());
}
return null;
}
我已经能够使用namespace选项实际解析xmlelment和xmlrootelement上的xml。但是,当我尝试放入xmlschema注释时,如上所示,我收到以下错误:
Classes known to this context:
[B
boolean
byte
char
com.sun.xml.internal.bind.api.CompositeStructure
double
float
int
java.awt.Image
java.io.File
java.lang.Boolean
java.lang.Byte
java.lang.Character
java.lang.Class
java.lang.Double
java.lang.Float
java.lang.Integer
java.lang.Long
java.lang.Object
java.lang.Short
java.lang.String
java.lang.Void
java.math.BigDecimal
java.math.BigInteger
java.net.URI
java.net.URL
java.util.Calendar
java.util.Date
java.util.GregorianCalendar
java.util.UUID
javax.activation.DataHandler
javax.xml.bind.JAXBElement
javax.xml.datatype.Duration
javax.xml.datatype.XMLGregorianCalendar
javax.xml.namespace.QName
javax.xml.transform.Source
long
short
simpleJAXB.IndividualRequestShort
simpleJAXB.RequestShort
void
我在这里遗失了什么吗?你能给我一个暗示吗?
提前谢谢!
PS:对不起,我问的是这么简单,但我正在学习这个JAXB框架。
问候!
答案 0 :(得分:0)
@XmlRootElement
和@XmlElement
都有namespace
个属性。未定义时,将使用package-info.java
中定义的包的名称空间。
由于您没有指定属性,因此您的所有元素都属于命名空间http://test.org/url1
。