I have made a web service in Netbeans. I am trying to understand why the methods of the bean does not get generated by the client.
Here is the bean.
package OnlineAuction;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
public class Auction implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Product product;
private Timestamp added;
private Timestamp expire;
private BigDecimal startingBid;
private BigDecimal reserve;
/**
*
* #return
*/
public Long getId() {
return id;
}
/**
*
* #param id
*/
public void setId(Long id) {
this.id = id;
}
#Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Auction)) {
return false;
}
Auction other = (Auction) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
#Override
public String toString() {
return "OnlineAuction.Auction[ id=" + id + " ]";
}
/**
*
* #return
*/
public Product getProduct() {
return product;
}
/**
*
* #param product
*/
public void setProduct(Product product) {
this.product = product;
}
/**
*
* #return
*/
public Timestamp getAdded() {
return added;
}
/**
*
* #param added
*/
public void setAdded(Timestamp added) {
this.added = added;
}
/**
*
*/
public void setAddedNow() {
this.added = getTimestampNow();
}
private Timestamp getTimestampNow() {
java.util.Date date = new java.util.Date();
return new Timestamp(date.getTime());
}
/**
*
* #return
*/
public Timestamp getExpire() {
return expire;
}
/**
*
* #param expire
*/
public void setExpire(Timestamp expire) {
this.expire = expire;
}
/**
* Sets the expiry time of the auction by adding the hours to the added date.
* If the added date is not set this function will set the added date to current time.
* #param hours to be added to the added time of the auction
*/
public void setExpire(int hours) {
// Set added time if null
if (this.added == null) {
this.setAddedNow();
}
Calendar cal = Calendar.getInstance();
cal.setTime(this.added);
cal.add(Calendar.HOUR, hours);
Date date = cal.getTime();
this.expire = new Timestamp(date.getTime());
}
/**
*
* #return
*/
public BigDecimal getStartingBid() {
return startingBid;
}
/**
*
* #param startingBid
*/
public void setStartingBid(BigDecimal startingBid) {
this.startingBid = startingBid;
}
/**
*
* #return
*/
public BigDecimal getReserve() {
return reserve;
}
/**
*
* #param reserve
*/
public void setReserve(BigDecimal reserve) {
this.reserve = reserve;
}
public boolean isAuctionExpired() {
boolean expired;
if (this.getTimestampNow().compareTo(this.expire) > 0) {
// now is greater than expire date
expired = true;
} else {
// now is less than expire date
expired = false;
}
return expired;
}
}
And here is the generated source by JAX-WS for the client.
package OnlineAuction.client;
import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for auction complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="auction">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="added" type="{http://OnlineAuction/}timestamp" minOccurs="0"/>
* <element name="expire" type="{http://OnlineAuction/}timestamp" minOccurs="0"/>
* <element name="id" type="{http://www.w3.org/2001/XMLSchema}long" minOccurs="0"/>
* <element name="product" type="{http://OnlineAuction/}product" minOccurs="0"/>
* <element name="reserve" type="{http://www.w3.org/2001/XMLSchema}decimal" minOccurs="0"/>
* <element name="startingBid" type="{http://www.w3.org/2001/XMLSchema}decimal" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "auction", propOrder = {
"added",
"expire",
"id",
"product",
"reserve",
"startingBid"
})
public class Auction {
protected Timestamp added;
protected Timestamp expire;
protected Long id;
protected Product product;
protected BigDecimal reserve;
protected BigDecimal startingBid;
/**
* Gets the value of the added property.
*
* #return
* possible object is
* {#link Timestamp }
*
*/
public Timestamp getAdded() {
return added;
}
/**
* Sets the value of the added property.
*
* #param value
* allowed object is
* {#link Timestamp }
*
*/
public void setAdded(Timestamp value) {
this.added = value;
}
/**
* Gets the value of the expire property.
*
* #return
* possible object is
* {#link Timestamp }
*
*/
public Timestamp getExpire() {
return expire;
}
/**
* Sets the value of the expire property.
*
* #param value
* allowed object is
* {#link Timestamp }
*
*/
public void setExpire(Timestamp value) {
this.expire = value;
}
/**
* Gets the value of the id property.
*
* #return
* possible object is
* {#link Long }
*
*/
public Long getId() {
return id;
}
/**
* Sets the value of the id property.
*
* #param value
* allowed object is
* {#link Long }
*
*/
public void setId(Long value) {
this.id = value;
}
/**
* Gets the value of the product property.
*
* #return
* possible object is
* {#link Product }
*
*/
public Product getProduct() {
return product;
}
/**
* Sets the value of the product property.
*
* #param value
* allowed object is
* {#link Product }
*
*/
public void setProduct(Product value) {
this.product = value;
}
/**
* Gets the value of the reserve property.
*
* #return
* possible object is
* {#link BigDecimal }
*
*/
public BigDecimal getReserve() {
return reserve;
}
/**
* Sets the value of the reserve property.
*
* #param value
* allowed object is
* {#link BigDecimal }
*
*/
public void setReserve(BigDecimal value) {
this.reserve = value;
}
/**
* Gets the value of the startingBid property.
*
* #return
* possible object is
* {#link BigDecimal }
*
*/
public BigDecimal getStartingBid() {
return startingBid;
}
/**
* Sets the value of the startingBid property.
*
* #param value
* allowed object is
* {#link BigDecimal }
*
*/
public void setStartingBid(BigDecimal value) {
this.startingBid = value;
}
}
So why is my bean method isAuctionExpired() not in the generated source? I am able to cheat this by adding a auctionExpired property, but this is not how I want to do this.
The JAX-WS client is generated using the WSDL of the webservice. The WSDL only has the properties of the types that will be used for communication between the server and the client. So, you will never be able to pass the method implementation across server and client. The information in the below annotation is all the client class generator has while generating the client;
#XmlType(name = "auction", propOrder = {
"added",
"expire",
"id",
"product",
"reserve",
"startingBid"
})
This information is taken from the WSDL.
You have to understand that XML is used for communication between the server and the client. So, code from the webservice cannot be passed to the client or vice versa. The webservice and the client can be written in two different technologies. So, passing java code to the client does not make any sense if the client is implemented in any other language.
In short, you can pass information between the client and server but not behaviour.
Related
maybe my question are similiar like these question How do I parse this XML in Java with JAXB?
i want to generate some XML files to display some data
but i have different problem with using some #XmlAttribute & #XmlValue properly in JAXBthis is XML output supposed to be:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AnlKerja>
<Analisa no="1">
<Kode>B.1</Kode>
<Uraian>some description</Uraian>
<Material>
<item type="value">ID|ID|ID</item>
</Material>
<Jumlah>some value</Jumlah>
</Analisa>
<Analisa no="2">
<Kode>B.3</Kode>
<Uraian>some description</Uraian>
<Material>
<item type="value">ID|ID|ID</item>
<item type="value">ID|ID|ID</item>
</Material>
<Jumlah>some value</Jumlah>
</Analisa>
</AnlKerja>
since i'm using JAXB 2 days ago
i dont know how to use it properly,
and its kinda hard for me to read the JAXB documentation guide
because my native language isn't English.
this is my java code for JAXB
AnlKerja.java
#XmlRootElement(name = "AnlKerja")
public class AnlKerja {
#XmlElementWrapper(name = "Analisa")
#XmlElement(name = "Analisa")
private ArrayList<Analisa> analisa;
public ArrayList<Analisa> getAnl() {
return analisa;
}
public void setAnl(ArrayList<Analisa> anl) {
this.analisa = anl;
}
}
Analisa.java
#XmlRootElement(name="Analisa")
#XmlType(propOrder = {"no","value","kode","uraian","material","jumlah"})
public class Analisa {
#XmlAttribute
private String no;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
#XmlValue
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
private String kode,uraian;
private double jumlah;
#XmlElement(name="Kode")
public String getKode() {
return kode;
}
public void setKode(String kode) {
this.kode = kode;
}
#XmlElement(name="Uraian")
public String getUraian() {
return uraian;
}
public void setUraian(String uraian) {
this.uraian = uraian;
}
#XmlElementWrapper(name = "material")
private ArrayList<Material> material;
public ArrayList<Material> getMaterial() {
return material;
}
public void setMaterial(ArrayList<Material> material) {
this.material = material;
}
#XmlElement(name="Jumlah")
public double getJumlah() {
return jumlah;
}
public void setJumlah(double jumlah) {
this.jumlah = jumlah;
}
}
Material.java
#XmlRootElement(name = "Material")
public class Material {
#XmlElement(name = "items")
private String items;
public Material() {
this.items = "";
this.tipe = "";
this.value = "";
}
public Material(String[] isi, String tipe, String deskripsi) {
int temp = isi.length;
for (int x=0; x<temp; x++) {
this.items += isi[x]+"|";
};
this.value = deskripsi;
this.tipe = tipe;
}
public String getItems() {
return items;
}
public void setItems(String items) {
this.items = items;
}
public void setItems(String[] items) {
int temp = items.length;
for (int x=0; x<temp; x++) {
this.items += items[x]+"|";
}
}
#XmlAttribute
private String tipe;
public String getTipe() {
return tipe;
}
public void setTipe(String tipe) {
this.tipe = tipe;
}
#XmlValue
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Demo.java
public class Demo {
private static final String STORE_XML = "results/ANL.xml";
public static void main(String[] args) throws JAXBException, IOException{
ArrayList<Material> mtr = new ArrayList<Material>();
ArrayList<Analisa> anl = new ArrayList<Analisa>();
AnlKerja anKerja = new AnlKerja();
Material mat = new Material();
mat.setTipe("tipe");
mat.setValue("Bahan");
mat.setItems("MT001|MT002|MT003");
mtr.add(mat);
mat.setTipe("tipe");
mat.setValue("Tenaga");
mat.setItems("MT0001|MT0002|MT0003");
mtr.add(mat);
Analisa ana = new Analisa();
ana.setNo("no");
ana.setValue(1);
ana.setKode("B.1");
ana.setUraian("some description");
ana.setMaterial(mtr);
ana.setJumlah(122414.03);
anl.add(ana);
anKerja.setAnl(anl);
JAXBContext jCont = JAXBContext.newInstance(AnlKerja.class);
Marshaller marshal = jCont.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshal.marshal(anKerja, System.out);
marshal.marshal(anKerja, new File(STORE_XML));
}
}
this is the error i get
Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 8 counts of IllegalAnnotationExceptions
If a class has #XmlElement property, it cannot have #XmlValue property.
this problem is related to the following location:
at private int XML.Analisa.value
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at public int XML.Analisa.getValue()
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
If a class has #XmlElement property, it cannot have #XmlValue property.
this problem is related to the following location:
at private java.lang.String XML.Material.value
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at public java.lang.String XML.Material.getValue()
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "material"
this problem is related to the following location:
at public java.util.ArrayList XML.Analisa.getMaterial()
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "no"
this problem is related to the following location:
at public java.lang.String XML.Analisa.getNo()
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.lang.String XML.Analisa.no
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "value"
this problem is related to the following location:
at public int XML.Analisa.getValue()
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private int XML.Analisa.value
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "items"
this problem is related to the following location:
at public java.lang.String XML.Material.getItems()
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.lang.String XML.Material.items
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "tipe"
this problem is related to the following location:
at public java.lang.String XML.Material.getTipe()
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.lang.String XML.Material.tipe
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "value"
this problem is related to the following location:
at public java.lang.String XML.Material.getValue()
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.lang.String XML.Material.value
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
This is XML, not XML schema
you have some errors in your XML file: tag item is not closed on some lines.
After fixing this you can create XSD from XML on one of online services, for example: https://www.freeformatter.com/xsd-generator.html
With your XSD file you can create JAXB binding classes with xjc utility (https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html) from JDK
upd the generated class:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See http://java.sun.com/xml/jaxb
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2019.05.05 at 08:59:07 PM MSK
//
package generated;
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.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Analisa" maxOccurs="unbounded" minOccurs="0">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Kode" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Uraian" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Material">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="item" maxOccurs="unbounded" minOccurs="0">
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* <element name="Jumlah" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* <attribute name="no" type="{http://www.w3.org/2001/XMLSchema}byte" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"analisa"
})
#XmlRootElement(name = "AnlKerja")
public class AnlKerja {
#XmlElement(name = "Analisa")
protected List<AnlKerja.Analisa> analisa;
/**
* Gets the value of the analisa property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the analisa property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getAnalisa().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {#link AnlKerja.Analisa }
*
*
*/
public List<AnlKerja.Analisa> getAnalisa() {
if (analisa == null) {
analisa = new ArrayList<AnlKerja.Analisa>();
}
return this.analisa;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Kode" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Uraian" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Material">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="item" maxOccurs="unbounded" minOccurs="0">
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* <element name="Jumlah" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* <attribute name="no" type="{http://www.w3.org/2001/XMLSchema}byte" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"kode",
"uraian",
"material",
"jumlah"
})
public static class Analisa {
#XmlElement(name = "Kode", required = true)
protected String kode;
#XmlElement(name = "Uraian", required = true)
protected String uraian;
#XmlElement(name = "Material", required = true)
protected AnlKerja.Analisa.Material material;
#XmlElement(name = "Jumlah", required = true)
protected String jumlah;
#XmlAttribute(name = "no")
protected Byte no;
/**
* Gets the value of the kode property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getKode() {
return kode;
}
/**
* Sets the value of the kode property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setKode(String value) {
this.kode = value;
}
/**
* Gets the value of the uraian property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getUraian() {
return uraian;
}
/**
* Sets the value of the uraian property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setUraian(String value) {
this.uraian = value;
}
/**
* Gets the value of the material property.
*
* #return
* possible object is
* {#link AnlKerja.Analisa.Material }
*
*/
public AnlKerja.Analisa.Material getMaterial() {
return material;
}
/**
* Sets the value of the material property.
*
* #param value
* allowed object is
* {#link AnlKerja.Analisa.Material }
*
*/
public void setMaterial(AnlKerja.Analisa.Material value) {
this.material = value;
}
/**
* Gets the value of the jumlah property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getJumlah() {
return jumlah;
}
/**
* Sets the value of the jumlah property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setJumlah(String value) {
this.jumlah = value;
}
/**
* Gets the value of the no property.
*
* #return
* possible object is
* {#link Byte }
*
*/
public Byte getNo() {
return no;
}
/**
* Sets the value of the no property.
*
* #param value
* allowed object is
* {#link Byte }
*
*/
public void setNo(Byte value) {
this.no = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="item" maxOccurs="unbounded" minOccurs="0">
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"item"
})
public static class Material {
protected List<AnlKerja.Analisa.Material.Item> item;
/**
* Gets the value of the item property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the item property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getItem().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {#link AnlKerja.Analisa.Material.Item }
*
*
*/
public List<AnlKerja.Analisa.Material.Item> getItem() {
if (item == null) {
item = new ArrayList<AnlKerja.Analisa.Material.Item>();
}
return this.item;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"value"
})
public static class Item {
#XmlValue
protected String value;
#XmlAttribute(name = "type")
protected String type;
/**
* Gets the value of the value property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getValue() {
return value;
}
/**
* Sets the value of the value property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setValue(String value) {
this.value = value;
}
/**
* Gets the value of the type property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getType() {
return type;
}
/**
* Sets the value of the type property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setType(String value) {
this.type = value;
}
}
}
}
}
i found some solution but it still didnt solve the real problem that i facing now.
i found this tutorial HERE
and i change little bit of my codeAnlKerja.java
#XmlRootElement
public class AnlKerja {
private ArrayList<Analisa> analisa;
// removed #XmlElementWrapper & #XmlElement
public ArrayList<Analisa> getAnalisa() {
return analisa;
}
public void setAnalisa(ArrayList<Analisa> anl) {
this.analisa = anl;
}
}
Analisa.java
placing variable in the right order & remove #XmlElement
private String kode,uraian;
private ArrayList<Material> material;
private double jumlah;
private String no;
removed #XmlValue
and re-arrange #XmlAttribute to bottom line code
#XmlAttribute
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
Material.java i did same to material, remove #XmlValue and its variable
#XmlAttribute
public String getTipe() {
return tipe;
}
public void setTipe(String tipe) {
this.tipe = tipe;
}
and the rest i remove ana.setValue(1); from Demo.java
and this are the result i get for my XML files.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<anlKerja>
<analisa no="1">
<kode>B.1</kode>
<uraian>Some Description</uraian>
<material tipe="Tenaga">
<items>MT001|MT002|MT003</items>
</material>
<material tipe="Bahan">
<items>MT0001|MT0002|MT0003</items>
</material>
<jumlah>122414.03</jumlah>
</analisa>
</anlKerja>
its almost look like what i wanted but still there is some child element that i wanted to change
<material tipe="Bahan">
<items>MT0001|MT0002|MT0003</items>
</material>
to this
<Material>
<item type="Bahan">ID|ID|ID</item>
</Material>
Or this
<Material>
<item type="Bahan">ID|ID|ID</item>
<item type="Tenaga">ID|ID|ID</item>
</Material>
should i create additional class for item?
or tweak some code in Material.java ?
I am using JAXBContext for converting XML to Class.
but I had some problem.
1) XML Like this.
<Baseball namespace="Sport">
<League namespace="MLB">
<Description>Baseball league of America</Description>
<Team>
<Name>Yankees</Name>
<City>Newyork</City>
</Team>
<Team>
<Name>Dodgers</Name>
<City>LA</City>
</Team>
</League>
</Baseball>
2) Class like this.
2-1) Baseball.class
#XmlRootElement(name = "Baseball ", namespace = "Sport")
#XmlAccessorType(XmlAccessType.FIELD)
private class Baseball
{
#XmlElement(name = "League", namespace = "MLB")
public League league;
}
2-2) League.class
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
private class League
{
#XmlElement(name = "Description")
public String description;
#XmlElement(name = "Team")
public Team[] teams;
}
2-3) Team.class
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
private class Team
{
#XmlElement(name = "Name")
public String name;
#XmlElement(name = "City")
public String city;
}
Baseball and league was converted to class successfully but team is not converted to class.
Also value of description is null.
what's the problem in this code?
Your xml is invalid.
The element "Description" is terminated by a start-tag instead of an end-tag.
In addition, "League" is missing an end-tag.
Here's a valid version of your xml:
<Baseball namespace="Sport">
<League namespace="MLB">
<Description>Baseball league of America</Description>
<Team>
<Name>Yankees</Name>
<City>Newyork</City>
</Team>
<Team>
<Name>Dodgers</Name>
<City>LA</City>
</Team>
</League>
</Baseball>
I generated an XSD file from your (corrected) example XML, using this link, and used xjc to generate classes from it.
I attach the result, so you can compare it to your classes for differences.
Baseball.java:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See http://java.sun.com/xml/jaxb
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2018.10.15 at 12:07:59 PM IDT
//
package generated;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element ref="{}League"/>
* </sequence>
* <attribute name="namespace" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"league"
})
#XmlRootElement(name = "Baseball")
public class Baseball {
#XmlElement(name = "League", required = true)
protected League league;
#XmlAttribute(name = "namespace")
protected String namespace;
/**
* Gets the value of the league property.
*
* #return
* possible object is
* {#link League }
*
*/
public League getLeague() {
return league;
}
/**
* Sets the value of the league property.
*
* #param value
* allowed object is
* {#link League }
*
*/
public void setLeague(League value) {
this.league = value;
}
/**
* Gets the value of the namespace property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getNamespace() {
return namespace;
}
/**
* Sets the value of the namespace property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setNamespace(String value) {
this.namespace = value;
}
}
League.java:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See http://java.sun.com/xml/jaxb
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2018.10.15 at 12:07:59 PM IDT
//
package generated;
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.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element ref="{}Description"/>
* <element ref="{}Team" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* <attribute name="namespace" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"description",
"team"
})
#XmlRootElement(name = "League")
public class League {
#XmlElement(name = "Description", required = true)
protected String description;
#XmlElement(name = "Team")
protected List<Team> team;
#XmlAttribute(name = "namespace")
protected String namespace;
/**
* Gets the value of the description property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getDescription() {
return description;
}
/**
* Sets the value of the description property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setDescription(String value) {
this.description = value;
}
/**
* Gets the value of the team property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the team property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getTeam().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {#link Team }
*
*
*/
public List<Team> getTeam() {
if (team == null) {
team = new ArrayList<Team>();
}
return this.team;
}
/**
* Gets the value of the namespace property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getNamespace() {
return namespace;
}
/**
* Sets the value of the namespace property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setNamespace(String value) {
this.namespace = value;
}
}
Team.java:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See http://java.sun.com/xml/jaxb
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2018.10.15 at 12:07:59 PM IDT
//
package generated;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element ref="{}Name"/>
* <element ref="{}City"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"name",
"city"
})
#XmlRootElement(name = "Team")
public class Team {
#XmlElement(name = "Name", required = true)
protected String name;
#XmlElement(name = "City", required = true)
protected String city;
/**
* Gets the value of the name property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the city property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getCity() {
return city;
}
/**
* Sets the value of the city property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setCity(String value) {
this.city = value;
}
}
ObjectFactory.java:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See http://java.sun.com/xml/jaxb
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2018.10.15 at 12:07:59 PM IDT
//
package generated;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the generated package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
#XmlRegistry
public class ObjectFactory {
private final static QName _Description_QNAME = new QName("", "Description");
private final static QName _City_QNAME = new QName("", "City");
private final static QName _Name_QNAME = new QName("", "Name");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {#link League }
*
*/
public League createLeague() {
return new League();
}
/**
* Create an instance of {#link Team }
*
*/
public Team createTeam() {
return new Team();
}
/**
* Create an instance of {#link Baseball }
*
*/
public Baseball createBaseball() {
return new Baseball();
}
/**
* Create an instance of {#link JAXBElement }{#code <}{#link String }{#code >}}
*
*/
#XmlElementDecl(namespace = "", name = "Description")
public JAXBElement<String> createDescription(String value) {
return new JAXBElement<String>(_Description_QNAME, String.class, null, value);
}
/**
* Create an instance of {#link JAXBElement }{#code <}{#link String }{#code >}}
*
*/
#XmlElementDecl(namespace = "", name = "City")
public JAXBElement<String> createCity(String value) {
return new JAXBElement<String>(_City_QNAME, String.class, null, value);
}
/**
* Create an instance of {#link JAXBElement }{#code <}{#link String }{#code >}}
*
*/
#XmlElementDecl(namespace = "", name = "Name")
public JAXBElement<String> createName(String value) {
return new JAXBElement<String>(_Name_QNAME, String.class, null, value);
}
}
And the generated JAXB helper class:
package itemOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"title",
"author",
"publisher",
"description",
"price",
"publicationYear"
})
#XmlRootElement(name = "book")
public class Book {
#XmlElement(name = "Title", required = true)
protected String title;
#XmlElement(name = "Author", required = true)
protected String author;
#XmlElement(name = "Publisher", required = true)
protected String publisher;
#XmlElement(name = "Description", required = true)
protected String description;
#XmlElement(name = "Price")
protected float price;
#XmlElement(name = "PublicationYear")
protected int publicationYear;
#XmlAttribute(name = "ISBN")
protected Integer isbn;
/**
* Gets the value of the title property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getTitle() {
return title;
}
/**
* Sets the value of the title property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setTitle(String value) {
this.title = value;
}
/**
* Gets the value of the author property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getAuthor() {
return author;
}
/**
* Sets the value of the author property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setAuthor(String value) {
this.author = value;
}
/**
* Gets the value of the publisher property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getPublisher() {
return publisher;
}
/**
* Sets the value of the publisher property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setPublisher(String value) {
this.publisher = value;
}
/**
* Gets the value of the description property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getDescription() {
return description;
}
/**
* Sets the value of the description property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setDescription(String value) {
this.description = value;
}
/**
* Gets the value of the price property.
*
*/
public float getPrice() {
return price;
}
/**
* Sets the value of the price property.
*
*/
public void setPrice(float value) {
this.price = value;
}
/**
* Gets the value of the publicationYear property.
*
*/
public int getPublicationYear() {
return publicationYear;
}
/**
* Sets the value of the publicationYear property.
*
*/
public void setPublicationYear(int value) {
this.publicationYear = value;
}
/**
* Gets the value of the isbn property.
*
* #return
* possible object is
* {#link Integer }
*
*/
public Integer getISBN() {
return isbn;
}
/**
* Sets the value of the isbn property.
*
* #param value
* allowed object is
* {#link Integer }
*
*/
public void setISBN(Integer value) {
this.isbn = value;
}
}
And in the Main, using the XML template, I am setting data for the defined elements:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
itemOrder.Book quickXML = new itemOrder.Book();
quickXML.setAuthor("Lev Tolstoi");
quickXML.setDescription("Russion fixction about 1st world war");
quickXML.setISBN(62129985);
quickXML.setPrice((float)12.6);
quickXML.setPublisher("Progress");
quickXML.setTitle("War and Peace");
try {
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(quickXML.getClass().getPackage().getName());
javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
OutputStream os = new FileOutputStream("xmlFile.xml");
marshaller.marshal(quickXML, os);
} catch (javax.xml.bind.JAXBException ex) {
// XXXTODO Handle exception
java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
and then marshalling them into a file. The question is, how can I marshal multiple entries of the data into ONE file. So for example: I would like to marshal this:
quickXML.setAuthor("Lev Tolstoi");
quickXML.setDescription("Russion fixction about 1st world war");
quickXML.setISBN(62129985);
quickXML.setPrice((float)12.6);
quickXML.setPublisher("Progress");
quickXML.setTitle("War and Peace");
and this:
quickXML.setAuthor("Robert Schwentke");
quickXML.setDescription("description description description");
quickXML.setISBN(62129432);
quickXML.setPrice((float)10.9);
quickXML.setPublisher("Regress");
quickXML.setTitle("Red");
at the same time, into the same file. Any ideas?
You can wrap 'book' elements with a 'books' tag
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/books"
xmlns:tns="http://xml.netbeans.org/schema/books"
elementFormDefault="qualified">
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book">
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
<xsd:element name="Description" type="xsd:string"/>
<xsd:element name="Price" type="xsd:float"/>
<xsd:element name="PublicationYear" type="xsd:int"/>
<xsd:element name="book">
</xsd:sequence>
<xsd:attribute name="ISBN" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
I have this problem that i can't solve, this is my entity class:
/**
* #hibernate.class
* table="users.network_topic"
* #hibernate.cache usage="read-write"
*/
public class NetworkTopic implements Serializable, Idable{
/** identifier field */
private Long id;
/** persistent field */
private Long networkId;
/** persistent field */
private Long topicId;
private UserTopic topic;
private Network network;
/**
* #hibernate.id
* generator-class="assigned"
* type="java.lang.Long"
* column="id"
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
* #hibernate.property
* type="java.lang.Long"
* column="network_id"
* not-null="true"
*/
public Long getNetworkId() {
return networkId;
}
public void setNetworkId(Long networkId) {
this.networkId = networkId;
}
/**
* #hibernate.property
* type="java.lang.Long"
* column="user_topic_id"
* not-null="true"
*/
public Long getTopicId() {
return topicId;
}
public void setTopicId(Long topicId) {
this.topicId = topicId;
}
/**
* #hibernate.set
* lazy="true"
* inverse="true"
* cascade="none"
* #hibernate.key
* column="user_topic_id"
* #hibernate.one-to-many
* class="com.netblue.matchpoint.domain.UserTopic"
*
*/
public UserTopic getTopic() {
return topic;
}
public void setTopic(UserTopic topic) {
this.topic = topic;
}
/**
* #hibernate.set
* lazy="true"
* inverse="true"
* cascade="none"
* #hibernate.key
* column="network_id"
* #hibernate.one-to-many
* class="com.netblue.matchpoint.domain.Network"
*
*/
public Network getNetwork() {
return network;
}
public void setNetwork(Network network) {
this.network = network;
}
#Override
public String toString() {
return "Network[id=" + id + "]";
}
}
Then i try to save a new register with this code:
UserTopic topic = new UserTopic();
topic.setId(topicId);
topic.setName(ParseUtil.getString(map.get(MPConstants.TOPIC_NAME_PARAM)));
topic.setCreatedBy(ParseUtil.getInt(map.get(MPConstants.USER_ID)));
Date now = new Date();
topic.setCreatedDt(now);
topic.setLastUpdatedDt(now);
topic.setStatusId(Status.ACTIVE);
topic.setActivityCnt(0);
userTopicDao.saveOrUpdate(topic);
UserTopicMap utm = new UserTopicMap();
utm.setId(topicId);
utm.setMtid(topicId);
utm.setOtid(topicId);
utm.setIsDeleted(false);
utm.setLastUpdatedDt(now);
userTopicMapDao.saveOrUpdate(utm);
Network network = MpAuctionUtil.getNetworkById(networkId);
NetworkTopic networkTopic = new NetworkTopic();
networkTopic.setNetworkId(networkId);
networkTopic.setTopicId(topicId);
networkTopic.setNetwork(network);
networkTopic.setTopic(topic);
LOG.debug("userTopicService addObjSubscriber x networkId="+networkId+", topicId="+topicId);
networkTopicDao.saveOrUpdate(networkTopic);
The last lines are the ones that fail, if i remove them it works, i mean it saves topics ok, but when i try to do this it fail, i don't know if i'm doing something wrong, please help me !!
The NetworkTopic class hasnt been assigned an id. you must use the setId method or consider consider one of the other id strategies
2.2.3.1. Generating the identifier property
NetworkTopic networkTopic = new NetworkTopic();
networkTopic.setNetworkId(networkId);
networkTopic.setTopicId(topicId);
networkTopic.setNetwork(network);
networkTopic.setTopic(topic);
LOG.debug("userTopicService addObjSubscriber x networkId="+networkId+", topicId="+topicId);
networkTopicDao.saveOrUpdate(networkTopic);
Here,you must call the method setId, and set the value manually, because your id generator is "assigned", and that means you must assigned manually every time before you insert or update your data!
I have some problem when unmarshaling a .xml at a web service using JAXB.
This is the .xml file being sent from some client to the web service.
<?xml version="1.0" encoding="UTF-8"?>
<PERSON xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:/D:/MyWorkSpace/JAVA%20WEB%20Services%20DEVELOPER/XML%20Workspace/Persons.xsd">
<NAME>Michael</NAME>
<AGE>12</AGE>
<ADRESS>
<STREET>Somewhere in Spain</STREET>
<ZIP>47015</ZIP>
</ADRESS>
<HOBY indoorHoby="true"/>
</PERSON>
This is the method that does the unmarshaling
#POST
#Path("/XMLArrivalBeacon")
#Consumes(MediaType.APPLICATION_XML)
public Response methodI(String content) {
System.out.print(content);
try {
//Unmarshaling
JAXBContext context = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
//Unmarshal the String
Person person = (Person) unmarshaller.unmarshal(new StreamSource(new StringReader(content)));
//Checking that the values were recieved ok
System.out.print(person.getName());
return Response.ok("XML recieved from client!!!").build();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Here is the person class annotated with the JAXB annotations(Was automatically generated from the Schema)
package bindedschemas;
import java.math.BigInteger;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="age">
* <simpleType>
* <restriction base="{http://www.w3.org/2001/XMLSchema}integer">
* <minInclusive value="18"/>
* </restriction>
* </simpleType>
* </element>
* <element name="address">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="street" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="zip" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* <attribute name="country" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* <element name="hobie">
* <simpleType>
* <restriction base="{http://www.w3.org/2001/XMLSchema}string">
* </restriction>
* </simpleType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"name",
"age",
"address",
"hobie"
})
#XmlRootElement(name = "Person")
public class Person {
#XmlElement(required = true)
protected String name;
#XmlElement(required = true)
protected BigInteger age;
#XmlElement(required = true)
protected Person.Address address;
#XmlElement(required = true)
protected String hobie;
/**
* Gets the value of the name property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the age property.
*
* #return
* possible object is
* {#link BigInteger }
*
*/
public BigInteger getAge() {
return age;
}
/**
* Sets the value of the age property.
*
* #param value
* allowed object is
* {#link BigInteger }
*
*/
public void setAge(BigInteger value) {
this.age = value;
}
/**
* Gets the value of the address property.
*
* #return
* possible object is
* {#link Person.Address }
*
*/
public Person.Address getAddress() {
return address;
}
/**
* Sets the value of the address property.
*
* #param value
* allowed object is
* {#link Person.Address }
*
*/
public void setAddress(Person.Address value) {
this.address = value;
}
/**
* Gets the value of the hobie property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getHobie() {
return hobie;
}
/**
* Sets the value of the hobie property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setHobie(String value) {
this.hobie = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="street" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="zip" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* <attribute name="country" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"street",
"zip"
})
public static class Address {
#XmlElement(required = true)
protected String street;
#XmlElement(required = true)
protected String zip;
#XmlAttribute(name = "country")
protected String country;
/**
* Gets the value of the street property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getStreet() {
return street;
}
/**
* Sets the value of the street property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setStreet(String value) {
this.street = value;
}
/**
* Gets the value of the zip property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getZip() {
return zip;
}
/**
* Sets the value of the zip property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setZip(String value) {
this.zip = value;
}
/**
* Gets the value of the country property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getCountry() {
return country;
}
/**
* Sets the value of the country property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setCountry(String value) {
this.country = value;
}
}
}
And this is the exception i get
SEVERE: javax.xml.bind.UnmarshalException: unexpected element (uri:"",
local:"PERSON"). Expected elements are <{}Person> at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)
at
com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
at
com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
Any idea how can i fix it?
Your xml does not match your schema. xml element names are case sensitive. "PERSON" and "Person" are not the same element.