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 8171875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:35:58+00:00 2026-06-06T21:35:58+00:00

I am trying to create objects using binding data from xml file to classes

  • 0

I am trying to create objects using binding data from xml file to classes which has generated from schema file xsd but it is giving null.

here is my xsd , from which I have generated my java classes :

  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

     <xsd:element name="people">
        <xsd:complexType>
           <xsd:sequence>
               <xsd:element ref="employee"/>
               <xsd:element ref="customer"/>
           </xsd:sequence>
         </xsd:complexType>
       </xsd:element>

      <xsd:element name="employee">
     <xsd:complexType>
             <xsd:sequence>
                     <xsd:element ref='name'/>
                     <xsd:element ref='country'/>
              </xsd:sequence>
         </xsd:complexType>
      </xsd:element>

       <xsd:element name='name'>
               <xsd:complexType>
                  <xsd:sequence>
                        <xsd:any namespace='http://www.w3.org/namespace/'/>
                   </xsd:sequence>
              </xsd:complexType>
       </xsd:element>

       <xsd:element name='country'>
             <xsd:complexType>
                 <xsd:sequence>
                     <xsd:any namespace='http://www.w3.org/namespace/'/>
                 </xsd:sequence>
              </xsd:complexType>
       </xsd:element>

       <xsd:element name="customer">
            <xsd:complexType>
            <xsd:sequence>
                    <xsd:element ref='cname'/>
                </xsd:sequence>
            </xsd:complexType>
       </xsd:element>

       <xsd:element name='cname'>
             <xsd:complexType>
                     <xsd:sequence>
                          <xsd:any namespace='http://www.w3.org/namespace/'/>
                     </xsd:sequence>
              </xsd:complexType>
        </xsd:element>
    </xsd:schema>  

My XML File:

      <?xml version="1.0" encoding="UTF-8"?>
       <people>
           <employee>
              <name>John</name>            
              <country>India</country>
           </employee>
           <customer>
              <cname>steve</cname>            
           </customer>
       </people>

and here my code which trying to bind xml data to java objects , but giving null:

 File file = new File("D:\\file.xml");  
 JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.xmlbinding");  
 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  People element = (People) jaxbUnmarshaller.unmarshal(file);  
 System.out.println(element.getEmployee().getName().getAny()); //giving null

could somebody please help me ….

  • 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-06-06T21:36:00+00:00Added an answer on June 6, 2026 at 9:36 pm
       <xsd:element name='name'>
               <xsd:complexType>
                  <xsd:sequence>
                        <xsd:any namespace='http://www.w3.org/namespace/'/>
                   </xsd:sequence>
              </xsd:complexType>
       </xsd:element>
    

    means that element name may contain any XML element. No wonder you’re getting null, because XML contains text content.

    One solution may be to change schema to this:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    
      <xsd:element name="people">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref="employee" />
            <xsd:element ref="customer" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    
      <xsd:element name="employee">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref='name' />
            <xsd:element ref='country' />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    
      <xsd:element name='name'>
        <xsd:complexType mixed="true">
          <xsd:sequence>
            <xsd:any namespace='http://www.w3.org/namespace/' />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    
      <xsd:element name='country'>
        <xsd:complexType mixed="true">
          <xsd:sequence>
            <xsd:any namespace='http://www.w3.org/namespace/' />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    
      <xsd:element name="customer">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref='cname' />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    
      <xsd:element name='cname'>
        <xsd:complexType mixed="true">
          <xsd:sequence>
            <xsd:any namespace='http://www.w3.org/namespace/' />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    

    (mind mixed="true"). This way you’ll get:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "content"
    })
    @XmlRootElement(name = "name")
    public class Name {
    
        @XmlMixed
        @XmlAnyElement(lax = true)
        protected List<Object> content;
    

    instead of:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "any"
    })
    @XmlRootElement(name = "name")
    public class Name {
    
        @XmlAnyElement(lax = true)
        protected Object any;
    

    and after unmarshalling, you’ll get:

    Eclipse's debug view

    EDIT: case when changing XSD is not an option

    This is valid XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <people xmlns:p1="http://www.w3.org/namespace/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema2.xsd">
      <employee>
        <name>
          <p1:a>x</p1:a>
        </name>
        <country>
          <p1:a>x</p1:a>
        </country>
      </employee>
      <customer>
        <cname>
          <p1:a>x</p1:a>
        </cname>
      </customer>
    </people>
    

    Then after unmarshalling you’ll get:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using XmlReader.Create to retrieve data from an RSS xml file. Then I
I'm trying to create a new System.Threading.Thread object using Jscript, but I can't get
I am using backbone and trying to create a new object and I am
I'm trying to create an instance of a generic class using a Type object.
I am trying to create new Event objects to be persisted in the database
I am trying to create an array of class objects taking an integer argument.
I'm trying to create an NSDictionary that stores objects with keys based on IDs.
I'm trying to create a method that will sum two timeO objects and return
I'm trying to create a loop that creates a series of objects that contains
I'm trying to create a 2 dimensional array of Node objects as follows public

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.