0

我正在尝试使用 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);
        }

如果您能帮助我正确格式化它,那将意味着很多!先谢谢了!

4

1 回答 1

1

在尝试了许多不同的事情之后,我发现你必须使用迭代器。

        Set mapSet = (Set) Analyser.XMLDUMP.entrySet();
        Iterator mapIterator = mapSet.iterator();

        while (mapIterator.hasNext()) {
            Map.Entry mapEntry = (Map.Entry) mapIterator.next();
            String keyValue = (String) mapEntry.getKey();
            String value = (String) mapEntry.getValue();

            Element Details = doc.createElement(keyValue);
            root.appendChild(Details);

            Element id = doc.createElement("Class");
            id.appendChild(doc.createTextNode(value));
            Details.appendChild(id);

            for (FieldAnalyser fieldAnal : Analyser.FIELDS ) {
                if(fieldAnal.getOwner().equals(value)) {

                    Element fieldFound = doc.createElement(fieldAnal.getName());
                    fieldFound.appendChild(doc.createTextNode(fieldAnal.getField()));
                    Details.appendChild(fieldFound);
                }
            }
        }
于 2013-06-19T13:41:48.273 回答