Unmarshal对象内的Java循环 [英] Java loop inside Unmarshal object
问题描述
在此链接上: JAXB解组XML字符串-遍历所有标签 提出了一个好的解决方案. 我正在尝试从中受益,但无法使其适用于我的情况.
At this link: JAXB Unmarshalling XML string - Looping through all tags it was proposed a good solution. I'm trying to benefit from it but I am not able to have it working in my case.
考虑这样的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.
private String Name = null;
[...]
Name = campaign.getName().getValue();
System.out.println(Name);
知道其他XML文件中可能还有更多<record>
的情况下,如何循环进入<record>
,并获取firstTime和secondTime的所有值?
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?
广告系列
@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;}
}
程序更大,但是不断重复..我需要提取所有内容以便以后上传到数据库. 但是现在,我只需要在屏幕上打印它们,并且知道我已经拥有了所有需要的值即可.
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!
谢谢
编辑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.
分辨率
我终于能够找到解决方案,而不是提出并标记为正确的解决方案.尽管它帮助我创建了getRecord()
吸气剂.
EDIT 4: The resolution
I was finally able to find the solution, which is not the one proposed and marked as correct. Although it helped me creating the getRecord()
getter.
无需更改我的代码,而是添加到record.class:
Without changing my code, but adding to the 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
里面还有另一个列表,则另外一个:
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:
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 because it starts from 0.
推荐答案
我仍然不确定您的问题是什么.告诉我我的答案是否不是您想要的.
I'm still unsure about what your problem is. Tell me if my answer is not what you're looking for.
您需要进行一些更改:
@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()方法.
I'll go under the assumption that you have defined proper toString() method for all of the classes used in the Campaign.
例如,Record类字符串可能像这样:
For exemple, the Record class string may go like :
@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()行更改为所需的任何代码.
You can of course change the System.out.println() lines to whatever code you want.
这篇关于Unmarshal对象内的Java循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!