JAXB minOccurs = 0。元素存在与否? [英] JAXB minOccurs=0. Element exists or not?

查看:74
本文介绍了JAXB minOccurs = 0。元素存在与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML架构:

<xsd:element name="Person">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="name" type="xsd:string" />
   <xsd:element name="lat" type="xsd:double" minOccurs="0"/>
   <xsd:element name="lon" type="xsd:double" minOccurs="0"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>

我有一条XML消息:

<Person>
 <name>Fred</name>
</Person>

我使用JAXB自动生成我的类(即Person.java等)。

I use JAXB to auto-generate my classes (i.e. Person.java, etc).

所以在运行时我使用JAXB解组上面的XML消息并获取Person对象实例。当我执行 p.getLat() p.getLon()时,返回值为0.0,即使原始值为0.0 XML不包含< lat> < lon> 元素。

So at run time I use JAXB to unmarshal the above XML message and get a Person object instance. When I do a p.getLat() or p.getLon() the return values are 0.0 even though the original XML didn't contain <lat> or <lon> elements.

更糟糕的是0.0,0.0是有效的纬度和经度。一个人在那里很少见,但这不是重点!

What makes this worse is that 0.0, 0.0 is a valid latitude and longitude. It's rare for a person to be located there but that's beside the point!

文章建议使用其他XML元素作为元数据来明确说明是否存在可选元素。即。

An article on the IBM site suggested using an additional XML element as metadata to explicitly state whether the optional element exists or not. i.e.

<xsd:element name="hasLat" type="xsd:boolean"/>
<xsd:element name="hasLon" type="xsd:boolean"/>

因此上面的XML消息将成为:

So the XML message above would become:

<Person>
 <name>Fred</name>
 <hasLat>false</hasLat>
 <hasLon>false</hasLon>
</Person>

这看起来像是一个丑陋的黑客。必须有一个正确的方法与JAXB检查元素是否存在,以便我可以信任我的 getLat() getLon()的返回值

This seems like an ugly hack. There must be a proper way with JAXB to check if the element existed so that I can trust the return value from my getLat(), getLon()?

推荐答案

我根本没有看到这个问题。对我来说 xjc 生成一个 Person 类,其属性为 lat lon 类型 Double

I don't see that problem at all. For me xjc generates a Person class with the properties lat and lon with type Double.

如果我解散一个没有< lat> < lon> 元素的XML文件,然后生成 Person 对象具有 null 这些属性的值,正如我所料。

If I unmarshall an XML file with no <lat> or <lon> elements, then the resulting Person objects has null values for those properties, as I'd expect.

我不知道你如何在任何地方获得 0.0

I don't know how you get 0.0 anywhere.

我的XML架构:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.com/person">
 <xsd:element name="Person">
  <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="name" type="xsd:string" />
    <xsd:element name="lat" type="xsd:double" minOccurs="0"/>
    <xsd:element name="lon" type="xsd:double" minOccurs="0"/>
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>

我的 Test.java

import com.example.person.Person;
import javax.xml.bind.JAXB;
import java.io.File;

public class Test {
  public static void main(String[] args) {
    Person p = JAXB.unmarshal(new File("foo.xml"), Person.class);
    System.out.println(p.getName());
    System.out.println(p.getLat());
    System.out.println(p.getLon());
  }
}

我的 foo.xml

<Person>
 <name>Fred</name>
 <lat>1.0</lat>
</Person>

输出:

Fred
1.0
null

这篇关于JAXB minOccurs = 0。元素存在与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