XML in Java: Print every node once (instead of multiple times)



I have the following xml-structure (in my XML-file there are way more nodes but I think that's enough for demonstration):



<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>


The following method prints all nodes:



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("--------------------------------------------");
}
}
}


The Output look like this (again shortened, it is the Output of my "createString / createNestedString"-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=""/>
...


My problem is that I don't know why some nodes are printed multiple times. I expect that for example "log" and "actor" just appears once - and the child-nodes of them are appended once, too (indeed they will be appended only once as you see in the output... but the parent nodes are printed way to often) . Is there a quick hack how I can make this?


CreateString just appends a String-Node to the log element and createNestedString just appends a child node to the parent node element.


EDIT



//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);
}

No comments:

Post a Comment