Java:Jax reading and writing xml, classes confusion

Andrew

Hello i am new to xml reading/writing and am not sure if i doing reading correctly.I am using Java core with jaxb-api version 2.1.What i want to do is basically create product objects and write them to a different file based on this xml.the problem is that i am not sure how to mark the fields do i need to create only an Order class and add the products as wrappers for the fields that are inside or do i need to create a product class aswell? it is confusing for me since product is inside order but it acts like a wrapper for the other tags inside. I am finding several tutorials online but none of them handle a List of objects, most of the examples contain simple String fields or arrays of Strings.So i am not sure how to handle this issue.

Any help would be greatly appreciated. Thank you very much

<orders>
    <order created='2012-07-12T15:29:33.000' ID='2343'>
        <product>
            <description>Sony 54.6" (Diag) Xbr Hx929 Internet Tv</description>
            <gtin>00027242816657</gtin>
            <price currency="USD">2999.99</price>
            <supplier>Sony</supplier>
        </product>
        <product>
            <description>Apple iPad 2 with Wi-Fi 16GB - iOS 5 - Black</description>
            <gtin>00885909464517</gtin>
            <price currency="USD">399.0</price>
            <supplier>Apple</supplier>
        </product>
        <product>
            <description>Sony NWZ-E464 8GB E Series Walkman Video MP3 Player Blue</description>
            <gtin>00027242831438</gtin>
            <price currency="USD">91.99</price>
            <supplier>Sony</supplier>
        </product>
    </order>
    <order created='2012-07-13T16:02:22.000' ID='2344'>
        <product>
            <description>Apple MacBook Air A 11.6" Mac OS X v10.7 Lion MacBook</description>
            <gtin>00885909464043</gtin>
            <price currency="USD">1149.0</price>
            <supplier>Apple</supplier>
        </product>
        <product>
            <description>Panasonic TC-L47E50 47" Smart TV Viera E50 Series LED HDTV</description>
            <gtin>00885170076471</gtin>
            <price currency="USD">999.99</price>
            <supplier>Panasonic</supplier>
        </product>
    </order>
</orders>
@XmlRootElement
public class Order {
    private LocalDateTime created;
    private List<Product> products;


    public LocalDateTime getCreated() {
        return created;
    }

    @XmlElement
    public void setCreated(LocalDateTime created) {
        this.created = created;
    }

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

    @XmlElementWrapper(name="product")
    @XmlElement
    public void setProducts(List<Product> products) {
        this.products = products;
    }
}
Thomas Fritsch

There are several issues with your code. (In order not to spoil your learning experience, I will only give some instructions, but no complete code.)

  • To represent the XML root element <orders>...</orders> you need another class (let's call it Orders).
    • Annotate it with @XmlRootElement.
    • The <orders> element contains several <order> elements. Therefore you need to add a member List<Order> orders. Annotate it with @XmlElement(name="order").
  • In your Order class:
    • You don't need the @XmlRootElement annotation here, because <order> is not the root element.
    • Annotate the products with only @XmlElement(name="product") (as already written by @PiotrP.Karwasz in his comment).
    • Annotate the created member with @XmlAttribute instead of @XmlElement. This is because you have an XML attribute created="something", not an XML element <created>something</created>.
    • JAXB will probably complain that it doesn't support LocalDateTime. So you may need to change this to Date.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive