Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing XML to populate a JComboBox - something missing
    primarykey
    data
    text
    <p>it's me again. I have the following XML file:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;components&gt; &lt;resources&gt; &lt;resource id="House"&gt; &lt;id&gt;int&lt;/id&gt; &lt;type&gt;string&lt;/type&gt; &lt;maxUsage&gt;float&lt;/maxUsage&gt; &lt;minUsage&gt;float&lt;/minUsage&gt; &lt;averageUsage&gt;float&lt;/averageUsage&gt; &lt;/resource&gt; &lt;resource id="Commerce"&gt; &lt;id&gt;int&lt;/id&gt; &lt;type&gt;string&lt;/type&gt; &lt;maxUsage&gt;float&lt;/maxUsage&gt; &lt;minUsage&gt;float&lt;/minUsage&gt; &lt;averageUsage&gt;float&lt;/averageUsage&gt; &lt;/resource&gt; &lt;resource id="Industry"&gt; &lt;id&gt;int&lt;/id&gt; &lt;type&gt;string&lt;/type&gt; &lt;maxUsage&gt;float&lt;/maxUsage&gt; &lt;minUsage&gt;float&lt;/minUsage&gt; &lt;averageUsage&gt;float&lt;/averageUsage&gt; &lt;/resource&gt; &lt;/resources&gt; &lt;agregatorsType1&gt; &lt;agregator1 id="CSP"&gt; &lt;id&gt;int&lt;/id&gt; &lt;type&gt;string&lt;/type&gt; &lt;/agregator1&gt; &lt;agregator1 id="Microgrid"&gt; &lt;id&gt;int&lt;/id&gt; &lt;type&gt;string&lt;/type&gt; &lt;/agregator1&gt; &lt;/agregatorsType1&gt; &lt;soagregatorsType0&gt; &lt;agregator0 id="VPP"&gt; &lt;id&gt;int&lt;/id&gt; &lt;type&gt;string&lt;/type&gt; &lt;/agregator0&gt; &lt;/agregatorsType0&gt; &lt;/components&gt; </code></pre> <p>and I want to populate a JComboBox with the id's of each Resource (House, Commerce and Industry).</p> <p>I have the following method:</p> <pre><code>public static String[] readResourcesXML(String fileName) throws IOException, ClassNotFoundException, Exception { //Gets XML DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document documento = docBuilder.parse(fileName); //Searches all text documento.getDocumentElement().normalize(); //Gets elements from xml Element raiz = documento.getDocumentElement(); NodeList listaResources = raiz.getElementsByTagName("resources"); //Search all resources int tam = listaResources.getLength(); String[] vecResources = new String[tam]; for (int i = 0; i &lt; tam; i++) { Element elem = (Element) listaResources.item(i); vecResources[i] = elem.getAttribute("/resource/@id"); } //returns an array with all the id's of the resources return vecResources; } </code></pre> <p>NOTE: The String fileName has the following value: "src\configs\features.xml"</p> <p>The problem is, the JComboBox is always empty. What am I missing?</p> <p>Thanks ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload