Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
tntn
Posts: 102
Registered: ‎03-01-2010
My Device: 8310
Accepted Solution

xml and blackberry application

hello ;

 

 

 

Iam developping an application which manipulate a xml file from blackberry java application ,i have some textField and i want to add the contain of these fields to an xml file as a new tags ,and i want to edit the doc added to modifie it fields will be added in the same texyField i will modifie and save modification in the xml file , i have made somes codes but its not works if there is any tuto or method which works in  BB application can any one send it to me ,i am blocked with this step .

 

 

thank you very much for helping

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: xml and blackberry application

You might find searching this forum useful, I'm sure someone else has asked a similar question.

 

There is an xmldemo supplied with the JDE too.

Trusted Contributor
tntn
Posts: 102
Registered: ‎03-01-2010
My Device: 8310

Re: xml and blackberry application

hello;

 

 

i have made this code :

 

 

public class AddDoc extends MainScreen{
    private Element createBookElement(Document dom,String name1,String name2,String name3){

        Element bookEle = dom.createElement("Book");
        bookEle.setAttribute("Subject",name1);

        //create author element and author text node and attach it to bookElement
        Element authEle = dom.createElement("Author");
        Text authText = dom.createTextNode(name2);
        authEle.appendChild(authText);
        bookEle.appendChild(authEle);
        //create title element and title text node and attach it to bookElement
        Element titleEle = dom.createElement("Title");
        Text titleText = dom.createTextNode(name3);
        titleEle.appendChild(titleText);
        bookEle.appendChild(titleEle);

        return bookEle;

    }

    
    public AddDoc() throws Exception
    
