【问题标题】:Using JAXB, marshal nested element in JAVA使用 JAXB,在 JAVA 中编组嵌套元素
【发布时间】:2019-02-20 06:08:57
【问题描述】:

我想得到一个xml结果如下,它使用JAXB来获取java对象。

<Mall>
  <ProductInfo>
     <Product>
       <name>chair</name>
       <price>150</price>
     </Product>
 </ProductInfo>
</Mall>

为了得到这个结果,我制作了 3 个 java 类

  1. 定义XmlRootElementXmlElement
  2. 产品组件(getter/setter)
  3. 插入组件值的主类

通过这种方式,我只能使用XmlRootElementXmlElement、产品组件来制作 3 个深度。

因此,我需要更多深度.. 我尝试使用XmlElementWrapper 提供更多深度,但出现错误,提示它不是收集属性...

请帮我解决这个问题...

【问题讨论】:

    标签: java xml spring jaxb marshalling


    【解决方案1】:

    下面是应该工作的类结构

    @XmlRootElement (name = "mall")
    public class Mall {
        Mall(){  }
    
        @XmlElement(name="ProductInfo")
        private ProductInfo info; // must create getter and setter
        }
    }
    
    public class ProductInfo {  // you should be missing this
        ProductInfo(){
        }
    
        @XmlElement(name="Product")
        private List<Product> info; // must create getter and setter
        }
    }
    
    public class Product {
        Product(){
        }
    
        @XmlElement(name="name")
        private ProductInfo info; // must create getter and setter
    
        @XmlElement(name="price")
        private ProductInfo info; // must create getter and setter
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多