×

Discussion Board

Results 1 to 9 of 9

Thread: kxml2 and J2ME

  1. #1
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    52

    kxml2 and J2ME

    Hello. I am working with kxml2 in order to modify a xml document. My aim is to add some new fields when pressing a command button of my MIDLET.
    I got taking the data from the file and display it on my MIDLET. However, i dont know how to modify the content of the xml document from my midlet.

    I will aprecciate some code examples that could help me to solve this.

    Thanks in advance

    nadiuska

  2. #2
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: kxml2 and J2ME

    Quote Originally Posted by nadiuska View Post
    Hello. I am working with kxml2 in order to modify a xml document. My aim is to add some new fields when pressing a command button of my MIDLET.
    I got taking the data from the file and display it on my MIDLET. However, i dont know how to modify the content of the xml document from my midlet.

    I will aprecciate some code examples that could help me to solve this.

    Thanks in advance

    nadiuska

    Hi there will be some methods to add xml element in it. Did you read this article

  3. #3
    Regular Contributor
    Join Date
    Sep 2007
    Location
    Thirunelveli
    Posts
    115

    Re: kxml2 and J2ME

    Hi,
    please try this:
    Document doc = new Document();
    XmlSerializer serializer = new KXmlSerializer();
    StringWriter writer = new StringWriter();
    serializer.setOutput(writer);
    doc.write(serializer);

    Element element = doc.getRootElement();
    element.setName("ROOT");
    Element childElement = doc.createElement(null, "CHILD_ELEMENT");

    element.addChild(Node.ELEMENT, CHILD_ELEMENT);
    Regards
    Senthil

  4. #4
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    52

    Re: kxml2 and J2ME

    Thanks a lot for your replies. However i had some problems with them.

    <senthil_k> StringWriter is not available in J2ME,at least in java.io.StringWriter.

    <honest_success> good examples,but they do not show how to modify the data from the xml file.

    The fact is that i know how to make a file in J2ME,as follows:

    try{

    FileConnection fc = (FileConnection) Connector.open(path,Connector.READ_WRITE);
    if (!fc.exists())
    fc.create();

    OutputStream os = fc.openOutputStream();
    String xmlText="any text...";
    byte data[]=xmlTest.getBytes();
    os.write(data);
    fc.close();

    }
    catch(Exception e){
    e.printStackTrace();
    }
    And i also know how to serialize a document with KXML2,as follows:

    Document doc = new Document();
    doc.setEncoding("ISO-8859-1");
    XmlSerializer serializer = new KXmlSerializer();
    Element listaRepartos = doc.createElement("","ListaRepartos");
    listaRepartos.setAttribute(null,"id","1");
    doc.addChild(Node.ELEMENT, listaRepartos);
    ...
    StringWriter is the problem,as it is not available in J2ME
    /*StringWriter writer=new StringWriter();
    serializer.setOutput(writer);
    doc.write(serializer);
    */

    But i do not know how to link both of them!

    Thanks for the help

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: kxml2 and J2ME

    Hmmm... I haven't used KXML for years... but how about:

    PHP Code:
    Document doc = new Document();

    // more code...

    OutputStream out fc.openOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(out);
    try {
        
    XmlSerializer serializer = new KXmlSerializer();
        
    serializer.setOutput(writer);
        
    doc.write(serializer);
    finally {
        
    writer.close();

    Any help?

    Cheers,
    Graham.

  6. #6
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    52

    Re: kxml2 and J2ME

    Thank u very much. However,i get a NullPointerException in OutputStream os. This is the code:

    try{
    String path="file:///C:/Data/Images/";
    FileConnection fc = (FileConnection) Connector.open(path+"oleola.xml", Connector.READ_WRITE);
    if (!fc.exists())
    fc.create();
    OutputStream os = fc.openOutputStream(); //HERE NULL POINTEREXCEPTION
    writer = new OutputStreamWriter(os);
    serializer.setOutput(writer);
    doc.write(serializer);
    }
    finally {
    writer.close();
    }

    Thanks

  7. #7
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    52

    Re: kxml2 and J2ME

    My mistake.It has worked! I forgot deleting wrong code that i had before.

    I put the entire code so that other developers can use it.

    try {

    //Create the XML Document structure
    Document doc = new Document();
    doc.setEncoding("ISO-8859-1");

    XmlSerializer serializer = new KXmlSerializer();

    Element principal = doc.createElement("","ListaRepartos");
    listaRepartos.setAttribute(null,"id","1");
    doc.addChild(Node.ELEMENT, principal);

    //Create the output file which will be located in path
    OutputStreamWriter writer=null;
    FileConnection fc=null;
    try{
    String path="file:///C:/Data/Images/";
    fc = (FileConnection) Connector.open(path+"oleola.xml", Connector.READ_WRITE);
    if (!fc.exists())
    fc.create();
    OutputStream os = fc.openOutputStream();
    writer = new OutputStreamWriter(os);
    serializer.setOutput(writer);
    doc.write(serializer);
    }
    finally {
    writer.close();
    fc.close();
    }

    }
    catch (Exception exception) {
    exception.printStackTrace();

    }

    Thanks guys!!

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    22

    Re: kxml2 and J2ME

    Thanks for your sample !
    It's helping me a lot !

    By any chance, do you know how to write line breaks using your method ?
    Thank you for your help !

    David

  9. #9
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    7

    Re: kxml2 and J2ME

    Quote Originally Posted by nadiuska View Post
    My mistake.It has worked! I forgot deleting wrong code that i had before.

    I put the entire code so that other developers can use it.

    try {

    //Create the XML Document structure
    Document doc = new Document();
    doc.setEncoding("ISO-8859-1");

    XmlSerializer serializer = new KXmlSerializer();

    Element principal = doc.createElement("","ListaRepartos");
    listaRepartos.setAttribute(null,"id","1");
    doc.addChild(Node.ELEMENT, principal);

    //Create the output file which will be located in path
    OutputStreamWriter writer=null;
    FileConnection fc=null;
    try{
    String path="file:///C:/Data/Images/";
    fc = (FileConnection) Connector.open(path+"oleola.xml", Connector.READ_WRITE);
    if (!fc.exists())
    fc.create();
    OutputStream os = fc.openOutputStream();
    writer = new OutputStreamWriter(os);
    serializer.setOutput(writer);
    doc.write(serializer);
    }
    finally {
    writer.close();
    fc.close();
    }

    }
    catch (Exception exception) {
    exception.printStackTrace();

    }

    Thanks guys!!

    Thanks a lot..it is helping me

Similar Threads

  1. Parsing xml data from the server using kxml2 in J2ME
    By dninsiima in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2010-03-26, 08:08
  2. how to make kxml2 parse in phones
    By kibreab in forum Mobile Java General
    Replies: 5
    Last Post: 2009-01-11, 03:08
  3. how to parse wbxml file in j2me midp?
    By ramyaas in forum Mobile Java General
    Replies: 6
    Last Post: 2007-06-15, 04:18
  4. problem kxml, kxml2 class not found
    By kmsoomro in forum Mobile Java General
    Replies: 0
    Last Post: 2005-02-22, 08:55

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×