JAXB: creating java object from xml-attribute

I want to map an XML file (which is mostly exported from some database) to java objects using JAXB.

My target XML is like -

<database name="sales">
    <table name="product">
        <column name="id">1</column>
        <column name="name">Product 1</column>
        <column name="qty">10</column>
    </table>
    <table name="product">
        <column name="id">2</column>
        <column name="name">Product 2</column>
        <column name="qty">20</column>
    </table>
</database>

      

And I am expecting a model like below:

public class Product {
  int id;
  String name;
  int qty;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getQty() {
    return qty;
  }

  public void setQty(int qty) {
    this.qty = qty;
  }
}

      

and

public class Sales {
  List<Product> products;

  public List<Product> getProducts() {
    return products;
  }

  public void setProducts(List<Product> products) {
    this.products = products;
  }
}

      

Some Unmarshelling example that suits my requirements would be very helpful. Can anyone help me please!

+3


source to share


2 answers


Final EDIT:

You need to add the following classes:

Change sales class to:

   /**
JMathur
 */

package com.org.test1;

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;

@XmlRootElement(name="database")
@XmlAccessorType(XmlAccessType.FIELD)
public class Sales {

@XmlElement(name="table")
List<Product> products;

@XmlAttribute(name="names")
private String sales;

public String getSales() {
    return sales;
}

public void setSales(String sales) {
    this.sales = sales;
}

public List<Product> getProducts() {
    return products;
}

public void setProducts(List<Product> products) {
    this.products = products;
}


}

      

Change the product class to:

    /**
JMathur
 */

package com.org.test1;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

@XmlAccessorType(XmlAccessType.FIELD)
public class Product {
@XmlAttribute(name="name")
private String tableName;


public String getTableName() {
    return tableName;
}

public void setTableName(String tableName) {
    this.tableName = tableName;
}


private  List<Column> column;


public List<Column> getColumn() {
    return column;
}

public void setColumn(List<Column> column) {
    this.column = column;
}



}

      

Then you need to add two more classes:

Column.java



 /**
JMathur
*/

package com.org.test1;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlAccessorType(XmlAccessType.FIELD)
public class Column {
@XmlValue
@XmlJavaTypeAdapter(AdapterDATA.class)
protected String value;

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

@XmlAttribute
protected String name;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


}

      

AdapterDATA.java

package com.org.test1;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class AdapterDATA extends XmlAdapter<String, String> {

@Override
public String marshal(String arg0) throws Exception {
    return arg0;
}
@Override
public String unmarshal(String arg0) throws Exception {
    return arg0;
}

}

      

Then you can marshal and unmarhsal with the following code

     /**
JMathur
*/

package com.org.test1;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class XMLBuilder {
public static void main(String args[]) {
    Sales object = new Sales();
    object.setSales("sales");

    Product p1 = new Product();
    Product p2 = new Product();

    Column c1 = new Column();
    Column c2 = new Column();
    Column c3 = new Column();
    c1.setName("id");
    c1.setValue("1");
    c2.setName("name");
    c2.setValue("Product_Name");
    c3.setName("qty");
    c3.setValue("10");

    List<Column> listColumn=new ArrayList<Column>();
    listColumn.add(c1);
    listColumn.add(c2);
    listColumn.add(c3);

    p1.setTableName("product");
    p1.setColumn(listColumn);

    p2.setTableName("product");
    p2.setColumn(listColumn);

    List<Product> list = new ArrayList<Product>();
    list.add(p1);
    list.add(p2);

    object.setProducts(list);

    String response=marshal(object);

    Sales salesAfterUnMarshal=(Sales)unmarshal(new StringReader(response), Sales.class);



    System.out.println("Marshalling====="+response);

    System.out.println("Unmarshalling==="+salesAfterUnMarshal.getProducts());
}

public static <T> String marshal(T object) {

    StringWriter response = new StringWriter();
    try {
        JAXBContext jaxbContext = JAXBContext
                .newInstance(object.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller
                .setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);

        jaxbMarshaller.marshal(object, response);

    } catch (JAXBException jxbException) {
        throw new RuntimeException(jxbException.getMessage(), jxbException);
    }
    return response != null ? response.toString() : null;

}
public static <T> T unmarshal(Reader reader, Class<T> clazz) {
    T classInstance = null;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Object object = unmarshaller.unmarshal(new StreamSource(reader));
        classInstance = clazz.cast(object);
    } catch (JAXBException jxbException) {
    //  throw new RuntimeException(errorMessage, jxbException);
    }
    catch (Exception jxbException) {
    //  throw new RuntimeException(errorMessage, jxbException);
    }
    return classInstance;
}
}

      

Output:

Marshalling=====<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Database names="sales">
<table name="product">
    <column name="id">1</column>
    <column name="name">Product_Name</column>
    <column name="qty">10</column>
</table>
<table name="product">
    <column name="id">1</column>
    <column name="name">Product_Name</column>
    <column name="qty">10</column>
</table>
</Database>

Unmarshalling===[com.org.test1.Product@27d6c5e0, com.org.test1.Product@4f3f5b24]

      

+1


source


I think you need to change xml

like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<database name="sales">
 <table name="product">
    <id>1</id>
    <name>Product 1</name>
    <qty>10</qty>
 </table>
</database>

      

Define a class Product

, for example:

@XmlRootElement(name="table")
public class Product {
 int id;
 String name;
 int qty;

public int getId() {
    return id;
}
@XmlElement
public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getQty() {
    return qty;
}

public void setQty(int qty) {
    this.qty = qty;
}

 @Override
 public String toString() {
    return id+" "+ name +" "+qty;
 }
}

      

And a Sales

class like:



@XmlRootElement(name="database")
public class Sales {
List<Product> products;

@XmlElement(name="table")
public List<Product> getProducts() {
    return products;
}

public void setProducts(List<Product> products) {
    this.products = products;
}
}

      

After defining the class Main

:

public class Main {

public static void main(String[] args) {
    try {

        File file = new File("sales.xml");
        System.out.println(file);
        JAXBContext jaxbContext = JAXBContext.newInstance(Sales.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Sales sales = (Sales) jaxbUnmarshaller.unmarshal(file);

        List<Product> products= sales.getProducts();
        System.out.println(products.size());
        Product product= products.get(0);
        System.out.println(product);

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}
}

      

It gives me this result: 1 Product 1 10

0


source







All Articles