×

Discussion Board

Results 1 to 10 of 10

Thread: xml with j2me

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    33

    xml with j2me

    Hi folks ,

    Im a biginner in xml parsing .I have my "spin.xml" file in E dir. Im using Netbeans 5.0 for my game development. i want to parse the xml file to get its content into my mobile or emulator. Please tell me the basic steps i have to follow. I got one sample program for xml parsing which uses

    FileConnection fc = (FileCOnnection)Connector.open("file:///root1/helloword.xml");

    So i replaced it by

    FileConnection fc = (FileCOnnection)Connector.open("file:///E:/spin.xml");

    But its throwing IO EXP: "Root not accesible"

    plz suggest where(which folder) should i store the xml file to get it in emulator and phone.

    thanks in advance....

    yafy.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: xml with j2me

    Hi yafi,

    Well, if your target device implements JSR-75 then you should go that way. Otherwise, include you XML file as a resource within your JAR file.

    Regarding your doubts related to directories and files i advise you to read this doc:

    Introduction To The FileConnection API (With Example) v1.1
    http://www.forum.nokia.com/info/sw.n..._v1_1.zip.html

    Take a look at the doc below for good information regarding XML parsing in JME:

    XML in J2ME
    http://trix2.cellmania.com/downloads...E_20060301.pdf

    Forum Nokia also has a document that briefly discuss the JSR-172 XML parsing optional package (the standard way). Check it out, page 6.

    MIDP: Web Services API Developer's Guide (With Example)
    http://www.forum.nokia.com/info/sw.n..._v1_0.zip.html

    I hope it helps!

    BR,

    Juarez

  3. #3
    Super Contributor
    Join Date
    Mar 2006
    Location
    Phoenix, AZ. USA
    Posts
    556

    Re: xml with j2me

    and if you are using netbeans and WTK emualtor, you need to find the emulators root file system on your PC's file system

    for example in Win2k and XP, mine is
    D:\Documents and Settings\Jay\.netbeans\4.1\emulators\wtk22_win\emulator\wtk22\appdb\DefaultColorPhone\filesystem\root1

    using e of your hard drive, wont work, if thats what your trying, note the bolded above, this is where the emulators file system begins

    so you would place your xml file in there
    Jason Glass
    http://IChiBanComputers.Com

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Thumbs up Re: xml with j2me

    spin.xml
    ========
    <details>
    <title>abc</title>
    <description>hai</description>
    </details>




    ParseXML.java
    =============

    import java.io.*;
    import org.kxml.*;
    import org.kxml.parser.*;
    import javax.microedition.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.Displayable;

    public class ParseXML extends MIDlet implements CommandListener {
    private Command exit;
    private Command xml;
    private Display display;
    private static TextBox t;
    private static String textBoxString = "";
    private String xmlStr = "/spin.xml";


    public ParseXML() {
    display = Display.getDisplay( this );
    exit = new Command( "Exit", Command.EXIT, 2 );
    xml = new Command( "XML", Command.SCREEN, 1 );

    }

    public void startApp() {
    t = new TextBox( "MIDlet XML", "kXML", 256, 0 );
    t.addCommand( exit );
    t.addCommand( xml );
    t.setCommandListener( this );
    display.setCurrent( t );
    }

    public void pauseApp() { }

    public void destroyApp(boolean unconditional) { }

    public void commandAction(Command c, Displayable s) {
    if ( c == exit ) {
    destroyApp( false );
    notifyDestroyed();
    } else if ( c == xml ) {
    try {
    viewXML();
    t.removeCommand(xml);
    } catch( Exception e ) {
    e.printStackTrace();
    }
    }
    }

    public void viewXML() throws IOException {
    try {
    byte[] xmlByteArray = xmlStr.getBytes();
    ByteArrayInputStream xmlStream = new ByteArrayInputStream( xmlByteArray );
    InputStreamReader xmlReader = new InputStreamReader( xmlStream );
    XmlParser parser = new XmlParser( xmlReader );
    try {
    traverse( parser, "" );
    } catch (Exception exc) {
    exc.printStackTrace();
    }
    return;
    } catch ( IOException e ) {
    return ;
    } finally {
    return ;
    }
    }

    public static void traverse( XmlParser parser, String indent ) throws Exception{
    boolean leave = false;
    String title = new String();
    String desc = new String();
    do {
    ParseEvent event = parser.read ();
    ParseEvent pe;
    switch ( event.getType() ) {
    case Xml.START_TAG:
    if ("title".equals(event.getName())){
    pe = parser.read();
    title = pe.getText();
    }

    if ("description".equals(event.getName())){
    pe = parser.read();
    desc = pe.getText();
    }
    textBoxString = title + " " + desc;
    traverse( parser, "" ) ;
    break;

    case Xml.END_TAG:
    leave = true;
    break;

    case Xml.END_DOCUMENT:
    leave = true;
    break;

    case Xml.TEXT:
    break;

    case Xml.WHITESPACE:
    break;

    default:
    }
    } while( !leave );

    t.setString( textBoxString );
    }

  5. #5
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: xml with j2me

    hehe,
    you did notice it was a 3 year old thread right ? ;)

  6. #6
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: xml with j2me

    Hi,
    try using the SAX parser.

    Thanks
    Soku

  7. #7
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: xml with j2me

    lol,
    I guess some people just dont read :P

  8. #8
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: xml with j2me

    Quote Originally Posted by Tiger79 View Post
    lol,
    I guess some people just dont read :P
    Tiger,rather they keep on positng
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  9. #9
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    387

    Re: xml with j2me

    Try this, use kxml2


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.397.79
    */

    import java.io.InputStreamReader;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import org.kxml2.io.KXmlParser;
    import org.xmlpull.v1.XmlPullParser;

    /**
    * @author Manjul
    */
    public class AddressMidlet extends MIDlet implements Runnable
    {
    InputStreamReader ip;
    KXmlParser parser;
    Thread t;
    String tag,text;

    public void startApp()
    {
    t=new Thread(this);
    t.start();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void run()
    {
    try{
    ip= new InputStreamReader(this.getClass().getResourceAsStream("/address.xml"));
    parser= new KXmlParser();
    parser.setInput(ip);

    parser.nextTag();
    parser.require(XmlPullParser.START_TAG, null, "address_book");
    System.out.println("111111111");
    while(parser.nextTag()!=XmlPullParser.END_TAG)
    {
    parser.require(XmlPullParser.START_TAG, null , "address");

    while(parser.nextTag()!=XmlPullParser.END_TAG)
    {
    tag=parser.getName();
    text=parser.nextText();
    System.out.println(tag+" <<<::>>> "+text);
    parser.require(XmlPullParser.END_TAG, null , tag);
    }
    }

    parser.require(XmlPullParser.END_TAG,null, "address_book");
    parser.next();
    parser.require(XmlPullParser.END_DOCUMENT,null,null);
    }catch(Exception e){System.out.println("errorrrrr "+e);e.printStackTrace();}
    }

    }


    address.xml look like this


    <address_book>
    - <address>
    <name>Robert Cadena</name>
    <street_1>5555 Fake Street</street_1>
    <street_2>Suit 202</street_2>
    <city>Fake City</city>
    <state>CA</state>
    <zipcode>900099</zipcode>
    </address>
    - <address>
    <name>Fake Name</name>
    <street_1>3232 Another Street</street_1>
    <city>Another City</city>
    <state>TX</state>
    <zipcode>79999</zipcode>
    </address>
    - <address>
    <name>Manjul</name>
    <street_1>1119/5</street_1>
    <city>Gurgaon</city>
    <state>Haryana</state>
    <zipcode>122055</zipcode>
    </address>
    </address_book>
    Thanks and Regards,
    Manjul.

  10. #10
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    387

    Smile Re: xml with j2me


    Hi all,

    this is common mistake done by every one, guys keep on accessing file system simply using File(.....)

    it is only possible by using jsr75 else not.

    or supply a file with jar.


    Thanks and Regards,
    Manjul.

Similar Threads

  1. Using kxml package in J2ME!! PLEASE HELP
    By ajayluthria in forum Mobile Java General
    Replies: 3
    Last Post: 2007-03-21, 05:43
  2. help on linking XML databases to J2ME.
    By yenpaul2001 in forum Mobile Java General
    Replies: 0
    Last Post: 2006-01-22, 18:43
  3. XML Schema parser for J2ME???
    By Pepper_91 in forum Mobile Java General
    Replies: 1
    Last Post: 2005-10-11, 00:37
  4. download/upload xml from-to server in j2me
    By freezenik in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2005-01-26, 05:00
  5. J2ME Polish Version 1.0 RC6
    By enough in forum Mobile Java General
    Replies: 1
    Last Post: 2004-07-20, 11:25

Posting Permissions

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