Search code examples
xmlblackberryfile-iojava-me

Better approach for XML Creation in Blackberry


I have created XML file,but I can't view it/output it.I know there is no way to output created XML file.

Can anyone please suggest what is better way of creating xml files? 1. create xml with DocumentBuilderFactory and then parse it Or 2. manually create hardcoded xml and save it on sd card and then access it for parsing.

I have continuosly varying text data in xml files. Which approach will suite me most?


Solution

  • I'm using kXML2 to create/chage/save/read xml. Using it with BlackBerry remember:
    - for release you have to preverify it & build proj with ant
    Ahmad Ferdous Bin Alam - How to Import kxml jar File to Your Project
    Slashdev - BlackBerry Development with Ant & Eclipse
    UPDATE: Tutorial: How To Use 3rd Party Libraries in your Applications
    - for debug you have to add kXML sources and org.xmlpull.v1 sources to your BB project

    Create XML

        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);
    

    Save XML on BlackBerry filesystem

    Read XML file

        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();
        }
    

    See also: RoseIndia.net - J2ME Kxml Example

    Change XML document

    All you have to do is get needed element and change it:

        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");
    

    Output XML document to BlackBerry screen (somewhere in Screen class)

    Simply serialize xml doc to string and put it in RichTextField:

        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()));