Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The trouble is you create Employee record for <code>dist</code> elements. </p> <pre><code>if ("dist".equals(reader.getLocalName())){ currEmp = new Employee(); currEmp.id = reader.getAttributeValue(null, "id"); } </code></pre> <p>This is followed my multiple employee definition. Effectively making the last element data into Employee record.</p> <p>I would store the id into a variable, say <code>currentDistID</code> and use it with Employee. The employee record should be created for each <code>employee</code> element.</p> <p>code</p> <pre><code> public static void main(String[] args) throws XMLStreamException, FileNotFoundException { List&lt;Employee&gt; empList = null; Employee currEmp = null; XMLInputFactory factory = XMLInputFactory.newInstance(); String currentDistID=null; InputStream inputStream= new FileInputStream("c:\\sample.xml"); XMLStreamReader reader = factory.createXMLStreamReader(inputStream); while(reader.hasNext()){ int event = reader.next(); switch(event){ case XMLStreamConstants.START_ELEMENT: if ("dist".equals(reader.getLocalName())){ //all subsequent employees share this. currentDistID = reader.getAttributeValue(null, "id"); } else if ("emp".equals(reader.getLocalName())){ currEmp = new Employee(); currEmp.id=currentDistID; currEmp.name = reader.getAttributeValue(null, "name"); currEmp.age = reader.getAttributeValue(null, "age"); currEmp.location = reader.getAttributeValue(null, "location"); } if("top".equals(reader.getLocalName())){ empList = new ArrayList&lt;Employee&gt;(); } break; case XMLStreamConstants.END_ELEMENT: Cloumns value = Cloumns.valueOf(reader.getLocalName()); switch(value){ case emp: empList.add(currEmp); break; } break; case XMLStreamConstants.START_DOCUMENT: //empList = new ArrayList&lt;Employee&gt;(); break; } } //Print the employee list populated from XML for ( Employee emp : empList){ System.out.println(emp); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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