[英]Java loop inside Unmarshal object
At this link: JAXB Unmarshalling XML string - Looping through all tags it was proposed a good solution. 在此链接上: JAXB解组XML字符串-遍历所有标签被提议是一个很好的解决方案。 I'm trying to benefit from it but I am not able to have it working in my case.
我正在尝试从中受益,但无法使其适用于我的情况。
Consider a XML like this: 考虑这样的XML:
<campaign value="field1">
<name value="firstName"/>
<type value="type"/>
<record>
<firstTime value="13"/>
<secondTime value="14"/>
</record>
</campaign>
And consider all the classes for Unmarshalling, created and working. 并考虑解组,创建和工作的所有类。 campaign class, containing name, value, record[] etc.
广告系列类别,包含名称,值,记录[]等。
JAXBContext c = JAXBContext.newInstance(Campaign.class);
Unmarshaller u = c.createUnmarshaller();
Campaign campaign = (Campaign) u.unmarshal(file);
I can extract the value of "name" and "type" but, because of the List<>, I am not able to go beyond this. 我可以提取“名称”和“类型”的值,但是由于List <>,我无法超出此范围。
private String Name = null;
[...]
Name = campaign.getName().getValue();
System.out.println(Name);
How would you loop into <record>
, and get all the values for firstTime and secondTime, knowing that there could be more <record>
in other XML file? 知道其他XML文件中可能还有更多的
<record>
,如何循环进入<record>
并获取firstTime和secondTime的所有值?
EDIT: Campaign Class 编辑: 竞选班
@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);}
}
Record Class 记录类
@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;}
}
The program is much more big, but it just keeps repeating.. I need to extract everything in order to upload to a DB later. 该程序更大,但是不断重复。.我需要提取所有内容以便以后上传到数据库。 But now, I only need to print them on screen and knowing that I have all the values I'd need.
但是现在,我只需要在屏幕上打印它们,并且知道我已经拥有了所有需要的值。
Yes I had a look at that link too, I don't know why I just can't concentrate on how to continue, seems that the worst part was easier for me! 是的,我也查看了该链接,我不知道为什么我不能专心于继续,似乎最糟糕的部分对我来说更容易!
Thanks 谢谢
EDIT 3 : I forgot to paste the methods for set and get. 编辑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;}
}
It is the same for all, I simply pasted Type here. 所有人都一样,我只是在此处粘贴Type。
EDIT 4: The resolution I was finally able to find the solution, which is not the one proposed and marked as correct. 编辑4:我终于能够找到解决方案,而不是提出并标记为正确的解决方案。 Although it helped me creating the
getRecord()
getter. 尽管它帮助我创建了
getRecord()
getter。
Without changing my code, but adding to the record.class: 无需更改我的代码,而是添加到record.class中:
public List<Record> getRecord() { return this.Record; }
and to the main class: 和主要的类:
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());
}
I am able to print "13" and "14", more in general, I am able to print anything inside record
and if record
would have another List inside, one more: 我可以打印“ 13”和“ 14”,更一般而言,我可以打印
record
任何内容,如果record
有另一个列表,则可以再打印一个:
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());
}
will do the the job. 会做的工作。
-1 because it starts from 0. -1,因为它从0开始。
I'm still unsure about what your problem is. 我仍然不确定您的问题是什么。 Tell me if my answer is not what you're looking for.
告诉我我的答案是否不是您想要的。
You have a few changes to make : 您需要进行一些更改:
@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);}
}
I'll go under the assumption that you have defined proper toString() method for all of the classes used in the Campaign. 我假设您已经为Campaign中使用的所有类定义了正确的toString()方法。
For exemple, the Record class string may go like : 例如,Record类字符串可能类似于:
@Override
public String toString(){
return " First value : + "this.firstTime.getValue() + " Second value : " +this.secondTime.getValue();
}
Now to display everything : 现在显示所有内容:
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);
}
}
You can of course change the System.out.println() lines to whatever code you want. 当然,您可以将System.out.println()行更改为所需的任何代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.