xml Java:如何忽略标记前缀
将以下xml作为post参数,如何忽略“v1:”或“typ:”名称空间,以便将每个标记映射到Java类? 我想从这类xml创建一个对象,但由于标记之前的名称空间,我遇到了一个错误(没有它们可以正常工作,但我无法更改xml)
Java控制器方法是:
@RequestMapping(value = "/XML", method = RequestMethod.POST, produces = {MediaType.APPLICATION_ATOM_XML_VALUE})
public ResponseEntity<GetCompanyStructureDetailsResponseContent> getXml(@RequestBody GetCompanyStructureDetailsRequest request){
// how should I filter this XML....
return null;
}
请求时的XML是:
<v1:getCompanyStructureDetailsRequest>
<v1:trackingInformation>
<typ:eventLocalID>234233423434234234</typ:eventLocalID>
<typ:applicationID>MSD</typ:applicationID>
<typ:originApplicationID>MSD</typ:originApplicationID>
</v1:trackingInformation>
<v1:getCompanyStructureDetailsRequestContent>
<v1:customer>
<v1:organizationIdentifier>
<v1:companyIndex>33503</v1:companyIndex>
</v1:organizationIdentifier>
</v1:customer>
</v1:getCompanyStructureDetailsRequestContent>
</v1:getCompanyStructureDetailsRequest>
错误为:
w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to [class com.example.dto.request.GetCompanyStructureDetailsRequest]: null; nested exception is javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 39; The prefix "v1" for element "v1:getCompanyStructureDetailsRequest" is not bound.]
# 1 楼答案
SimpleXml可以做到这一点,首先是一些POJO:
然后序列化并打印:
此代码将打印:
SimpleXml位于maven central: