here's a sample of my XML file:
<agregator1 id="CSP">
<id>int</id>
<type>string</type>
</agregator1>
<agregator1 id="Microgrid">
<id>int</id>
<type>string</type>
</agregator1>
I've never worked with DOM4J
and I've read the documentation but I can't seem to get the sub-elements into ArrayList
's, I'm trying to do the following:
arrayList1: id, type
arrayList2: int, string
here's my code:
for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
Element foo = (Element) i.next();
if(foo.attributeValue("id").toString().equals("CSP"))
{
//what am I missing here?
}
}
I've searched a lot, and I can't find any solution since I can't do foo.getChildNodes()
.
Any sugestions?
EDIT: I need to get the node names without using elementText(String)
(or anything like this) because I need my code to work even if the XML file is changed, without editing my code.
I solved my problem. Thanks to all who tryed to help. To all the people who are searching for a similar solution, here's my code:
This is the above method, completed:
public static ArrayList<Element> readSubElements()
{
ArrayList<Element> subElements = new ArrayList<>();
Element root = configs.XMLreaderDOM4J.getDoc().getRootElement();
for (Iterator it = root.elementIterator(); it.hasNext();){
Element foo = (Element) it.next();
if(foo.attributeValue("id").equals("VPP"))
{
for (Iterator it2 = foo.elementIterator(); it2.hasNext(); )
{
Element temp = (Element) it2.next();
subElements.add(temp);
System.out.println(""+temp);
}
}
}
return subElements;
}
And a new method I've created:
public static void getTagNames() {
ArrayList<Element> elements = readSubElements();
ArrayList<String> list1 = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
setSize(elements.size());
for (Iterator it = elements.iterator(); it.hasNext();) {
Element entry = (Element) it.next();
list1.add(entry.getQualifiedName());
list2.add(entry.getText());
}
setTagNamesAL(list1);
setTagContentAL(list2);
}