Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are setting <code>EndTextElementListener</code> for <code>&lt;content&gt;</code> elements at the moment the parser actually <em>parse</em> the parent (<code>&lt;article&gt;</code>) element. Simply set all Listeners before you call <code>Xml.parse()</code> method and add condition to everyone of them.</p> <p>I would put it like this:</p> <pre><code>private static Article currentArticle; public Article parse(final String articleTitle) { RootElement root = new RootElement("learning"); Element group = root.getChild("group"); final Element article = group.getChild("article"); article.setStartElementListener(new StartElementListener() { @Override public void start(Attributes attributes) { if (attributes.getValue("name").equals(articleTitle)) { currentArticle = new Article(); currentArticle.setTitle(articleTitle); } else { currentArticle = null; } } }); article.getChild("content").setEndTextElementListener( new EndTextElementListener() { @Override public void end(String body) { if (currentArticle.getTitle().equals(articleTitle)) { currentArticle.setContent(body); } } }); Context context = Application.getAppContext(); InputStream raw; try { raw = context.getAssets().open("learning_articles.xml"); Xml.parse(raw, Xml.Encoding.UTF_8, root.getContentHandler()); } catch (Exception e) { throw new RuntimeException(e); } return currentArticle; </code></pre> <p>Or if you want to parse multiple Articles, add <code>currentArticle</code> to the <code>List</code> at the end of <code>&lt;article&gt;</code> element and return whole Collection instead of just one object.</p>
 

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