[英]JAXB XML generation
我想知道如何使用 JAXB 生成以下内容。
主要问题——soapenv 中有两个命名空间。 当我制作 xml 然后添加 soap 环境时,它会在不同的行中添加名称空间。 这不是我想要的方式
问题 2 - 而不是 xmlsn="..." 我想要 xmlns:SOME_TEXT="..." 如下所示
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fis="http://fis.certegy.cka.com/">
<soapenv:Body>
<fis:InquiryRequest>
<Version>1.0</Version>
<RequestUID>191919191919191</RequestUID>
<Station>1078686704</Station>
<TranType>40</TranType>
<Consumer>
<FirstName>Susie</FirstName>
<LastName>Smith</LastName>
<Address>
<Line1>100 59th</Line1>
<Line2>Ave NE</Line2>
<City>New York</City>
<State>NY</State>
<Zip>10021</Zip>
</Address>
<Phone>1114589658</Phone>
<EmailAddress>Your.Email@yahoo.com</EmailAddress>
<DateOfBirth>1960-01-01</DateOfBirth>
<ID>
<Type>NY</Type>
<Value>285756967</Value>
</ID>
<DeviceID>12345678000</DeviceID>
<DaysOfEmployment>9991</DaysOfEmployment>
<PayDate>2019-05-03</PayDate>
<PayFrequency>Weekly</PayFrequency>
</Consumer>
<Amount>70.00</Amount>
<CashBack>0</CashBack>
<GiftCard>0</GiftCard>
<Check>
<Micr>
<ExpansionType>1002</ExpansionType>
<Line>T861000016A100002106C</Line>
<Swiped>false</Swiped>
</Micr>
<Type>P</Type>
</Check>
</fis:InquiryRequest>
</soapenv:Body>
</soapenv:Envelope>
请注意:我使用的是EclipseLink MOXY
,它是 JAXB 的扩展,与普通JAXB
相比, JAXB
提供了许多额外的好处。 以下答案基于MOXY
不确定它是否适用于Standard JAXB
。
我不确定我是否正确理解了您的问题 1。 根据我在解决problem-2
时的理解,我猜它也应该解决这个问题。 因为它会将所有命名空间添加到您的案例中的 XML 的XML
中soapenv:Envelope
。
关于您的问题 2:如果您想获取namespace URI
的自定义prefix
,那么您可以使用prefix
和namespace
创建一个Map<String, String>
并在marshaling
期间传递它,以便moxy
自动处理并提供header ( xmlns
) 和您的 XML node
的prefix
。
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put("http://schemas.xmlsoap.org/soap/envelope/", "soapenv");
namespaces.put("http://fis.certegy.cka.com/", "fis");
namespaces.put("Whatever your namespace", "SOME_TEXT");
jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
jsonMarshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespaces);
参考文档
GitHub代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.