我有XML這樣的:JAXB不能處理XML元素含有白色空間java.net.URI中的映射
<repository>
<location>/home/username whitespace/somedir</location>
</repository>
我使用JAXB來解組到JAXB註解Bean的這一點。 「位置」XML元素映射到java.net.URI
類。 問題是,當位置包含空白時,JAXB(或可能是底層的XML解析器)無法處理此位置,並且位置設置爲null
。沒有UnmarshalException
或任何東西,只是setLocation(URI loc)
被稱爲null
方法的參數。
當然,沒有空白的URI工作正常。 事情是,我不能真的改變RepositoryDTO
說有String location;
字段。
你能告訴我在這裏有什麼選擇嗎?
我應該看看URLEncode然後解碼說location
字段?
這是一個REST /澤西使用情況的方式,但顯然是罪魁禍首在於JAXB/XML解析器...
public class Whitespace {
public static void main(String[] args) throws JAXBException {
String xml = "<repository><location>/home/username whitespace/somedir</location></repository>";
Unmarshaller createUnmarshaller = JAXBContext.newInstance(RepositoryDTO.class).createUnmarshaller();
RepositoryDTO repositoryInfo = (RepositoryDTO) createUnmarshaller.unmarshal(new StringReader(xml));
System.out.println(repositoryInfo.getLocation());
}
@XmlRootElement(name = "repository")
static public class RepositoryDTO {
private URI location;
@XmlElement
public URI getLocation() {
return location;
}
public void setLocation(URI loc) {
this.location = loc;
}
}
}
接受這個答案,雖然我寧願使用URLEncoder或類似於自己編碼。 – Svilen