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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:17:29+00:00 2026-06-01T22:17:29+00:00

Could someone help me understand why I’m getting this error: javax.xml.bind.UnmarshalException: unexpected element (uri:,

  • 0

Could someone help me understand why I’m getting this error:

javax.xml.bind.UnmarshalException: unexpected element (uri:””, local:”items”). Expected elements are <{}item>

I’ve new to JAX-B but been stuck on this all day, I really don’t understand whats happening and any help is really appreciated, thanks a lot.

Item Class:

@XmlRootElement

public class Item {

private String itemID;
private String itemDescription;

//need to have a constructor with no params
public Item(){

}

//Constructor: sets object vars
public Item(String itemID, String itemDescription) {

    this.itemID = itemID;
    this.itemDescription = itemDescription;
}

@XmlAttribute
//getters and setters
public String getID() {
    return itemID;
}

public void setId(String id) {
    itemID= id;
}

@XmlElement
public String getDescription() {
    return itemDescription;
}

public void setDescription(String description) {
    itemDescription = description;
}

Unmarshalling code:

resource = client.resource("http://localhost:8080/testProject/rest/items");
    ClientResponse response= resource.get(ClientResponse.class);
    String entity = response.getEntity(String.class);

    System.out.println(entity);

    JAXBContext context = JAXBContext.newInstance(Item.class);
    Unmarshaller um = context.createUnmarshaller();
    Item item = (Item) um.unmarshal(new StringReader(entity));


And this is the XML i'm trying to parse:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <items>
      <item id="1">
        <description>Chinos</description>
      </item>
      <item id="2">
        <description>Trousers</description>
      </item>
</items>

Here is the Web Service that is creating the XML:

@GET
            @Produces(MediaType.TEXT_XML)
            public List<Item> getItemsBrowser(){

                java.sql.Connection connection;
                java.sql.Statement statement;

                List<Item> items = new ArrayList<Item>();


                ResultSet resultSet = null;

                try {
                    connection = dataSource.getConnection();
                    statement = connection.createStatement();

                    String query = "SELECT * FROM ITEMS";

                    resultSet = statement.executeQuery(query);

                    // Fetch each row from the result set
                    while (resultSet.next()) {
                      String a = resultSet.getString("itemID");

                      String b = resultSet.getString("itemDescription");

                      //Assuming you have a user object
                      Item item = new Item(a, b);

                      items.add(item);
                    }


                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                return items;
            }
  • 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-01T22:17:31+00:00Added an answer on June 1, 2026 at 10:17 pm

    The class you’re creating the JAXBContext from is Item.class, but the XML contains a list called items which in turn contains distinct item entries. You would need another class that wraps a

    List<Item>
    

    for this to work.

    Here’s a full working example:

    The Items class:

    import java.util.List;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Items {
    
        private List<Item> items;
    
        @XmlElement(name="item")
        public List<Item> getItems() {
            return items;
        }
    
        public void setItems(List<Item> items) {
            this.items = items;
        }
    
    }
    

    Note that there is an @XmlElement annotation on the items property, because the actual elements are called “item” in the XML.

    The Item class:

    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    
    public class Item {
    
        private String itemID;
        private String itemDescription;
    
        // need to have a constructor with no params
        public Item() {}
    
        public Item(String itemID, String itemDescription) {
            this.itemID = itemID;
            this.itemDescription = itemDescription;
        }
    
        @XmlAttribute
        public String getId() {
            return itemID;
        }
    
        public void setId(String id) {
            itemID = id;
        }
    
        @XmlElement
        public String getDescription() {
            return itemDescription;
        }
    
        public void setDescription(String description) {
            itemDescription = description;
        }
    }
    

    And a unit test:

    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    
    import java.io.File;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Unmarshaller;
    
    import org.junit.Test;
    
    public class JAXBTest {
    
        @Test
        public void xmlIsUnmarshalled() throws JAXBException {
            JAXBContext context = JAXBContext.newInstance(Items.class);
            Unmarshaller um = context.createUnmarshaller();
            Items items = (Items) um.unmarshal(new File("items.xml"));
    
            assertNotNull(items);
            assertNotNull(items.getItems());
            assertEquals(2, items.getItems().size());
    
            assertEquals("Chinos", items.getItems().get(0).getDescription());
            assertEquals("Trousers", items.getItems().get(1).getDescription());
    
            assertEquals("1", items.getItems().get(0).getId());
            assertEquals("2", items.getItems().get(1).getId());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please help me understand that why am I getting this error at
Could someone help me understand the primitive accessors with this example : i don't
Please could someone help me understand why the div.fl element shown in Developer Tools
could someone help me to understand why this errors document.getElementById(actContentToGet).contentWindow.document.body.getElementById is not a function
Could someone help me understand why this link works perfectly in firefox but in
could someone help me on this problem, i want to access facebook API through
Could someone help me on this, I have created simple web services using axis2
Could someone please help explain why I can't get this to work? I properly
I'm new to Flex. Could someone help me understand how Flex generally works with
could someone help me to understand how can I define an entity with JPA

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.