I am trying to use mkyong's JAXB Hello World example and am unable to print the contents of the entire class, although I am able to print specific elements. The output should be:
Customer [name=mkyong, age=29, id=100]
100
29
Here's my output:
xml_test.Customer@eed1f14
100
29
I am currently running Java 1.8 via Netbeans 8.2 on a Windows 10 box. Note that there are some other includes in here because my next step is to work with a list of the customer objects (that may be the subject of another question).
Here's my object (which I believe to be the same as his):
package xml_test;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement
class Customer {
String name;
int age;
int id;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
}
Here's my code:
package xml_test;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class XML_Test {
public static void main(String[] args) {
try {
File file = new File("S:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
/* Added for testing */
System.out.println(customer.id);
System.out.println(customer.age);
} catch (JAXBException e) {
e.printStackTrace();
}
}
Here's my data file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
<age>29</age>
<name>mkyong</name>
</customer>
Any help would be most appreciated.
Thanks!