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

  • Home
  • SEARCH
  • 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 8131553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:07:30+00:00 2026-06-06T09:07:30+00:00

I created a method to unmarshall my xml (item.xml) file .But if there are

  • 0

I created a method to unmarshall my xml (item.xml) file .But if there are more than one element ,how can i iterate through all the elements and get them displayed?

My Code is as following :

        final JAXBContext jc = JAXBContext.newInstance("com.generated");

        final Unmarshaller u = jc.createUnmarshaller();

        final File f = new File("D:\\item.xml");

        final JAXBElement element = (JAXBElement) u.unmarshal(f);

        final Item item = (Item) element.getValue();

        // This will be helpful only if the xml contains one element
        System.out.println(item.getCode());
        System.out.println(item.getName());
        System.out.println(item.getPrice());

If my xml is

       <item>
         <item1>
            <code>12000</code>
            <name>Samsung Galaxy Tab 620</name>
            <price>9999</price>
         </item1>
         <item2>
            <code>15000</code>
            <name>NOKIA</name>
            <price>19999</price>
         </item2>
         <item3>
            <code>18000</code>
            <name>HTC 620</name>
            <price>29999</price>
         </item3>
       </item>

How can i get all the values displayed? Can anyone 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-06T09:07:31+00:00Added an answer on June 6, 2026 at 9:07 am

    I’ve used JAXB in some projects at university.
    As far as I remember, you should return an object, like an ItemList and then query that object in order to retrieve the elements contained.

    So, your xml should appear somehow like this:

    <itemlist>
       <item>
         <code>..</code>
         <name>..</name>
         <price>..</price>
       </item>
       <item>
         <code>..</code>
         <name>..</name>
         <price>..</price>
       </item>
       .
       .
    </itemlist>
    

    At this point, your Java code will be:

    final Unmarshaller u = jc.createUnmarshaller();
    final File f = new File("D:\\item.xml");
    final JAXBElement element = (JAXBElement) u.unmarshal(f);
    final ItemList itemList = (ItemList) element.getValue();
    
    // This will be helpful if the xml contains more elements
    for (Item item : itemList.getItems()) {
       System.out.println(item.getCode());
       System.out.println(item.getName());
       System.out.println(item.getPrice());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i created a method to read a xml file but it doesn't work twice,
i created a method called insertTable in a class called Table but i can't
I created a web method but when i go to the site and type
I have created a new method in one of the project's controllers that it
I created one method in my model : public IList<Test> GetData(int IDCus){ var list
I have created a method that can be called using a stored procedure name
I'm trying to use JAXB to unmarshall some XML, but I'm getting an Unable
hi friend i created this method for image select it working proper but i
I created a method to basically detect white space characters. I go through a
I created a method and pass the element type, id, and any inner text

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.