by
2
7
3,966
0
Top 1% !
Popular
Famous
Tagged
Easy-to-find
Specified
OpenSource
Popularity: 2608th place

Published on:

ClassNameMyNetwork
Frameworkjax-b
Languagejava
LicenseMIT_X11
MethodNamegetVendors
OutputTypeList<Vendor>

MyNetwork class - Java example for marshalling/unmarshalling object to xml using JAX-B

This is the main class of the previous example. MyNetwork contains two lists, one for Equipment class and one for Vendor class.
Since it's the main root of the xml, I set the annotation XmlRootElement, with value = network-topology. Such name will be displayed in the xml.
@XmlType annotation sets the order of the attributes in the xml, using propOrder keyword.
@XmlElement annotation defines the single xml element to be generated.
@XmlElementWrapper sets the attribute name containing each single class instance in lists.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/42435/MyNetwork-class---Java-example-for-marsh?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
package com.test.jaxb; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "network-topology") @XmlType(propOrder={"vendors", "equipments"}) @XmlAccessorType(XmlAccessType.FIELD) public class MyNetwork { @XmlElementWrapper(name = "equipments-list") @XmlElement(name = "equipment") private List<Equipment> equipments; @XmlElementWrapper(name = "vendors-list") @XmlElement(name = "vendor") private List<Vendor> vendors; /** * @return the vendors */ public List<Vendor> getVendors() { return vendors; } /** * @param vendors the vendors to set */ public void setVendors(List<Vendor> vendors) { this.vendors = vendors; } /** * @return the equipments */ public List<Equipment> getEquipments() { return equipments; } /** * @param equipments the equipments to set */ public void setEquipments(List<Equipment> equipments) { this.equipments = equipments; } /** * @param eqp the equipment to add */ public void addEquipments(Equipment eqp) { if (equipments == null) { this.equipments = new ArrayList(); } equipments.add(eqp); } /** * @param vendor the equipment to add */ public void addVendor(Vendor vendor) { if (vendors == null) { this.vendors = new ArrayList(); } vendors.add(vendor); } }

Other Steps and Solutions

If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus