【问题标题】:Get particular object in xml based on supplied criteria根据提供的条件获取 xml 中的特定对象
【发布时间】:2014-05-15 07:40:30
【问题描述】:

我是 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>

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

【问题讨论】:

标签: java xml jaxb


【解决方案1】:

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

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 + "]";
    }

}

类文档操作:

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 解组为对象。打印第一个对象。

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);
        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2019-07-21
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多