Keep value over the whole recursion



DESCRIPTION OF THE PROBLEM


I want to modify the xml-structure to create a new file with a similar xml-structure (though it bases on XML but is not the same).


I have the following XML-structure:



<?xml version="1.0" encoding="UTF-8"?>
<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>
<target>
<objectType>conceptMap</objectType>
<id>4b8f69e3-2914-3a1a-454e-f4c157734bd1</id>
<displayName>my first concept map</displayName>
</target>
<generator>
<objectType>application</objectType>
<url>http://ift.tt/1tOtl1k;
<id>c9933ad6-dd4a-6f71-ce84-fb1676ea3aac</id>
<displayName>ut.tools.conceptmapper</displayName>
</generator>
<provider>
<objectType>ils</objectType>
<url>http://ift.tt/100GWWw;
<id>10548c30-72bd-0bb3-33d1-9c748266de45</id>
<displayName>unnamed ils</displayName>
</provider>
</log>


Here's the method which should do this (including both submethods):



public static void parseRecursive(Node node, Node parent, Node value) {

NodeList childs = node.getChildNodes();
int countChilds = childs.getLength();
int i = 0;
//<log> shouldn't be added as a node!
if (parent != null && !(parent.getNodeName().equals("log"))) {
if (!doubledItem.contains(parent)) {
value = toXes.createString(parent.getNodeName(), "");
doubledItem.add(parent);
System.out.println("Added ---" + parent + "--- to HashSet");
}
}
//iterate over all child elements
while (i < countChilds) {
Node child = childs.item(i);

//Proofs whether "#text" is the only child-element.
if (countChilds > 1) {
//System.err.println("Number of WHILE-passes: " + i);
parseRecursive(child, node, value);
} else {
//"published" and "verb" shouldn't be handled as if they were child elements of <log> but rather of <event>
if (parent != null && (node.getNodeName().equals("verb")|| node.getNodeName().equals("published"))){
toXes.createString(node.getNodeName(), child.getNodeValue());
//leave out the id-node
} else if (parent != null && !(node.getNodeName().equals("id"))) {
//in some passes the value is "null". What I want: it shouldn't be null but pass the value of the previous recursion without execute "createString" multiple times.
toXes.createNestedString2(node.getNodeName(), child.getNodeValue(), value);
}

}
++i;
}
}

//Appends the <string>-element to the <event>-element.
public static Node createString(String key, String value) {
System.out.println("KEY: " + key + " - VALUE: " + value);

//NodeList nl = eventElement.getElementsByTagName(key);
//if (nl == null) {
stringElement = doc.createElement("string");
// 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);

eventElement.appendChild(stringElement);

//}
return stringElement;

}
//Appends the <string>-element to the parent element.
public static void createNestedString2(String key, String value, Node parent) {
Element stringElement = doc.createElement("string");
parent.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);
}


As mentioned in the comments I try to achieve that the Node which is generated by "createNestedString2" should only append to the actual parent and not just anywhere without calling "createString" more than one time for each parent node. Is there a way to save the result of the return that "createString" generates for more than one recursion pass without calling createString more than one time?


EDIT


Expected Output:



<log xes.features="" xes.version="2.0">
<trace>
<event>
<string key="published" value="2014-03-28T15:28:36.646Z"/>
<string key="actor" value="">
<string key="objectType" value="person"/>
<string key="displayName" value="anonymized"/>
</string>
<string key="verb" value="update"/>
<string key="object" value="">
<string key="objectType" value="concept"/>
<string key="content" value="time"/>
</string>
<string key="target" value="">
<string key="objectType" value="conceptMap"/>
<string key="displayName" value="my first concept map"/>
</string>
<string key="generator" value="">
<string key="objectType" value="application"/>
<string key="url" value="http://ift.tt/1plxxoz"/>
<string key="displayName" value="ut.tools.conceptmapper"/>
</string>
<string key="provider" value="">
<string key="objectType" value="ils"/>
<string key="url" value="http://ift.tt/1plxxER"/>
<string key="displayName" value="unnamed ils"/>
</string>
</event>
</trace>
</log>


But the output looks like:



<log xes.features="" xes.version="2.0">
<trace>
<event>
<string key="published" value="2014-03-28T15:28:36.646Z"/>
<string key="actor" value="">
<string key="objectType" value="person"/>
<NULLPOINTER-EXCEPTION, because "value" is null>

No comments:

Post a Comment