Unmarshalling XML using JAXB

2019-02-17 16:16发布

I went through almost all questions related to this topic here. But was not able to get a proper solution.

My issue is as follows:

I created a simple program to unmarshall an xml file for which i had a xsd. I was able to do that successfully. But if i am getting an xml without xsd, how can I get my attributes from that, if the xml looks something like this :

<items>
  <item>
    <code>12000</code>
    <name>Samsung  620</name>
    <price>9999</price>
  </item>
  <item>
    <code>15000</code>
    <name>NOKIA</name>
    <price>19999</price>
  </item>
  <item>
    <code>18000</code>
    <name>HTC 620</name>
    <price>29999</price>
  </item>
</items> 

Here I don't have an xsd to generate my classes. How can i proceed? Kindly help me.

Thank You

标签: java xml jaxb
2条回答
时光不老,我们不散
2楼-- · 2019-02-17 16:26

Below is one way that you could map your use case with a JAXB (JSR-222) implementation:

Items

We will use the following class for the root object and annotate it with @XmlRootElement. The @XmlRootElement annotation tells JAXB that this class should be instantiated if the root element in the document being unmarshalled is items, you can also specify a different name @XmlRootElement(name="foo").

package forum11152046;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Items {

    private List<Item> items;

    @XmlElement(name="item")
    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

}

Item

In this example I created a class where all the property names correspond directly to the names in the XML document. This means there aren't any annotations that need to be added. If you need to override the default name you can use an annotation such as @XmlElement to do so. I used the @XmlElement annotation to do this in the Items class for the items property.

package forum11152046;

public class Item {

    private int code;
    private String name;
    private int price;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

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

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}

Demo

package forum11152046;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Items.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11152046/input.xml");
        Items items = (Items) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(items, System.out);
    }

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items>
    <item>
        <code>12000</code>
        <name>Samsung  620</name>
        <price>9999</price>
    </item>
    <item>
        <code>15000</code>
        <name>NOKIA</name>
        <price>19999</price>
    </item>
    <item>
        <code>18000</code>
        <name>HTC 620</name>
        <price>29999</price>
    </item>
</items>
查看更多
Juvenile、少年°
3楼-- · 2019-02-17 16:29

If you want to stick with JAXB, you can either write an XML Schema Document on your own to validate such XML (it looks simple but it's just an instance, you need to find out what could change in these documente beforehand) or create a POJO with JAXB annotations matching these nodes. I'm afraid there's no other way. You still have to know well what the format allows.

查看更多
登录 后发表回答