根据提供的条件在xml中获取特定对象

时间:2014-05-15 07:40:31

标签: java xml jaxb

我是 Jaxb 的新生,并尝试在xml中进行读/写操作。 我完成了写操作,但是读取操作有问题。 我有以下xml -

<docOperations>    
<SkuSlabs id="1">
    <docId>677-WORK</docId>
    <itemIds>11</itemIds>
    <itemName>new item addedaaaaaa</itemName>
</SkuSlabs>
<SkuSlabs id="2">
    <docId>699-WORK</docId>
    <itemIds>21</itemIds>
    <itemName>extra</itemName>
</SkuSlabs>
</docOperations>

现在我想根据提供的条件解组SkuSlabs对象,其中id = 1&#39;但不知道如何实现。 请帮忙。

1 个答案:

答案 0 :(得分:0)

在这里(您想要为属性添加相同的访问修饰符):

package pl.skuslab;

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

@XmlRootElement(name = "SkuSlabs")
@XmlAccessorType(XmlAccessType.FIELD)
public class SkusLab {

    @XmlAttribute
    int id;

    String docId;
    int itemIds;
    String itemName;

    public SkusLab(int id, String docId, int itemIds, String itemName) {
        super();
        this.id = id;
        this.docId = docId;
        this.itemIds = itemIds;
        this.itemName = itemName;
    }

    public SkusLab() {
        super();
    }

    @Override
    public String toString() {
        return "SkusLab [id=" + id + ", docId=" + docId + ", itemIds=" + itemIds + ", itemName=" + itemName + "]";
    }

}

Class DocOperation:

package pl.skuslab;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class DocOperation {

    @XmlElement(name = "SkuSlabs")
    List<SkusLab> docOperations = new ArrayList<SkusLab>();
}

具有主要功能的类。首先,我像你写的那样生成XML,然后我将xml解组为obejcts。打印出第一个对象。

package pl.skuslab;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class SkusLabJaxb {

    private static JAXBContext jc;

    static {
        try {
            jc = JAXBContext.newInstance(DocOperation.class);
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    private static Marshaller getMarshaller() {
        try {
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            return marshaller;
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    private static Unmarshaller getUnmarshaller() {
        try {
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            return unmarshaller;
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String xml = "";

        try {
            DocOperation doper = new DocOperation();
            doper.docOperations.add(new SkusLab(1, "677-WORK", 11, "new item addedaaaaaa"));
            doper.docOperations.add(new SkusLab(2, "699-WORK", 21, "extra"));

            StringWriter sw = new StringWriter();
            getMarshaller().marshal(doper, sw);
            xml = sw.toString();
            System.out.println(xml);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        try {
            DocOperation docOperation = (DocOperation) getUnmarshaller().unmarshal(
                    new StringReader(xml));

            System.out.println(docOperation.docOperations.get(0).toString());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}