Friday, January 31, 2014

Test for XML Import and MOXy + EclipseLink

package kcwobjectxmljsontest;
 
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.stream.StreamSource;

/**
 *
 * @author KCW
 */
public class kcwMOXytest {
    
    @XmlRootElement
    @XmlType(propOrder={"street", "city", "zip"})
    @XmlAccessorType(XmlAccessType.FIELD)
    static public class Address {

        String street;
        String city;
        String zip;
    }

    @XmlRootElement
    @XmlType(propOrder={"name","address"})
    @XmlAccessorType(XmlAccessType.FIELD)
    static public class Customer {

        String name;

        @XmlElement
        Address address;

    }
    
    @XmlRootElement
    static public class Customers {
        @XmlElement(name="customer")
        List<Customer> customers;
    }
    
    
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customers.class);
 
        System.out.println("---------------------");
        System.out.println("Convert XML to Object");
        System.out.println("---------------------");

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource xml = new StreamSource("data/customers.xml");
//        Customers customers = (Customers) unmarshaller.unmarshal(xml);
        Customers doc= (Customers) unmarshaller.unmarshal(xml);
        
//        System.out.println("customers.test="+customers.test);
        System.out.println("customerlist.customers.="+doc.customers.size());
        int i=0;
        for(Customer s : doc.customers) {
            System.out.println("doc.customers.get("+i+").name="+s.name);
            System.out.println("doc.customers.get("+i+").address.city="+s.address.city);
            System.out.println("doc.customers.get("+i+").address.street="+s.address.street);
            System.out.println("doc.customers.get("+i+").address.zip="+s.address.zip);
            System.out.println("---------------------");
            i++;
        }        


        System.out.println("");
        System.out.println("");
        System.out.println("---------------------");
        System.out.println("Convert Object to XML");
        System.out.println("---------------------");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(doc, System.out);
        
        FileOutputStream fos= new FileOutputStream("data/test-list.xml");
        marshaller.marshal(doc, fos);
    }
}


//================================================
// Customers.xml
<?xml version="1.0" encoding="UTF-8"?>
<customers>
    <customer>
        <name>Jane Doe</name>
        <address>
            <zip>19873</zip>
            <street>1 A Street</street>
            <city>Any Town</city>
        </address>
    </customer>
    <customer>
        <name>Jane Doe 2</name>
        <address>
            <zip>19872</zip>
            <street>2 A Street</street>
            <city>Any Town Area</city>
        </address>
    </customer>
</customers>

1 comment:

  1. Casino Review 2021: Is it Worth Your Bet $20? - DrmCD
    Are you a gambler and a 대전광역 출장샵 gambler who 계룡 출장안마 still have a good time? Well, we've 안산 출장안마 here at Dr. MCD to answer that question. 김포 출장샵 The answer is 경상남도 출장마사지 NO. Casino players are

    ReplyDelete