I have an xml which consists of elements xx and xxt. Each xxt contains one or more xx elements. Each xx has an “cap” and “act” attributes. What I need to do is put all the cap-act values stored in a map.
Now note that my xml can contain 0 or an indefinite number of xxt. Thus, this should be done recursively. For now, I just did the simple parse when there are no xxt elements :
for (Element element: (List<Element>) minfo.elements()) {
if (element.getName().equals("xx")) {
String cap = element.attributeValue("cap").trim();
String act = element.attributeValue("act").trim();
map.put(cap,act);
}
else if(element.getName().equals(xxt)){
//TODO recursive method
}
}
An example of xml could be like this:
<xx cap="Min" act="act1"/>
<xx cap="Ver" act="asd" />
<xx cap="Tan" act="fw" />
<xxt id="PR" cap="A">
<xx cap="tY" act="ate" />
<xx cap="Tn" act="bga" />
<xx cap="Tn" act="sga" />
<xxt cap="an" act="y34" />
<xx cap="Miu" act="sahg"/>
</xxt>
</xxt>
<xx cap="Mzt" act="act1"/>
You simply need to create a method which takes in parameter the node to analyze and go through its children.
When a ‘xx’ is found, it is added to the map.
When a ‘xxt’ is found, you call the function by passing the ‘xxt’ node as parameter.
This code won’t compile but it is to give you an example.