Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 462989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:07:49+00:00 2026-05-12T23:07:49+00:00

I have an XML schema: <xsd:element name=Person> <xsd:complexType> <xsd:sequence> <xsd:element name=name type=xsd:string /> <xsd:element

  • 0

I have an XML schema:

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

And I have an XML message:

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

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

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.

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!

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"/>

So the XML message above would become:

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

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()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:07:49+00:00Added an answer on May 12, 2026 at 11:07 pm

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

    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.

    I don’t know how you get 0.0 anywhere.

    My XML Schema:

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

    My 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());
      }
    }
    

    My foo.xml:

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

    Output:

    Fred
    1.0
    null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this (outrageously) simplified XML schema: <xsd:complexType name=Person> <xsd:sequence> <xsd:element ref=FirstName/> <xsd:element
I have an xsd (for basecamp xml) that contains <xsd:complexType name=ProjectType> <xsd:sequence> <xsd:element name=announcement
We have an XSD which defines the following: <xsd:element name=Product type=cm:Product abstract=true /> <xsd:complexType
I have the following .xsd code: <?xml version=1.0 encoding=UTF-8?> <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema id=MyDataSet> <xs:element name=Row>
I have an XML Schema Definition file .XSD who's elements are in Spanish. I
I have created an XML schema (foo.xsd) and used xjc to create my binding
I have an xml message that is in the older schema (xsd) format. My
I want to connect my log4j.xml with log4j.xsd (xml schema). Project don't have any
I have a complex XSD schema and hundreds of XML files conforming to the
I have this XML <?xml version=1.0 encoding=utf-8?> <root> <xsd:schema id=root xmlns= xmlns:xsd=http://www.w3.org/2001/XMLSchema xmlns:msdata=urn:schemas-microsoft-com:xml-msdata> <xsd:import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.