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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:42:29+00:00 2026-06-16T22:42:29+00:00

I have an object that I need to marshal to XML. To do this

  • 0

I have an object that I need to marshal to XML. To do this I am using JAXB. The resulting XML file is created, however I end up with duplicate nodes:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order>
    <supplier_id>suppliername</supplier_id>
    <store_name>storename</store_name>
    <order_id>1234567890</order_id>
    <order_item>
        <order_item_id>0987654321</order_item_id>
        <item_state>Shipped</item_state>
        <item_state_date>2012-12-27T14:40:00.000-05:00</item_state_date>
        <carrier_code>NA</carrier_code>
        <carrier_name>CarrierName</carrier_name>
        <shipping_method>Expedited</shipping_method>
        <tracking_number>348134987124872734817324</tracking_number>
        <backorder_replacement_sku_price>0.0</backorder_replacement_sku_price>
    </order_item>
    <order_item>
        <order_item_id>0987654321</order_item_id>
        <item_state>Shipped</item_state>
        <item_state_date>2012-12-27T14:40:00.000-05:00</item_state_date>
        <carrier_code>NA</carrier_code>
        <carrier_name>CarrierName</carrier_name>
        <shipping_method>Expedited</shipping_method>
        <tracking_number>348134987124872734817324</tracking_number>
        <backorder_replacement_sku_price>0.0</backorder_replacement_sku_price>
    </order_item>
    ....

I only require the first <order_item> node, and subsequent ones are there erroneously.

My code is as follows:

JAXBContext context = JAXBContext.newInstance(classname.class.getPackage().getName());
Marshaller marsheller = context.createMarshaller();

Order order = new Order();
OrderItem orderItem = new OrderItem();

...Business logic related to our ERP system...
//We start a loop here to iterate over search results. The goal is to have one XML file per search row returned. No knowledge of the ERP system is required. 

for (SearchRow row : results.getSearchRowList().getSearchRow()) {   
    //we capture our variables from the search results here and build our order and orderItem objects with them. 

    order.setSupplierId("suppliername");
    order.setStoreName("storename");
    order.setOrderId(orderNumber);
    orderItem.setOrderItemId(orderItemId);
    orderItem.setItemState(StatusCodeType.SHIPPED);
    orderItem.setItemStateDate(date2);
    orderItem.setCarrierCode("NA");
    orderItem.setCarrierName(carrierName);
    orderItem.setShippingMethod(shippingMethod);
    orderItem.setTrackingNumber(trackingNumber);

    order.getOrderItem().add(orderItem);
    File f = new File("tempFiles/" + order.getOrderId() + ".xml");
    marsheller.marshal(order, f);
}

I thought that the issue was the loop, but if I log all the variables after object creation from the objects themselves only show orderItem result and not multiple orderItems.

Can anyone point me in the direction where I am going off the tracks?

  • 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-16T22:42:30+00:00Added an answer on June 16, 2026 at 10:42 pm

    You either need to

    • create a new Order() and new OrderItem() each iteration round the loop or
    • move the order.getOrderItem().add(orderItem); to outside the loop immediately after you create the Order and OrderItem instances.

    Your current logic is adding the same OrderItem instance to the list each time round the loop, so the first iteration you’ll get one <order_item> element, the second iteration you’ll get two identical ones, the third iteration three, etc.

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

Sidebar

Related Questions

I have an Object that is being marshalled to XML using JAXB. One element
I have an object that I need to serialize in C#, however this object
I have an object that needs to be returned, however I need to perform
I have an object that is created by Newtonsoft's JSON serializer. I need to
Is this possible? I have a service that may need to marshal objects from
I have a list of object that need to be displayed and moveable on
I have a pretty large object that I need to pass to a function
I have a nested JSON object that I need to loop through, and the
If I have an object that holds the parameters for my method. I need
I have a simple Dictionary(of String, Object) that I need to iterate through and

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.