【发布时间】:2015-05-21 16:13:22
【问题描述】:
我目前正在尝试将一个对象编组到 xml,但在编组时,命名空间最终落得太远,我似乎无法弄清楚原因。
这里是 ValuationIn 引用了valuation.xsd 中的 ValuationDetails:
<xs:complexType name="ValuationIn">
<xs:sequence>
<xs:element xmlns:q1="httpAddress/common" minOccurs="0" name="ValuationIdentifier" nillable="true" type="q1:ValuationIdentifier">
<xs:annotation>
<xs:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element xmlns:q2="httpAddress/common" minOccurs="0" name="ValuationDetails" nillable="true" type="q2:ValuationDetails">
<xs:annotation>
<xs:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
这是通用模式中的 ValuationDetails 元素:
<xs:element name="ValuationDetails" nillable="true" type="tns:ValuationDetails"/>
<xs:complexType name="Policy">
<xs:sequence>
<xs:element minOccurs="0" name="PolicyEffectiveDate" nillable="true" type="xs:dateTime">
<xs:annotation>
<xs:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" name="PolicyRenewalDate" nillable="true" type="xs:dateTime">
<xs:annotation>
<xs:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" name="EstimateExpirationDate" nillable="true" type="xs:dateTime">
<xs:annotation>
<xs:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" name="CurrentCoverage" nillable="true" type="xs:int">
<xs:annotation>
<xs:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" name="AccountNumber" nillable="true" type="xs:string">
<xs:annotation>
<xs:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" name="AttachmentCount" type="xs:int"/>
<xs:element minOccurs="0" name="RecordType" nillable="true" type="xs:string">
<xs:annotation>
<xs:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" name="DataSource" nillable="true" type="xs:string">
<xs:annotation>
<xs:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="Policy" nillable="true" type="tns:Policy"/>
这是评估服务架构中的元素:
<xs:element name="GetFastTrackURL">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q35="httpAddress/valuation"
minOccurs="0" name="FastTrackRequest" nillable="true"
type="q35:FastTrackRequest" />
<xs:element xmlns:q36="httpAddress/valuation"
minOccurs="0" name="Valuation" nillable="true" type="q36:ValuationIn" />
<xs:element xmlns:q37="httpAddress/valuation"
minOccurs="0" name="SupplementalDataRequest" nillable="true"
type="q37:SupplementalDataRequest" />
</xs:sequence>
</xs:complexType>
</xs:element>
当我将其分解并与我们在项目中使用的硬编码 XML 进行比较时,我可以看到命名空间已关闭。比较 xml 的一小部分显示顶部的硬编码和底部的生成。
硬编码:
<val:Valuation>
<val1:ValuationDetails>
<com:PropertyAddress>
<com:Address1>Address</com:Address1>
<com:City>City</com:City>
<com:StateProvince>MI</com:StateProvince>
<com:ZipPostalCode>Zip</com:ZipPostalCode>
</com:PropertyAddress>
<com:Building>
<com:MainHome>
<com:YearBuilt>1979</com:YearBuilt>
<com:LivingArea>2000</com:LivingArea>
</com:MainHome>
</com:Building>
</val1:ValuationDetails>
</val:Valuation>
生成:
<val1:ValuationIn>
<com:ValuationDetails>
<com:PropertyAddress>
<com:Address1>Address</com:Address1>
<com:City>City</com:City>
<com:StateProvince>MI</com:StateProvince>
<com:ZipPostalCode>Zip</com:ZipPostalCode>
</com:PropertyAddress>
<com:Building>
<com:MainHome>
<com:YearBuilt>1979</com:YearBuilt>
<com:LivingArea>2000</com:LivingArea>
</com:MainHome>
</com:Building>
</com:ValuationDetails>
</val1:ValuationIn>
我包含的 XML 的子部分是架构中的 q36,它们在硬编码 vs 生成中引用的命名空间是在硬编码的 xml 中 xmlns=valuationService 而在生成的 xmlns=valuation 中(估值和估值服务是两种不同的模式)。
这是 ValuationService.xsd 顶部的导入:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="httpAddress/valuationservice">
<xs:import namespace="httpAddress/valuation" schemaLocation="valuation.xsd" />
<xs:import namespace="httpAddress/common" schemaLocation="common.xsd"/>
我知道要写很多东西可能会相当简单,但我一生都无法弄清楚为什么我的编组 xml 引用的命名空间与主模式中的命名空间不同。
【问题讨论】:
-
您应该包含 jaxb bean 而不是编组器,因为问题与映射有关。附加的 xml 表明问题在于evaluationDetails 位于错误的命名空间 com 而不是 val? 中。封闭的 xsd 模式没有描述 valuatioDetails 元素,因此很难说哪个文档是正确的(硬编码的或生成的),尤其是没有解析命名空间前缀的文档根。请添加那些缺少的位。
-
是的,为混乱道歉,问题是评估详细信息在编组时指的是不正确的命名空间,我不知道如何将其设置为正确的命名空间。
标签: java xml namespaces jaxb