JAXB 解组问题
JAXB unmarshall issue
我必须在 Java 对象中解析以下 xml:
<ArrayOfTranslateArrayResponse xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<TranslateArrayResponse>
<From>it</From>
<OriginalTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>5</a:int>
</OriginalTextSentenceLengths>
<TranslatedText>Mom</TranslatedText>
<TranslatedTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>3</a:int>
</TranslatedTextSentenceLengths>
</TranslateArrayResponse>
<TranslateArrayResponse>
<From>it</From>
<OriginalTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>4</a:int>
</OriginalTextSentenceLengths>
<TranslatedText>Dad</TranslatedText>
<TranslatedTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>3</a:int>
</TranslatedTextSentenceLengths>
</TranslateArrayResponse>
这是我实现的两个 class:
@XmlRootElement(name="ArrayOfTranslateArrayResponse")
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@ToString
public class TranslateResponse {
@XmlElement(name="TranslateArrayResponse")
private LinkedList<TranslatedResponseObject> translations;
}
和
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@ToString
public class TranslatedResponseObject {
@XmlElement(name="TranslatedText")
private String translatedText;
}
根据提供的 xml 示例的解组,我收到了字段翻译的空值。怎么了?
您可以使用 ValidationEventHandler 来调试问题。
public class MyValidationEventHandler implements ValidationEventHandler {
@Override
public boolean handleEvent(ValidationEvent event) {
switch (event.getSeverity()) {
case ValidationEvent.WARNING:
case ValidationEvent.ERROR:
case ValidationEvent.FATAL_ERROR: {
log.error("Validation event : {} in {} line {} col {}",
event.getMessage(),
event.getLocator().getURL(),
event.getLocator().getLineNumber(),
event.getLocator().getColumnNumber());
return false;
}
default:
return true;
}
}
}
你可以像下面这样使用它:
final JAXBContext jAXBContext = JAXBContext.newInstance(target);
final Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
unmarshaller.setEventHandler(new MyValidationEventHandler());
unmarshaller.unmarshal(file);
感谢@klonq 建议调试问题,我得到了解决方案。有必要在每个@XmlElement 和@XmlRootElement 上添加命名空间,这样:
@XmlRootElement(name="ArrayOfTranslateArrayResponse", namespace="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2")
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@ToString
public class TranslateResponse {
@XmlElement(name="TranslateArrayResponse", namespace="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2")
private LinkedList<TranslatedResponseObject> translations;
}
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@ToString
public class TranslatedResponseObject {
@XmlElement(name="TranslatedText", namespace="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2")
private String translatedText;
}
我必须在 Java 对象中解析以下 xml:
<ArrayOfTranslateArrayResponse xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<TranslateArrayResponse>
<From>it</From>
<OriginalTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>5</a:int>
</OriginalTextSentenceLengths>
<TranslatedText>Mom</TranslatedText>
<TranslatedTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>3</a:int>
</TranslatedTextSentenceLengths>
</TranslateArrayResponse>
<TranslateArrayResponse>
<From>it</From>
<OriginalTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>4</a:int>
</OriginalTextSentenceLengths>
<TranslatedText>Dad</TranslatedText>
<TranslatedTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>3</a:int>
</TranslatedTextSentenceLengths>
</TranslateArrayResponse>
这是我实现的两个 class:
@XmlRootElement(name="ArrayOfTranslateArrayResponse")
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@ToString
public class TranslateResponse {
@XmlElement(name="TranslateArrayResponse")
private LinkedList<TranslatedResponseObject> translations;
}
和
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@ToString
public class TranslatedResponseObject {
@XmlElement(name="TranslatedText")
private String translatedText;
}
根据提供的 xml 示例的解组,我收到了字段翻译的空值。怎么了?
您可以使用 ValidationEventHandler 来调试问题。
public class MyValidationEventHandler implements ValidationEventHandler {
@Override
public boolean handleEvent(ValidationEvent event) {
switch (event.getSeverity()) {
case ValidationEvent.WARNING:
case ValidationEvent.ERROR:
case ValidationEvent.FATAL_ERROR: {
log.error("Validation event : {} in {} line {} col {}",
event.getMessage(),
event.getLocator().getURL(),
event.getLocator().getLineNumber(),
event.getLocator().getColumnNumber());
return false;
}
default:
return true;
}
}
}
你可以像下面这样使用它:
final JAXBContext jAXBContext = JAXBContext.newInstance(target);
final Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
unmarshaller.setEventHandler(new MyValidationEventHandler());
unmarshaller.unmarshal(file);
感谢@klonq 建议调试问题,我得到了解决方案。有必要在每个@XmlElement 和@XmlRootElement 上添加命名空间,这样:
@XmlRootElement(name="ArrayOfTranslateArrayResponse", namespace="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2")
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@ToString
public class TranslateResponse {
@XmlElement(name="TranslateArrayResponse", namespace="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2")
private LinkedList<TranslatedResponseObject> translations;
}
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@ToString
public class TranslatedResponseObject {
@XmlElement(name="TranslatedText", namespace="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2")
private String translatedText;
}