    {
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = fac.newDocumentBuilder();
        InputStream  inputStream = getClass().getResourceAsStream( "dat.xml" );
        Document document = builder.parse( inputStream );
 
  
    Element root = document.getDocumentElement();
    Element book=createBookElement(document,"BB","dd",xx");

   try

{
   
     root.appendChild(book);  
 
    document.normalizeDocument();

 }
   catch(DOMException n)

{
        Status.show(n.getMessage());
 
   }

}

 

i have and xml file which  is dat.xml and which contain <database></database>

 

when i execute  informations dosn't saved in the xml file .

can you tell me if i missed somthing ???

thank you in advance

Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: xml and blackberry application

I don't see where you are saving anything. You only have an inputstream created from a resource, right?

 

You cannot "save" a resource, even if you wrote code to do so. A resource file is part fo your COD distribution and is immutable.

 

If you want to save the resulting file someplace, you'll need to open a FileConnection, or possibly persist the value as a String or possibly a byte array in the PersistentStore.

 

 

Trusted Contributor
tntn
Posts: 102
Registered: ‎03-01-2010
My Device: 8310

Re: xml and blackberry application

hello ;

 

i know that i desturb you but i am sooo sorry i have not other choice : if any one have any experiences in that ,plz help me : so i try to add  data to the xml file :

 

this is my code :

 

package com;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;



import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.jaxp.DOMInternalRepresentation;
import net.rim.device.api.xml.jaxp.XMLWriter;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;


public class AddDoc extends MainScreen{
    /*private Element createBookElement(Document dom,String name1,String name2,String name3){

        Element bookEle = dom.createElement("Book");
        bookEle.setAttribute("Subject",name1);

        //create author element and author text node and attach it to bookElement
        Element authEle = dom.createElement("Author");
        Text authText = dom.createTextNode(name2);
        authEle.appendChild(authText);
        bookEle.appendChild(authEle);

        //create title element and title text node and attach it to bookElement
        Element titleEle = dom.createElement("Title");
        Text titleText = dom.createTextNode(name3);
        titleEle.appendChild(titleText);
        bookEle.appendChild(titleEle);

        return bookEle;

    }*/

    
    public AddDoc() throws Exception{
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = fac.newDocumentBuilder();
        InputStream  inputStream = getClass().getResourceAsStream( "dat.xml" );
      Document document = builder.parse( inputStream );
    
    // Normalize the root element of the XML document.  This ensures that all Text
    // nodes under the root node are put into a "normal" form, which means that
    // there are neither adjacent Text nodes nor empty Text nodes in the document.
    // See Node.normalize().
   Element root = document.getDocumentElement();
    
  /*  NodeList elementsTitre = root.getElementsByTagName("item");
    Element elementTitre = (Element)elementsTitre.item(0);
    Text texteTitre = (Text)elementTitre.getFirstChild();
    texteTitre.setData("am");
    // Auteur du livre
   
    Element elementAuteur = (Element)elementsTitre.item(1);
    Text texteAuteur = (Text)elementAuteur.getFirstChild();
    texteAuteur.setData("da");
    // Description du livre
 
    Element elementDescription = (Element)elementsTitre.item(2);
    // Supprime et recrée la description
   
    root.removeChild(elementDescription);
    elementDescription = document.createElement("description");
    Text texteDescription = document.createTextNode("blabla");
    elementDescription.appendChild(texteDescription);
    root.appendChild(elementDescription);*/
    
    
   // Element book=createBookElement(document,"iss","tara","tntn");
    //document.insertBefore(book, root);*/

       //root.appendChild(book);

       
      FileConnection cfile;
      cfile=(FileConnection) Connector.open("dat.xml",Connector.READ_WRITE,true);
      add(new RichTextField("file opened"));
     OutputStream dos= cfile.openOutputStream();
     ByteArrayOutputStream ba=new ByteArrayOutputStream();
      XMLWriter writer=new XMLWriter(ba);
      writer.setPrettyPrint();
      DOMInternalRepresentation.parse(document,writer);
     byte bXml[]=ba.toByteArray();
     dos.write(bXml);
     
    /*OutputStream out=null;
    XMLWriter writer= new XMLWriter(out);
    writer.startDocument();*/
 
add(new RichTextField(root.getNodeName()));
// add(new RichTextField(book.getNodeName()));
 //add(new RichTextField(book.getFirstChild().getNodeName()));

 
 
 //add(new RichTextField(root.getFirstChild().getNodeName()));
  /* Element Docu=document.createElement("Document");
   
    Docu.setTextContent("Document");
    Docu.setAttribute("form", "Affaire");
     
   
 
    Element item1=document.createElement("item");                        
    item1.setTextContent("item");      
    item1.setAttribute("name", "RSociete");   
    item1.setNodeValue("bloblo");          
   

    Element item2=document.createElement("item");                     
    item2.setTextContent("item");  
    item2.setAttribute( "name", "RDirection");                      
    item2.setNodeValue("gt");  
    
    Docu.appendChild( item1);
    Docu.appendChild(item2);
    root.appendChild(Docu);
    document.appendChild(Docu);*/
       
      
    }
}

this gfive me this exception : illegualArgument Exception !!!!

can any one examine with me this ??

 

 

 

 

thank you very much  ;

 

 

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: xml and blackberry application

Hi. This forum exists to disturb us, so don't be sorry!

 

Just skimming your code, I think the line:

cfile=(FileConnection) Connector.open("dat.xml",Connector.READ_WRITE,true);

might be causing the exception. The Connector.open method requires that the first argument be a string with the format:

{scheme}:[{target}][{parms}]

where {scheme} is the name of a protocol and is required. Your argument, "dat.xml", doesn't have a scheme. Since you're casting to a FileConnection, the protocol should be "file". The target also has to be an absolute path name. A typical target for a file stored in device memory would be:

"file:///store/home/user/dat.xml"

For the SDCard, a typical target would be:

"file:///SDCard/BlackBerry/dat.xml"

You can determine the file system roots using FileSystemRegistry.listRoots().




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Trusted Contributor
tntn
Posts: 102
Registered: ‎03-01-2010
My Device: 8310

Re: xml and blackberry application

Hello ;

 

YES ,thank you very much for helping  ,now it's works very well :smileyhappy:

 

i have onther question if its possible , i have an xml file in my computer ,and i want to put it into SDCARD  memory for my blackberry simulator (JDE 4.7 as plug-in with eclipse) ,how can i put it  ?? ,i can consult all over the files located in this sdcard with this url : i open simulator ,i go to files menu and open all my file.xml .

 

thanks in advance

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: xml and blackberry application

I'm glad it's working. Could you mark this thread as solved by clicking on "Accept as solution" on the post that solved your problem.

 

If you're using the latest plug-in, you can open the run/debug configurations, select the Simulator tab, and the Memory tab underneath that. At the bottom, you can check the option "Use PC file system for SD Card files" and enter a directory on your PC to use for the simulated SD card. Then you can just copy your xml file into that directory structure.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.