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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:42:53+00:00 2026-06-18T11:42:53+00:00

I wonder if anyone can help, I have the following xml file, the problem

  • 0

I wonder if anyone can help, I have the following xml file, the problem is I can not see the third node ‘food’. I have created the files using JAXB there are four files have been created ObjectFactory, Breakfast-menu, foodType and food. I can get to foodType from Breakfast-menu and see the name(fresh and frozen) but not ‘food’ and it’s content. can some please give some ideas.
thanks

<Breakfast-menu>
    <foodType>
        <name>fresh</name>
        <food name="Milk">
            <price>2</price>
        </food>
        <food name="Pears">
            <price>3</price>
        </food>
        <food name="Apples">
            <price>1</price>
        </food>
    </foodType>
    <foodType>
        <name>frozen</name>
        <food name="Fish">
            <price>2</price>
        </food>
        <food name="chips">
            <price>1</price>
        </food>
    </foodType>
</Breakfast-menu>

Java

JAXBContext jaxbContext = JAXBContext.newInstance("food");
              Unmarshaller u = jaxbContext.createUnmarshaller();

              BreakfastMenu bM = (BreakfastMenu) u.unmarshal( 
                      new FileInputStream( "C:\\Users\\food.xml" ));
              List<FoodType> tType = bM.getFoodType();                
              for (FoodType tT : tType) {
                System.out.println("\t" + tT.getFood());

              }
  • 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-18T11:42:54+00:00Added an answer on June 18, 2026 at 11:42 am

    Since this use case has a very simple XML document I would recommend starting from Java objects.

    BreakfastMenu

    JAXB (JSR-222) is configuration by exception so you only need to add annotations where you want the binding to differ from the default (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html).

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="Breakfast-menu")
    public class BreakfastMenu {
    
        private List<FoodType> foodType;
    
        public List<FoodType> getFoodType() {
            return foodType;
        }
    
        public void setFoodType(List<FoodType> foodType) {
            this.foodType = foodType;
        }
    
    }
    

    FoodType

    By default the mappings are based on the public properties, but we can change JAXB to use fields with the @XmlAccessorType(XmlAccessType.FIELD) annotation (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class FoodType {
    
        private String name;
    
        @XmlElement(name="food")
        private List<Food> foods;
    
    }
    

    Food

    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Food {
    
        @XmlAttribute
        private String name;
    
        private int price;
    
    }
    

    Demo

    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(BreakfastMenu.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum14751166/input.xml");
            BreakfastMenu bm = (BreakfastMenu) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(bm, System.out);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wonder if anyone can help me with this problem. I have created a Window
I wonder if anyone can help with the following. I have integrated both iAds
I wonder if anyone can help - I have this error showing recently when
I wonder if anyone can help with a jQuery problem I am having. I
I have a VC++ Win32 DLL project. I wonder if anyone can help me
I wonder if anyone can help with a little problem I can't seem to
wonder if anyone can help; I have an a two dimensional array which I
I wonder if anyone can help with the following. using (FbConnection conn = new
Wonder if anyone can help me. I have a setup whereby a main menu
I wonder if anyone can help? I have a list with a collection of

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.