简体   繁体   中英

Using JAXB, marshal nested element in JAVA

I would like to get a xml result as below which using JAXB to get java object.

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

To get this result, I made 3 java classes which are

  1. Define XmlRootElement , XmlElement
  2. component of product (getter/setter)
  3. Main class which insert the value of component

In this way, I could make only 3 depths using XmlRootElement , XmlElement , Component of product.

Hence I need one more depth.. I tried to use a XmlElementWrapper to give one more depth, but there was an error regarding it is not a collecting attribute...

Please help me to solve this out...

Below is class structure that should work

@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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM