Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm using <a href="http://kxml.sourceforge.net/" rel="noreferrer">kXML2</a> to create/chage/save/read xml. Using it with BlackBerry remember:<br> - for release you have to preverify it &amp; build proj with ant<br> <a href="http://ahmadferdous.blogspot.com/2009/03/how-to-import-kxml-jar-file-to-your.html" rel="noreferrer">Ahmad Ferdous Bin Alam - How to Import kxml jar File to Your Project</a><br> <a href="http://www.slashdev.ca/2007/05/30/blackberry-development-with-ant-eclipse/" rel="noreferrer">Slashdev - BlackBerry Development with Ant &amp; Eclipse</a><br> <strong>UPDATE:</strong> <a href="http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&amp;message.id=22441" rel="noreferrer">Tutorial: How To Use 3rd Party Libraries in your Applications</a><br> - for debug you have to add <a href="http://sourceforge.net/project/downloading.php?group_id=9157&amp;filename=kxml2-src-2.3.0.zip&amp;a=104174" rel="noreferrer">kXML sources</a> and <a href="http://www.xmlpull.org/v1/download/xmlpull_1_1_3_4c_src.zip" rel="noreferrer">org.xmlpull.v1 sources</a> to your BB project </p> <h2>Create XML</h2> <pre><code> Document d = new Document(); Element root = d.createElement("", "parent"); root.setName("catalog"); Element book = d.createElement("", "child"); book.setName("book"); book.setAttribute(null, "id", "1"); Element author = d.createElement("", "child"); author.setName("author"); author.addChild(0, Node.TEXT, "Colin Wilson"); book.addChild(0, Node.ELEMENT, author); Element title = d.createElement("", "child"); title.setName("title"); title.addChild(0, Node.TEXT, "The Mind Parasites"); book.addChild(1, Node.ELEMENT, title); Element genre = d.createElement("", "child"); genre.setName("genre"); genre.addChild(0, Node.TEXT, "Horror novel, Science fiction novel"); book.addChild(2, Node.ELEMENT, genre); Element publishDate = d.createElement("", "child"); publishDate.setName("publish-date"); publishDate.addChild(0, Node.TEXT, "1967"); book.addChild(3, Node.ELEMENT, publishDate); root.addChild(0, Node.ELEMENT, book); d.addChild(root.ELEMENT, root); </code></pre> <h2>Save XML on BlackBerry filesystem</h2> <ul> <li>If use emulator don't forget to emulate SD card (Tools->Change SD Card...) </li> <li><p>be sure you have access rights for read/write operation </p> <pre><code>String fileName = "file:///SDCard/books.xml"; DataOutputStream os = null; FileConnection fc = null; try { fc = (FileConnection) Connector.open(fileName, Connector.READ_WRITE); if (!fc.exists()) fc.create(); os = fconn.openDataOutputStream(); KXmlSerializer serializer = new KXmlSerializer(); serializer.setOutput(os, "UTF-8"); d.write(serializer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>See also:<br> <a href="http://www.blackberry.com/developers/docs/4.7.0api/javax/microedition/io/file/FileConnection.html" rel="noreferrer">RIM API - Interface FileConnection</a><br> <a href="http://developers.sun.com/mobility/apis/articles/fileconnection/index.html" rel="noreferrer">SUN Dev Network - Getting Started with the FileConnection APIs</a><br> <a href="http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800620/How_To_-_Add_plain_text_or_binary_files_to_an_application.html?nodeid=800687&amp;vernum=0" rel="noreferrer">RIM - How To - Add plain text or binary files to an application</a><br> <a href="http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&amp;message.id=26417" rel="noreferrer">BB Support Forum - Some questions about FileConnection/JSR 75</a><br> <a href="http://developer.sonyericsson.com/community/message/126441%3Bjsessionid=5E406A0EC79BB5AE926DBD85E3D65997" rel="noreferrer">Sony Ericsson Forum - Create XML file</a> </p></li> </ul> <h2>Read XML file</h2> <pre><code> Document d= new Document(); FileConnection fc = null; DataInputStream is = null; try { fc = (FileConnection) Connector.open(fileName, Connector.READ); is = fc.openDataInputStream(); KXmlParser parser = new KXmlParser(); parser.setInput(is, "UTF-8"); d.parse(parser); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>See also: <a href="http://www.roseindia.net/j2me/kxml/j2me-xml-parser.shtml" rel="noreferrer">RoseIndia.net - J2ME Kxml Example</a></p> <h2>Change XML document</h2> <p>All you have to do is get needed element and change it: </p> <pre><code> Element catalog = d.getElement("", "catalog"); Element book = catalog.getElement("", "book"); Element title = book.getElement("", "title"); title.removeChild(0); title.addChild(Element.TEXT, "Spider World: The Tower"); Element publish = book.getElement("", "publish-date"); publish.removeChild(0); publish.addChild(Element.TEXT, "1987"); </code></pre> <h2>Output XML document to BlackBerry screen (somewhere in Screen class)</h2> <p>Simply serialize xml doc to string and put it in RichTextField: </p> <pre><code> deleteAll(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); KXmlSerializer serializer = new KXmlSerializer(); try { serializer.setOutput(baos, "UTF-8"); d.write(serializer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } add(new RichTextField(baos.toString())); </code></pre>
 

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