【问题标题】:JAXB XML generationJAXB XML 生成
【发布时间】:2021-08-23 23:58:06
【问题描述】:

我想知道如何使用 JAXB 生成以下内容。

主要问题 - 在 soapenv 中有两个命名空间。当我制作 xml 然后添加soap env时,它会在不同的行中添加名称空间。这不是我想要的方式

问题 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>

【问题讨论】:

    标签: java xml jaxb


    【解决方案1】:

    请注意:我使用的是EclipseLink MOXY,它是JAXB 的扩展,与普通JAXB 相比,它提供了许多额外的好处。以下答案基于MOXY,不确定它是否适用于Standard JAXB

    我不确定我是否正确理解了您的问题 1。根据我在解决problem-2 时的理解,我猜它也应该解决这个问题。因为它会将所有命名空间添加到 XML 的标题中,在您的情况下为 soapenv:Envelope

    关于您的问题 2: 如果您想为您的namespace URI 获取自定义prefix,那么您可以使用您的prefixnamespace 创建一个Map&lt;String, String&gt;,并在marshaling 期间传递它,这样moxy 将自动处理和将 prefix 提供给标头 (xmlns) 和您的 XML node

    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);
    
    

    参考documentation

    GitHub code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2011-06-27
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 2011-01-10
      • 2012-04-10
      相关资源
      最近更新 更多