0

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.

2 Answers 2

1


I can help you with this 2 solutions:

Methode 1: select all element agregator1 after that verify only attributeValue if equal a CSP.

Element root = document.getRootElement();
List<Element> listElement = root.selectNodes("./agregator1");
if (listElement != null) {
    for (int j = 0; j < listElement.size(); j++) {
        Element el = listElement.get(j);
        String attrValue = el.attributeValue("id", "");
        if (attrValue.length() > 0 && attrValue.equals("CSP")) {
            // do your code
        }
    }
}
else {
    // element not found
}

Methode 2: do this in just 1 line, use xpath

Element rootElement = doc.getRootElement();
List<Element> elementNeeded = rootElement.selectNodes("./agregator1[@id='CSP']");
if (elementNeeded != null && elementNeeded.size() > 0) {
    // element found do your code
}
else {
    // element not found
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you do that? List doesn't take parameters
Isn't that the exact same thing I've done? I need a way to get the agregator1 child nodes...
for the first methode: the element "el" (Element el = listElement.get(j);) has all child nodes for get one just do this: el.selectnodes("./[NameOfElementYouLike]") ==> your get list for all element with name = [NameOfElementYouLike]. 2- for the second methode you current element is "elementNeeded": this element has all childs nodes
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.