Unmarshal对象中的Java循环

时间:2016-09-07 12:14:46

标签: java loops jaxb

点击此链接:JAXB Unmarshalling XML string - Looping through all tags 提出了一个很好的解决方案。 我正试图从中受益,但我不能让它在我的情况下工作。

考虑这样的XML:

<campaign value="field1">
  <name value="firstName"/>
  <type value="type"/>
  <record>
    <firstTime value="13"/>
    <secondTime value="14"/>
  </record>
</campaign>

考虑Unmarshalling,创建和工作的所有类。 广告系列类,包含名称,值,记录[]等。

JAXBContext c = JAXBContext.newInstance(Campaign.class);
Unmarshaller u = c.createUnmarshaller();
Campaign campaign = (Campaign) u.unmarshal(file);

我可以提取“name”和“type”的值但是,由于List&lt;&gt;,我无法超越这个。

private String Name = null;
[...]
Name = campaign.getName().getValue();
System.out.println(Name);

如何知道在其他XML文件中可能有更多<record>,你将如何循环到<record>并获取firstTime和secondTime的所有值?

修改 广告系列类

@XmlRootElement(name = "Campaign")
@XmlType(name = "Campaign", propOrder = {
    "Name",
    "Type",
    "Record" })
public class Campaign {
    @XmlElement(required = true)
    protected Name Name;

    @XmlElement(required = true)
    protected Type Type;

    @XmlElement(required = true)
    protected List<Record> Record= new ArrayList<Record>();


    public Name getName () {return Name;}
    public void setName (Name value) {this.Name= value;}

    public Type getType () {return Type;}
    public void setType (Type value) {this.Type = value;}

    public void setRecord(Recordvalue) {this.Record.add(value);}
}

记录类

@XmlRootElement(name = "Record")
@XmlType(name = "Record", propOrder = {
    "firstTime",
    "secondTime" })
public class Record{
    @XmlElement(required = true)
    protected firstTime firstTime;

    @XmlElement(required = true)
    protected secondTime secondTime;


    public Name getFirstTime () {return firstTime;}
    public void setFirstTime (firstTime value) {this.firstTime= value;}

    public Name getSecondTime () {return secondTime;}
    public void setSecondTime (secondTime value) {this.secondTime= value;}
}

程序更大,但它只是不断重复..我需要提取所有内容,以便以后上传到数据库。 但现在,我只需要在屏幕上打印它们并知道我拥有所需的所有值。

是的,我也看过那个链接,我不知道为什么我不能专注于如何继续,似乎最糟糕的部分对我来说更容易!

由于

编辑3 我忘了粘贴set和get的方法。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Type")
public class Type {

    @XmlAttribute
    protected String value;

    public String getValue() {return value;}
    public void setValue(String value) {this.value = value;}
}

对所有人来说都一样,我只是在这里贴了Type。

编辑4:分辨率 我终于能够找到解决方案,这不是建议的解决方案,并且标记为正确。虽然它帮助我创建了getRecord() getter。

不改变我的代码,而是添加到record.class:

public List<Record> getRecord() { return this.Record; }

和主要课程:

for (int i=1; i<=campaign.getRecord().size(); i++)   {                System.out.println(campaign.getRecord().get(i-1).getFirstTime().getValue());
System.out.println(campaign.getRecord().get(i-1).getSecondTime().getValue());
}

我可以打印“13”和“14”,更一般来说,我可以打印record内的任何内容,如果record内部还有另一个List,还可以打印一个:

for (int j=1; j<= campaign.getRecord().get(i-1).getANewListInsideRecord().size(); j++) {
System.out.println(campaign.getRecord().get(i-1).getANewListInsideRecord().get(j-1).getAObject().getValue());
}

将完成这项工作。

-1因为它从0开始。

1 个答案:

答案 0 :(得分:1)

我仍然不确定你的问题是什么。告诉我,如果我的答案不是你想要的。

您需要做一些更改:

@XmlRootElement(name = "campaign")
@XmlType(name = "campaign", propOrder = {
    "name",
    "type",
    "record" })
public class Campaign {
    @XmlElement(required = true)
    protected Name name;

    @XmlElement(required = true)
    protected Type type;

    @XmlElement(required = true)
    protected List<Record> record;


    public Name getName () {return name;}
    public void setName (Name value) {this.name= value;}

    public Type getType () {return type;}
    public void setType (Type value) {this.type = value;}

    //Initialise your record list inside the getter instead of in member declaration
    public List<Record> getRecord() {
        if(this.record == null) record = new ArrayList<>();
        return this.record;
    }

    //Do not use a setter as an add method for the list
    public void setRecord(List<Record> value) {this.record = value;}

    //If you need to add record inside your list, do not use a setter, define an add method or access the list with the getter.
    public void addRecord(Record value) {this.record.add(value);}
}

我会假设您已为Campaign中使用的所有类定义了正确的toString()方法。

例如,Record类字符串可能类似于:

@Override
public String toString(){
    return " First value : + "this.firstTime.getValue() + " Second value : " +this.secondTime.getValue();
}

现在显示所有内容:

List<File> files = new ArrayList<>();

//Add all XML Files containing a campaign root element into the files list
JAXBContext c = JAXBContext.newInstance(Campaign.class);
Unmarshaller u = c.createUnmarshaller();

//Declare list to store all of your camapaign object
List<Campaign> campaigns = new ArrayList<>();

for(File f : files)
{
    campaigns.add(u.unmarshall(f));
}

//display all campaigns
for(Campaign camp : campaigns){
    System.out.println(camp.getName());
    System.out.println(camp.getType());

    //Display all records
    for(Record rec : camp.getRecord()){
        System.out.println(rec);
    }
}

您当然可以将System.out.println()行更改为您想要的任何代码。