Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with STAX parsing in Java
    primarykey
    data
    text
    <p>I have an XML file as follows:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;top&gt; &lt;dist id="1"&gt; &lt;emp name="gaara" age="22" location="konoha"/&gt; &lt;emp name="vegeta" age="44" location="namek"/&gt; &lt;assitant name="nappa" age="64" location="namek"/&gt; &lt;/dist&gt; &lt;dist id="2"&gt; &lt;emp name="naruto" age="20" location="konoha"/&gt; &lt;emp name="lei" age="21" location="sand"/&gt; &lt;assitant name="gohan" age="25" location="island"/&gt; &lt;/dist&gt; &lt;/top&gt; </code></pre> <p>The expected result is as follows:</p> <pre><code>Required O/P 1 | gaara | 22 | konoha 1 | vegeta | 44 | namek 2 | naruto | 20 | konoha 2 | lei | 21 |sand </code></pre> <p>Can anyone explain how can I do the same. And one thing is the output should not contain assistant element data.</p> <p>I would have posted the code but since the elements appear more than once the code I have is not working.</p> <p><strong>EDIT</strong> I am posting the code : </p> <p>1) The class containing the main</p> <pre><code>import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; public class loadNwccData { public static void main(String[] args) throws XMLStreamException, FileNotFoundException { List&lt;Employee&gt; empList = null; Employee currEmp = null; XMLInputFactory factory = XMLInputFactory.newInstance(); InputStream inputStream= new FileInputStream("C:\\Documents and Settings\\samp\\Desktop\\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())){ currEmp = new Employee(); currEmp.id = reader.getAttributeValue(null, "id"); } else if ("emp".equals(reader.getLocalName())){ 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); } } } class Employee{ String id; String name; String age; String location; @Override public String toString(){ return id +" | " +name +" | "+age +" | "+location; } } </code></pre> <p>2) The eenum file</p> <pre><code>public enum Cloumns {emp,assitant,dist,top}; </code></pre> <p>3) The O/P I'm getting:</p> <pre><code>1 | vegeta | 44 | namek 1 | vegeta | 44 | namek 2 | lei | 21 | sand 2 | lei | 21 | sand </code></pre> <p>Can anyone please point out what is the issue here as the values are repeating.</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.
 

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