我正在尝试使用 HashMap 和 ArrayList 转储到 xml。
我希望它采用以下格式:
<Modscript>
<Class Name> Node </Class Name>
<Class Name Found> fi <Class Name Found>
<Field Name> getId<Field Name>
<Field Name Found> Dz <Field Name Found>
</Modscript>
我使用以下代码;
public static final HashMap<String, ClassNode> IDENTIFIED = new HashMap<>();
public static final ArrayList<FieldAnalyser> FIELDS = new ArrayList<FieldAnalyser>();
和
protected void addClass(ClassNode node, String name) {
IDENTIFIED.put(name, node);
}
protected ClassNode getClass(String name) {
return IDENTIFIED.get(name);
}
protected void addField(String cn, String fn, String name) {
FIELDS.add(new FieldAnalyser(cn, fn, name));
}
和
private final String owner;
private final String field;
private final String name;
public FieldAnalyser(String owner, String field, String name) {
this.owner = owner;
this.field = field;
this.name = name;
}
public String getOwner() {
return owner;
}
public String getField() {
return field;
}
public String getName() {
return name;
}
最后是我无法弄清楚如何正确操作的部分;
Element root = doc.createElement("Modscript");
doc.appendChild(root);
Element Details = doc.createElement("test");
root.appendChild(Details);
for (FieldAnalyser fieldAnal : Analyser.FIELDS ) {
Element tclass = doc.createElement("Class");
tclass.appendChild(doc.createTextNode(fieldAnal.getOwner()));
Details.appendChild(tclass);
Element tfield = doc.createElement("Field");
tfield.appendChild(doc.createTextNode(fieldAnal.getField()));
Details.appendChild(tfield);
Element tname = doc.createElement("name");
tname.appendChild(doc.createTextNode(fieldAnal.getName()));
Details.appendChild(tname);
}
如果您能帮助我正确格式化它,那将意味着很多!先谢谢了!