title: How to make JAXB xml array type in Java?
body: <p>I am building an API, and I have tried with Postman create a new "<code>items</code>" and only accepts the the "<code>Items</code>" with like this: <code>&lt;items type="array"&gt;</code></p>
  
  <p>My output does not have the <code>type="array"</code> that I want. I want to know <strong>how can I make in java ou jaxb the <code>type="array"</code></strong> manually or not to my output (this is the code part that prints my xml output <code>jaxbMarshaller.marshal(invoiceRec, System.out);</code> ).</p>
  
  <p>So <strong>this is my output</strong> right now is this, and my objective is to make this:</p>
  
  <pre><code>&lt;items&gt;
          &lt;item&gt;
              &lt;id&gt;1162842&lt;/id&gt;
              &lt;name&gt;tatat2&lt;/name&gt;
              &lt;description&gt;1&lt;/description&gt;
              &lt;unit_price&gt;1.0&lt;/unit_price&gt;
              &lt;unit&gt;hour&lt;/unit&gt;
          &lt;/item&gt;
          &lt;item&gt;
              &lt;id&gt;1163631&lt;/id&gt;
              &lt;name&gt;testItem2333&lt;/name&gt;
              &lt;description&gt;abc&lt;/description&gt;
              &lt;unit_price&gt;1.0&lt;/unit_price&gt;
              &lt;unit&gt;hour&lt;/unit&gt;
          &lt;/item&gt;
      &lt;/items&gt;
  </code></pre>
  
  <p><strong>I want to make it into this:</strong></p>
  
  <pre><code>&lt;items type="array"&gt;
          &lt;item&gt;
              &lt;id&gt;1162842&lt;/id&gt;
              &lt;name&gt;tatat2&lt;/name&gt;
              &lt;description&gt;1&lt;/description&gt;
              &lt;unit_price&gt;1.0&lt;/unit_price&gt;
              &lt;unit&gt;hour&lt;/unit&gt;
          &lt;/item&gt;
          &lt;item&gt;
              &lt;id&gt;1163631&lt;/id&gt;
              &lt;name&gt;testItem2333&lt;/name&gt;
              &lt;description&gt;abc&lt;/description&gt;
              &lt;unit_price&gt;1.0&lt;/unit_price&gt;
              &lt;unit&gt;hour&lt;/unit&gt;
          &lt;/item&gt;
  &lt;/items&gt;
  </code></pre>
  
  <p>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p>
  
  <p><strong>One of the solutions is very simple actually. I have just added without getters and setters.</strong></p>
  
  <pre><code>   @XmlAttribute(name="type")
      String type ="array";
  </code></pre>
  
  <p><strong>And it gives me the output with the <code>&lt;items type="array"&gt;</code> and without the field <code>&lt;type&gt;array&lt;/type&gt;</code> like this:</strong></p>
  
  <pre><code>&lt;items type="array"&gt;
          &lt;item&gt;
              &lt;id&gt;1162842&lt;/id&gt;
              &lt;name&gt;tatat2&lt;/name&gt;
              &lt;description&gt;1&lt;/description&gt;
              &lt;unit_price&gt;1.0&lt;/unit_price&gt;
              &lt;unit&gt;hour&lt;/unit&gt;
          &lt;/item&gt;
          &lt;item&gt;
              &lt;id&gt;1163631&lt;/id&gt;
              &lt;name&gt;testItem2333&lt;/name&gt;
              &lt;description&gt;abc&lt;/description&gt;
              &lt;unit_price&gt;1.0&lt;/unit_price&gt;
              &lt;unit&gt;hour&lt;/unit&gt;
          &lt;/item&gt;
  &lt;/items&gt;
  </code></pre>
  
  <p>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p>
  
  <p>I have my classes like this:</p>
  
  <pre><code>//ITEMS++++++++++++++++++++++++++++++++++
  
      import com.fasterxml.jackson.annotation.JsonIgnore;
      import org.hibernate.annotations.Cache;
      import org.hibernate.annotations.CacheConcurrencyStrategy;
      import org.hibernate.annotations.Type;
      import org.hibernate.mapping.Array;
      import org.springframework.data.elasticsearch.annotations.Document;
  
      import javax.persistence.*;
      import javax.xml.bind.annotation.*;
      import java.io.Serializable;
      import java.util.HashSet;
      import java.util.Set;
      import java.util.Objects;
      import javax.persistence.*;
  
      @Entity
      @Table(name = "items")
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      @Document(indexName = "items")
      @XmlRootElement(name="items")
      @XmlAccessorType(XmlAccessType.FIELD)
      public class Items implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @XmlElement(name="item")
      private Item[] item;
  
      public Item[] getItem() {
          return item;
      }
  
      public void setItem(Item[] item) {
          this.item = item;
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @Column(name = "sum")
      private Double sum;
  
      @Column(name = "discount")
      private Double discount;
  
      @Column(name = "before_taxes")
      private Double before_taxes;
  
      @Column(name = "taxes")
      private Double taxes;
  
      @Column(name = "total")
      private Double total;
  
      @ManyToMany(mappedBy = "items")
      @JsonIgnore
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      private Set&lt;Invoice&gt; invoices = new HashSet&lt;&gt;();
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public Double getSum() {
          return sum;
      }
  
      public void setSum(Double sum) {
          this.sum = sum;
      }
  
      public Double getDiscount() {
          return discount;
      }
  
      public void setDiscount(Double discount) {
          this.discount = discount;
      }
  
      public Double getBefore_taxes() {
          return before_taxes;
      }
  
      public void setBefore_taxes(Double before_taxes) {
          this.before_taxes = before_taxes;
      }
  
      public Double getTaxes() {
          return taxes;
      }
  
      public void setTaxes(Double taxes) {
          this.taxes = taxes;
      }
  
      public Double getTotal() {
          return total;
      }
  
      public void setTotal(Double total) {
          this.total = total;
      }
  
      public Set&lt;Invoice&gt; getInvoices() {
          return invoices;
      }
  
      public void setInvoices(Set&lt;Invoice&gt; invoices) {
          this.invoices = invoices;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) {
              return true;
          }
          if (o == null || getClass() != o.getClass()) {
              return false;
          }
          Items items = (Items) o;
          if(items.id == null || id == null) {
              return false;
          }
          return Objects.equals(id, items.id);
      }
  
      @Override
      public int hashCode() {
          return Objects.hashCode(id);
      }
  
      @Override
      public String toString() {
          return "Items{" +
              "id=" + id +
              ", sum='" + sum + "'" +
              ", discount='" + discount + "'" +
              ", before_taxes='" + before_taxes + "'" +
              ", taxes='" + taxes + "'" +
              ", total='" + total + "'" +
              '}';
      }
  
  //ITEM++++++++++++++++++++++++++++++++++
  
      import com.fasterxml.jackson.annotation.JsonIgnore;
      import org.hibernate.annotations.Cache;
      import org.hibernate.annotations.CacheConcurrencyStrategy;
      import org.springframework.data.elasticsearch.annotations.Document;
  
      import javax.persistence.*;
      import javax.validation.constraints.*;
      import javax.xml.bind.annotation.XmlAccessType;
      import javax.xml.bind.annotation.XmlAccessorType;
      import javax.xml.bind.annotation.XmlRootElement;
      import java.io.Serializable;
      import java.util.HashSet;
      import java.util.Set;
      import java.util.Objects;
  
      /**
       * A Item.
       */
      @Entity
      @Table(name = "item")
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      @Document(indexName = "item")
      @XmlRootElement(name="item")
      @XmlAccessorType(XmlAccessType.FIELD)
      public class Item implements Serializable {
  
          private static final long serialVersionUID = 1L;
  
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          private Long id;
  
          @NotNull
          @Column(name = "name", nullable = false)
          private String name;
  
          @NotNull
          @Column(name = "description", nullable = false)
          private String description;
  
          @NotNull
          @DecimalMin(value = "0")
          @Column(name = "unit_price", nullable = false)
          private Double unit_price;
  
          @NotNull
          @Column(name = "unit", nullable = false)
          private String unit;
  
          @Column(name = "quantity")
          private Double quantity;
  
          @Column(name = "discount")
          private Double discount;
  
          @Column(name = "subtotal")
          private Double subtotal;
  
          @Column(name = "tax_amount")
          private Double tax_amount;
  
          @Column(name = "discount_amount")
          private Double discount_amount;
  
          @Column(name = "total")
          private Double total;
  
          @ManyToOne
          @NotNull
          private Tax tax;
  
          @ManyToMany(mappedBy = "items")
          @JsonIgnore
          @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
          private Set&lt;Invoice&gt; invoices = new HashSet&lt;&gt;();
  
          public Long getId() {
              return id;
          }
  
          public void setId(Long id) {
              this.id = id;
          }
  
          public String getName() {
              return name;
          }
  
          public void setName(String name) {
              this.name = name;
          }
  
          public String getDescription() {
              return description;
          }
  
          public void setDescription(String description) {
              this.description = description;
          }
  
          public Double getUnit_price() {
              return unit_price;
          }
  
          public void setUnit_price(Double unit_price) {
              this.unit_price = unit_price;
          }
  
          public String getUnit() {
              return unit;
          }
  
          public void setUnit(String unit) {
              this.unit = unit;
          }
  
          public Double getQuantity() {
              return quantity;
          }
  
          public void setQuantity(Double quantity) {
              this.quantity = quantity;
          }
  
          public Double getDiscount() {
              return discount;
          }
  
          public void setDiscount(Double discount) {
              this.discount = discount;
          }
  
          public Double getSubtotal() {
              return subtotal;
          }
  
          public void setSubtotal(Double subtotal) {
              this.subtotal = subtotal;
          }
  
          public Double getTax_amount() {
              return tax_amount;
          }
  
          public void setTax_amount(Double tax_amount) {
              this.tax_amount = tax_amount;
          }
  
          public Double getDiscount_amount() {
              return discount_amount;
          }
  
          public void setDiscount_amount(Double discount_amount) {
              this.discount_amount = discount_amount;
          }
  
          public Double getTotal() {
              return total;
          }
  
          public void setTotal(Double total) {
              this.total = total;
          }
  
          public Tax getTax() {
              return tax;
          }
  
          public void setTax(Tax tax) {
              this.tax = tax;
          }
  
          public Set&lt;Invoice&gt; getInvoices() {
              return invoices;
          }
  
          public void setInvoices(Set&lt;Invoice&gt; invoices) {
              this.invoices = invoices;
          }
  
          @Override
          public boolean equals(Object o) {
              if (this == o) {
                  return true;
              }
              if (o == null || getClass() != o.getClass()) {
                  return false;
              }
              Item item = (Item) o;
              if(item.id == null || id == null) {
                  return false;
              }
              return Objects.equals(id, item.id);
          }
  
          @Override
          public int hashCode() {
              return Objects.hashCode(id);
          }
  
          @Override
          public String toString() {
              return "Item{" +
                  "id=" + id +
                  ", name='" + name + "'" +
                  ", description='" + description + "'" +
                  ", unit_price='" + unit_price + "'" +
                  ", unit='" + unit + "'" +
                  ", quantity='" + quantity + "'" +
                  ", discount='" + discount + "'" +
                  ", subtotal='" + subtotal + "'" +
                  ", tax_amount='" + tax_amount + "'" +
                  ", discount_amount='" + discount_amount + "'" +
                  ", total='" + total + "'" +
                  '}';
          }
      }
  
  //And the code to convert the items+++++++++++++++++++++++ 
  
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                  conn.setDoOutput( true );
                  conn.setRequestMethod(type);
                  conn.setRequestProperty("Content-Type", "application/xml");
                  conn.setRequestProperty("Accept", "application/xml");
                  conn.setRequestProperty("charset" , "utf-8");
  
                  JAXBContext jc = JAXBContext.newInstance(Invoice.class);
                  Marshaller jaxbMarshaller = jc.createMarshaller();
                  jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
      //            jaxbMarshaller.marshal(invoiceRec, System.out);
  
                  //TODO get of ITEM SET (DONE)-&gt; -&gt; Item[] item (DONE)-&gt;Items.setItem(item) (DONE) -&gt; Return ITEMS to invoiceRec (DONE) -&gt; Remove items -&gt; !!Item need to hide TAX!!(it gets automatically)
                  Boolean turOnTests = false;
                  Item[] arrayITEM = new Item[invoiceRec.getItem().size()+1];
                  int i =0;
                  for (Iterator&lt;Item&gt; it = invoiceRec.getItem().iterator(); it.hasNext(); ) {
                      Item f = it.next();
                      arrayITEM[i] = f; //GET ITEM into arrayITEM[]
                      arrayITEM[i].setTax(null); // !!SET TAX NULL!!
                      i++;
                      it.remove();
                  }
  
                  if(turOnTests)
                  for(int y=0; y!= arrayITEM.length;y++){
                      System.out.println("TEST2: TOTAL arrayITEM[]: " + arrayITEM[y]);
                  }
  
                  Items totalItems = new Items();
                  totalItems.setItem(arrayITEM);
  
                  Set&lt;Items&gt; invoiceReturn = new HashSet&lt;Items&gt;(); //Set á Items
                  invoiceReturn.add(totalItems);
  
                  if(turOnTests)
                  for (Iterator&lt;Items&gt; it = invoiceReturn.iterator(); it.hasNext(); ) {
                      Items f = it.next();
                      System.out.println("TEST3: Item on the invoiceReturn Items: " + f.getItem().length );
                  }
  
                  invoiceRec.setItems(invoiceReturn); //Return Items to invoiceRec
      //___________________________________________________________________________________________________
  
                  jaxbMarshaller.marshal(invoiceRec, System.out);
                  jaxbMarshaller.marshal(invoiceRec, conn.getOutputStream());
  </code></pre>