簡體   English   中英

使用 XML 和 Java 編組和解組

[英]Marshalling and Unmarshalling with XML and Java

我正在嘗試將 Java 編組到 XML 並將該文件解組回 Java。 我正在使用 ArrayList 並且在編程方面仍然很新。 任何指導將不勝感激。

這是我的保存方法

public void save() throws Exception {

try {
    File file = new File("order.xml");
    Items item = new Items();
    JAXBContext c = JAXBContext.newInstance(Items.class);
    Marshaller m = c.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(item, file);
    m.marshal(item, System.out);
 }
    catch (JAXBException e) {
    e.printStackTrace();
    }
 }

這是我的加載方法

public void load() throws Exception {
try {
    File file = new File("order.xml");
    JAXBContext c = JAXBContext.newInstance(Items.class);
    Unmarshaller u = c.createUnmarshaller();
    Items item = (Items) u.unmarshal(file);
    u.unmarshal(file);
}
catch (JAXBException e){
    e.printStackTrace();
}
}

這是我的 Items 類

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

import javax.xml.bind.annotation.*;

@XmlRootElement

public class Items {
    private List<String> pcPart;

    @XmlElement(name = "pcPart")
    public List<String> getpcPart(){
        if(pcPart == null){
        pcPart = new ArrayList<String>();
        }
        return pcPart;
    }

}

我試過添加item.setpcPart(save); 到我的保存方法,但無法解決,我嘗試使用save.addAll(item.getpcPart()); 到我的加載方法,它也無法解決。 任何時候我保存的輸出都是<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <items/>並且加載沒有輸出。 謝謝

將 setter 添加到您的 Items 類,如下所示:

public void setPcPart(List<String> pcPart) {
        this.pcPart = pcPart;
  }

import java.io.File;
import java.util.ArrayList;

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

    public class Parser {


    public static void main(String[] args) throws Exception {
        new Parser().save();
        new Parser().load();
    }

    public void save() throws Exception {

        try {
            File file = new File("C://order.xml");
            Items item = new Items();
            item.setPcPart(new ArrayList<String>(){{add("a");add("b");add("b");}});
            JAXBContext c = JAXBContext.newInstance(Items.class);
            Marshaller m = c.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(item, file);
            m.marshal(item, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    public void load() throws Exception {
        try {
            File file = new File("C://order.xml");
            JAXBContext c = JAXBContext.newInstance(Items.class);
            Unmarshaller u = c.createUnmarshaller();
            Items item = (Items) u.unmarshal(file);
            u.unmarshal(file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

}

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

import javax.xml.bind.annotation.*;

@XmlRootElement

public class Items {
    private List<String> pcPart;

    @XmlElement(name = "pcPart")
    public List<String> getpcPart(){
        if(pcPart == null){
        pcPart = new ArrayList<String>();
        }
        return pcPart;
    }

    public void setPcPart(List<String> pcPart) {
        this.pcPart = pcPart;
    }
    
    

}

Output
--------------
<items>
    <pcPart>a</pcPart>
    <pcPart>b</pcPart>
    <pcPart>b</pcPart>
</items>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM