我有以下xml结构(在我的XML文件中有更多的节点,但我认为这足以进行演示):
<log>
<published>2014-03-28T15:28:36.646Z</published>
<actor>
<objectType>person</objectType>
<id>e1b8948f-321e-78ca-d883-80500aae71b5</id>
<displayName>anonymized</displayName>
</actor>
<verb>update</verb>
<object>
<objectType>concept</objectType>
<id>a1ad6ace-c722-ffa9-f58e-b4169acdb4e3</id>
<content>time</content>
</object>
</log>
以下方法打印所有节点:
public static void parseRecursive(Node node, Node parent) {
NodeList childs = node.getChildNodes();
if (parent != null) {
toXes.createString(parent.getNodeName(), "");
}
for (int i = 0; i < childs.getLength(); i++) {
if (childs.getLength() > 1) {
parseRecursive(childs.item(i), node);
} else {
toXes.createNestedString(node.getNodeName(), childs.item(i).getNodeValue(), "string");
System.out.println("PARENT: " + parent.getNodeName());
System.out.println("CHILD-KEY: " + node.getNodeName());
System.out.println("CHILD-VALUE: " + childs.item(i).getNodeValue());
System.out.println("--------------------------------------------");
}
}
}
输出看起来像这样(再次缩短,它是我的输出&#34; createString / createNestedString&#34; -methods):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<log xes.features="" xes.version="2.0">
<trace>
<event>
<string key="log" value=""/>
<string key="log" value="">
<string key="published" value="2014-03-28T15:28:36.646Z"/>
</string>
<string key="log" value=""/>
<string key="log" value=""/>
<string key="actor" value=""/>
<string key="actor" value="">
<string key="objectType" value="person"/>
</string>
<string key="actor" value=""/>
<string key="actor" value="">
<string key="id" value="e1b8948f-321e-78ca-d883-80500aae71b5"/>
</string>
<string key="actor" value=""/>
<string key="actor" value="">
<string key="displayName" value="anonymized"/>
</string>
<string key="actor" value=""/>
...
我的问题是我不知道为什么有些节点被多次打印。我希望例如&#34; log&#34;和&#34;演员&#34;只出现一次 - 它们的子节点也被附加一次(实际上它们只会在输出中看到一次附加...但父节点经常打印)。我怎么能做到这一点?
CreateString只是将一个String-Node附加到log元素,而createNestedString只是将一个子节点附加到父节点元素。
修改
//Appends the <string>-element to the <log>-element.
public static void createString(String key, String value) {
stringElement = doc.createElement("string");
eventElement.appendChild(stringElement);
// attributes of "string"-element in .xes - key
attr = doc.createAttribute("key");
attr.setValue(key);
stringElement.setAttributeNode(attr);
// attributes of "string"-element in .xes - value
attr = doc.createAttribute("value");
attr.setValue(value);
stringElement.setAttributeNode(attr);
}
//Asks whether the attribute is nested.
//Valid params: log, event, ...
public static void createNestedString(String key, String value, String nested) {
Element stringElement2 = doc.createElement("string");
if (nested.equals("log")) {
logElement.appendChild(stringElement2);
} else if (nested.equals("global")) {
globalElement.appendChild(stringElement2);
} else if (nested.equals("trace")) {
traceElement.appendChild(stringElement2);
} else if (nested.equals("event")) {
eventElement.appendChild(stringElement2);
} else if (nested.equals("int")) {
intElement.appendChild(stringElement2);
} else if (nested.equals("date")) {
dateElement.appendChild(stringElement2);
} else if (nested.equals("string")) {
stringElement.appendChild(stringElement2);
} else if (nested.equals("float")) {
floatElement.appendChild(stringElement2);
} else if (nested.equals("boolean")) {
booleanElement.appendChild(stringElement2);
} else if (nested.equals("id")) {
idElement.appendChild(stringElement2);
} else if (nested.equals("list")) {
listElement.appendChild(stringElement2);
} else if (nested.equals("container")) {
containerElement.appendChild(stringElement2);
} else {
System.err.println("ERROR: Could not append nested <string> to another element.");
}
// attributes of "string"-element in .xes - key
attr = doc.createAttribute("key");
attr.setValue(key);
stringElement2.setAttributeNode(attr);
// attributes of "string"-element in .xes - value
attr = doc.createAttribute("value");
attr.setValue(value);
stringElement2.setAttributeNode(attr);
}