Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Fastest solution is by far a StAX parser, specially as you only need a specific subset of the XML file and you can easily ignore whatever isn't really necessary using StAX, while you would receive the event anyway if you were using a SAX parser.</p> <p>But it's also a little bit more complicated than using SAX or DOM. One of these days I had to write a StAX parser for the following XML:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;table&gt; &lt;row&gt; &lt;column&gt;1&lt;/column&gt; &lt;column&gt;Nome&lt;/column&gt; &lt;column&gt;Sobrenome&lt;/column&gt; &lt;column&gt;email@gmail.com&lt;/column&gt; &lt;column&gt;&lt;/column&gt; &lt;column&gt;2011-06-22 03:02:14.915&lt;/column&gt; &lt;column&gt;2011-06-22 03:02:25.953&lt;/column&gt; &lt;column&gt;&lt;/column&gt; &lt;column&gt;&lt;/column&gt; &lt;/row&gt; &lt;/table&gt; </code></pre> <p>Here's how the final parser code looks like:</p> <pre><code>public class Parser { private String[] files ; public Parser(String ... files) { this.files = files; } private List&lt;Inscrito&gt; process() { List&lt;Inscrito&gt; inscritos = new ArrayList&lt;Inscrito&gt;(); for ( String file : files ) { XMLInputFactory factory = XMLInputFactory.newFactory(); try { String content = StringEscapeUtils.unescapeXml( FileUtils.readFileToString( new File(file) ) ); XMLStreamReader parser = factory.createXMLStreamReader( new ByteArrayInputStream( content.getBytes() ) ); String currentTag = null; int columnCount = 0; Inscrito inscrito = null; while ( parser.hasNext() ) { int currentEvent = parser.next(); switch ( currentEvent ) { case XMLStreamReader.START_ELEMENT: currentTag = parser.getLocalName(); if ( "row".equals( currentTag ) ) { columnCount = 0; inscrito = new Inscrito(); } break; case XMLStreamReader.END_ELEMENT: currentTag = parser.getLocalName(); if ( "row".equals( currentTag ) ) { inscritos.add( inscrito ); } if ( "column".equals( currentTag ) ) { columnCount++; } break; case XMLStreamReader.CHARACTERS: if ( "column".equals( currentTag ) ) { String text = parser.getText().trim().replaceAll( "\n" , " "); switch( columnCount ) { case 0: inscrito.setId( Integer.valueOf( text ) ); break; case 1: inscrito.setFirstName( WordUtils.capitalizeFully( text ) ); break; case 2: inscrito.setLastName( WordUtils.capitalizeFully( text ) ); break; case 3: inscrito.setEmail( text ); break; } } break; } } parser.close(); } catch (Exception e) { throw new IllegalStateException(e); } } Collections.sort(inscritos); return inscritos; } public Map&lt;String,List&lt;Inscrito&gt;&gt; parse() { List&lt;Inscrito&gt; inscritos = this.process(); Map&lt;String,List&lt;Inscrito&gt;&gt; resultado = new LinkedHashMap&lt;String, List&lt;Inscrito&gt;&gt;(); for ( Inscrito i : inscritos ) { List&lt;Inscrito&gt; lista = resultado.get( i.getInicial() ); if ( lista == null ) { lista = new ArrayList&lt;Inscrito&gt;(); resultado.put( i.getInicial(), lista ); } lista.add( i ); } return resultado; } } </code></pre> <p>The code itself is in portuguese but it should be straightforward for you to understand what it is, <a href="https://github.com/mauricio/pbjug-inscricoes">here's the repo on github</a>.</p>
    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.
    3. 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