to read XML file from phone/card memory instead from .jar file in J2ME

688 Views Asked by At

i can parse an xml file when it is added to the resource using

InputStream in = getClass().getResourceAsStream("/data.xml"); 
KXmlParser parser = new KXmlParser();
parser.setInput(new InputStreamReader(in));

But i need to know how to read the xml file using kXML when it is kept outside the jar like (phone/card) memory, also how to edit that using kXML??

2

There are 2 best solutions below

1
On BEST ANSWER

If your target devices support SAX from JSR 172, you should go with this parser instead of kXML. It will reduce your app's final jar size (no libs imported). There is a good sample at http://www.developer.nokia.com/Community/Wiki/JSR_172:_XML_Parsing_Example It actually unmarshalls the XML into Java objects. For your case, instead of initiating InputStream in = getClass().getResourceAsStream("/JSR172Demo.xml"); you should use FileConnection API.

Then you change the objects attributes as needed.

To write your content back to XML you can override toString method making it create the XML String you need.

6
On

From your question, I am assuming that you want to read an .xml file from either phone memory or from sd card. Here an .xml file behaves as simple text file, so you need to know the File Connection API for your requirement. Visit How to read text files.