2

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?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
iOSDev
  • 3,617
  • 10
  • 51
  • 91

3 Answers3

15

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()));
Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
  • @MaxGontar hi nice ans. but i have one query i have a simple xml ( Ques :http://stackoverflow.com/questions/15361763/read-write-and-create-local-xml-file).i have add this xml to sd card and i am reading this xml file successfully . Now i want to remove Node from NodeList and want to reflected that change to my xml file. so how can i achive this (remove and add node than save this file )? Please help me if you have any idea – Hitarth Mar 14 '13 at 09:27
1

What about using the DOMInternalRepresentation class? Despite its name, it's part of the public API and has existed since JDE 4.0.0. Using the DOMInternalReprestationClass you can write the Document to an XMLWriter.

For example, to write to a ByteArrayOutputStream (making it handy to send over a Connection):

ByteArrayOutputStream os = new ByteArrayOutputStream();
XMLWriter xw = new XMLWriter(os);
DOMInternalRepresentation.parse( myDocument, xw );

Naturally, you can use a FileOutputStream to direct the XML output to a file on disc instead of to a byte-array.

(If you can't use external libraries for some reason, this seems like a pretty viable approach)

Skrud
  • 11,604
  • 5
  • 24
  • 22
  • It is impossible to set encoding of the document via the `org.w3c.dom.Document` class. There is a getter (`getXmlEncoding()`) but there is no setter. On the other hand, if one is using [kXML2](http://kxml.sourceforge.net/) it is as easy as `setEncoding` on the `org.kxml2.kdom.Document' class. – tonymontana Feb 07 '12 at 00:58
-1

you can also use net.rim.device.api.xml.jaxp.XMLWriter but it needs to write a whole page of code to generate a unless I missed something ?

RzR
  • 3,068
  • 29
  • 26