JAXB XJC解析机制

大家都知道使用JAXB的xjc工具可以把schema自动生成对应的java类,这仔细对比了一下不同schema的结构生成的java类,有一些不明白的地方

如果schema定义如下
  <xs:element name="PhysicalAddress">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="1">
<xs:element minOccurs="0" maxOccurs="1" ref="addressLine1" />
<xs:element minOccurs="0" maxOccurs="1" ref="addressLine2" />
<xs:element minOccurs="0" maxOccurs="1" ref="addressLine3" />
<xs:element minOccurs="0" maxOccurs="1" ref="cityName" />
<xs:element minOccurs="0" maxOccurs="1" ref="GlobalCountryCode" />
<xs:element minOccurs="0" maxOccurs="1" ref="GlobalLocationIdentifier" />
<xs:element minOccurs="0" maxOccurs="1" ref="NationalPostalCode" />
<xs:element minOccurs="0" maxOccurs="1" ref="postOfficeBoxIdentifier" />
<xs:element minOccurs="0" maxOccurs="1" ref="regionName" />
</xs:sequence>
</xs:complexType>
</xs:element>

生成的java类结果如下
[quote]
public class PhysicalAddress {

protected AddressLine1 addressLine1;
protected AddressLine2 addressLine2;
protected AddressLine3 addressLine3;
protected CityName cityName;
protected String globalCountryCode;
protected String globalLocationIdentifier;
protected String nationalPostalCode;
protected PostOfficeBoxIdentifier postOfficeBoxIdentifier;
protected RegionName regionName;
}
[/quote]

但是,如下的schema结构

<xs:element name="PartnerBusinessIdentification">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="ProprietaryBusinessIdentifier" />
<xs:element ref="ProprietaryDomainIdentifier" />
<xs:element minOccurs="0" maxOccurs="1" ref="ProprietaryIdentifierAuthority" />
</xs:sequence>
</xs:complexType>
</xs:element>

生成的java类却如下所示,可以忽略@XMLElementRef(s)的注释暂时不用管
[quote]
public class PartnerBusinessIdentification {

@XmlElementRefs({
@XmlElementRef(name = "ProprietaryBusinessIdentifier", type = JAXBElement.class),
@XmlElementRef(name = "ProprietaryDomainIdentifier", type = JAXBElement.class),
@XmlElementRef(name = "ProprietaryIdentifierAuthority", type = JAXBElement.class)
})

protected List<JAXBElement<String>> proprietaryBusinessIdentifierAndProprietaryDomainIdentifierAndProprietaryIdentifierAuthority;
}
[/quote]
我就不明白为什么不是生成以下这样的类结构呢?
[quote]
public class PartnerBusinessIdentification {
protected ProprietaryBusinessIdentifier proprietaryBusinessIdentifier;
protected ProprietaryDomainIdentifier proprietaryDomainIdentifier;
protected ProprietaryIdentifierAuthority proprietaryIdentifierAuthority;
}
[/quote]

后来比较多个类似结构的schema发现,问题应该是在<xs:sequence>上,[b]如果它加上了maxOccurs="unbounded"属性的话,就只会在此元素对应的java类中生成一个List<?>类型的元素,而不管里面<xs:sequence>究竟有一个还是多个子元素[/b],这样一来生成的java类就非常难处理了。我正在想对策中
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JAXBJava Architecture for XML Binding)是Java中用于将XML文档与Java对象之间进行映射的技术。它提供了一种简单的方式来将XML数据转换为Java对象,以及将Java对象转换为XML数据。 下面是使用JAXB解析XML的基本步骤: 1. 创建一个Java类,该类将作为XML数据的映射对象。该类需要使用JAXB注解来指定XML元素和属性与Java字段之间的映射关系。 2. 使用JAXB的上下文(JAXBContext)创建一个Unmarshaller对象,用于将XML数据转换为Java对象。 3. 调用Unmarshaller对象的unmarshal()方法,传入XML数据的来源(可以是文件、输入流等),将XML数据解析Java对象。 下面是一个简单的示例代码,演示了如何使用JAXB解析XML: ```java import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.File; public class JAXBExample { public static void main(String[] args) { try { // 创建JAXB上下文 JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); // 创建Unmarshaller Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); // 加载XML文件 File xmlFile = new File("person.xml"); // 解析XML并将其转换为Java对象 Person person = (Person) unmarshaller.unmarshal(xmlFile); // 打印Java对象的属性 System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们创建了一个名为Person的Java类,并使用JAXB注解指定了XML元素与Java字段之间的映射关系。然后,我们使用JAXBContext创建了一个Unmarshaller对象,并将XML文件解析为Person对象。最后,我们打印了Person对象的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值