简体   繁体   中英

Accessing a List autogenerated from XML Schema by Netbeans

I've auto generated code in Netbeans for an XML schema document into a package named jaxb. The root element Nutrition contains a child element named food that may appear many times.

<xsd:element name="food" maxOccurs="unbounded">

The Nutrition Object created by the autogeneration contains a protected List of Food objects.

protected List<Nutrition.Food> food;

When I try to add a Food object to the List with dot notation I can't access the list to add Food objects

Nutrition nutrition = objFactory.createNutrition();  //make a Nutrition object
Food food1 = objFactory.createNutritionFood();       // make a Food object
nutrition.food.add(food1);                           // add a Food object

Netbeans complains that "food has protected access in jaxb.Nutrition" I can't make the List public because it's auto generated. I've looked through the auto generated code for other methods with a reference to the List and there is only a getter that returns a copy of the list. How do I access the List to add a food object?

The answer lies in your question, just use the getter to get the list and then add the object to it.

You are trying to access a property of the object nutrition which is protected so the way to access it is by using the get/set methods. This concept is known as encapsulation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM