You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java XML编组/解组问题:编组时重复生成Parent1Child1元素

排查JAXB编组时Parent1Child1标签重复的问题

Hey there! Let's break down why you're seeing duplicate Parent1Child1 tags (one inside the Parent1 wrapper, one outside) when marshalling your XML with JAXB. This is a common gotcha, and it almost always boils down to annotation misconfiguration or redundant references in your classes. Here are the most likely causes and fixes:

1. Mixed Annotation Placement (Fields vs. Getters)

JAXB by default scans both fields and getter/setter methods for annotations unless you explicitly tell it otherwise. If you've added JAXB annotations to both a field and its corresponding getter method for Parent1Child1, JAXB will treat them as two separate properties—resulting in duplicate tags.

Example of the Problem:

public class Parent1 {
    // Annotation on the field
    @XmlElement(name = "Parent1Child1")
    private Parent1Child1 child;

    // Annotation also on the getter
    @XmlElement(name = "Parent1Child1")
    public Parent1Child1 getChild() {
        return child;
    }
}

Fix:

  • Pick one approach: annotate either fields OR getter methods, not both.
  • Or add @XmlAccessorType(XmlAccessType.FIELD) to your class to explicitly tell JAXB to only process fields:
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Parent1 {
        @XmlElement(name = "Parent1Child1")
        private Parent1Child1 child;
    
        // No annotation on getter
        public Parent1Child1 getChild() {
            return child;
        }
    }
    

2. Redundant References in the Root Class

If your RootElement class directly references a Parent1Child1 instance in addition to referencing the Parent1 class (which contains Parent1Child1), JAXB will marshal both references—creating one tag inside Parent1 and one outside.

Example of the Problem:

@XmlRootElement(name = "Root")
public class RootElement {
    @XmlElement(name = "Parent1")
    private Parent1 parent1;

    // Oops! This direct reference creates an extra tag outside Parent1
    @XmlElement(name = "Parent1Child1")
    private Parent1Child1 child1;
}

Fix:

Remove the direct Parent1Child1 reference from your RootElement class, leaving only the Parent1 reference that wraps the child element.

3. Misconfigured @XmlElementWrapper

If you're using @XmlElementWrapper to create the Parent1 wrapper, a misconfiguration can lead to duplicate tags. For example:

  • Forgetting that @XmlElementWrapper is meant for collections, and using it with a single Parent1Child1 instance.
  • Accidentally mapping both the wrapper and the child element to the same tag name.

Correct Usage for a Single Child:

If Parent1 is just a wrapper for one Parent1Child1, you don't need @XmlElementWrapper—just annotate the child field inside Parent1:

@XmlAccessorType(XmlAccessType.FIELD)
public class Parent1 {
    @XmlElement(name = "Parent1Child1")
    private Parent1Child1 child;
}

Correct Usage for a Collection of Children:

If you have multiple Parent1Child1 elements inside Parent1, use @XmlElementWrapper with @XmlElement for the elements:

@XmlAccessorType(XmlAccessType.FIELD)
public class Parent1 {
    @XmlElementWrapper(name = "Parent1")
    @XmlElement(name = "Parent1Child1")
    private List<Parent1Child1> children;
}

Quick Troubleshooting Step

Double-check all your class annotations to ensure:

  • No duplicate annotations on fields and getters.
  • No redundant references to Parent1Child1 in higher-level classes like RootElement.
  • @XmlElementWrapper is used correctly (only for collections, with matching element annotations).

If you still run into issues, sharing snippets of your actual RootElement, Parent1, and Parent1Child1 code would help narrow it down further!

内容的提问来源于stack exchange,提问作者user3807741

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:08:30