具有以下xml作为post参数,如何忽略“ v1:”或“ typ:”命名空间,以便可以将每个标记映射到Java类? 我想从这种xml创建对象,但是由于标签之前的名称空间而出现错误(没有它们的情况就很好,但是我无法更改XML)。
Java控制器方法为:
[ [ { id: 31,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:55:10.000Z' },
{ id: 30,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:54:21.000Z' },
{ id: 32,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:56:09.000Z' },
{ id: 33,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:57:05.000Z' } ],
[ { id: 14,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-04-01T10:24:40.000Z' },
{ id: 15,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-04-01T10:25:10.000Z' },
{ id: 13,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-04-01T10:24:00.000Z' } ] ]
应要求提供的XML是:
@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;
}
错误是:
<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>
答案 0 :(得分:0)
SimpleXml可以做到这一点,首先是一些POJO:
@XmlName("v1:getCompanyStructureDetailsRequest")
public class GetCompanyStructureDetailsRequest {
@XmlName("v1:trackingInformation")
public TrackingInformation tracking;
@XmlName("v1:getCompanyStructureDetailsRequestContent")
public CompanyStructure structure;
}
public class TrackingInformation {
@XmlName("typ:eventLocalID")
public String eventLocalID;
@XmlName("typ:applicationID")
public String applicationID;
@XmlName("typ:originApplicationID")
public String originApplicationID;
}
public class CompanyStructure {
@XmlName("v1:customer")
public Customer customer;
}
public class Customer {
@XmlWrapperTag("v1:organizationIdentifier")
@XmlName("v1:companyIndex")
public Integer companyId;
}
然后序列化并打印:
public static void main(final String... args) throws IOException {
final SimpleXml simple = new SimpleXml();
final GetCompanyStructureDetailsRequest r = simple.fromXml(xml, GetCompanyStructureDetailsRequest.class);
System.out.println(r.structure.customer.companyId);
}
此代码将打印:
33503
SimpleXml位于Maven中央:
<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>simplexml</artifactId>
<version>1.5.5</version>
</dependency>