| Reply | « Previous Thread | Next Thread » |
|
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. |
|
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 |
|
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 |
|
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 ); } |
| csaimohanb |
| View Public Profile |
| Find all posts by csaimohanb |
|
hehe,
you did notice it was a 3 year old thread right ? ;) |
|
Hi,
try using the SAX parser. Thanks Soku |
|
lol,
I guess some people just dont read :P |
|
Join Date: Mar 2008
Posts: 2,161
Location: The Capital of I N D I A
raj_J2ME
Offline
Forum Nokia Champion
|
|
Tiger,rather they keep on positng
Thanks R a j - The K e r n e l |
|
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. |
| manjul_saini |
| View Public Profile |
| Find all posts by manjul_saini |
|
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. |
| manjul_saini |
| View Public Profile |
| Find all posts by manjul_saini |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| Using kxml package in J2ME!! PLEASE HELP | ajayluthria | Mobile Java General | 3 | 2007-03-21 06:43 |
| help on linking XML databases to J2ME. | yenpaul2001 | Mobile Java General | 0 | 2006-01-22 19:43 |
| XML Schema parser for J2ME??? | Pepper_91 | Mobile Java General | 1 | 2005-10-11 01:37 |
| download/upload xml from-to server in j2me | freezenik | Mobile Java Networking & Messaging & Security | 0 | 2005-01-26 06:00 |
| J2ME Polish Version 1.0 RC6 | enough | Mobile Java General | 1 | 2004-07-20 12:25 |