Search code examples
xmljava-mekxml2

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


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??


Solution

  • 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.