1

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

Sin
  • 1,836
  • 2
  • 17
  • 24

2 Answers2

1

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.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • `fc = (FileConnection) Connector.open(""); in = fc.openInputStream(); parser.setInput(new InputStreamReader(in)); ` **this is working and now i can read file from the memory. Any help regarding Editing this xml, using kXML or other, is available??** – Sin Jul 16 '12 at 07:28
  • sorry, am new to this. I need to update values for the nodes in that xml file. for parsing i used kXML. – Sin Jul 16 '12 at 07:36
  • actually am using this mobile app as a supporting app for my PC version. so the xml file will be generated by the PC ver and this XML is loaded to the phone. the user should be able to update the values via phone. – Sin Jul 16 '12 at 07:41
  • 1
    then you can directly read your xml file from your pc, if your pc has live ip. – Lucifer Jul 16 '12 at 07:45
  • no boss, consider my xml contains some names, the user could edit these names via phone and when loaded back to PC the changes should reflect. now searching is ready. U got my question? – Sin Jul 16 '12 at 07:51
  • simple Lucifer is fine, ok so you want to read the xml and the edit the xml and the upload it to pc back right ? i guess better you ask a new question now. – Lucifer Jul 16 '12 at 07:53
0

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.

Telmo Pimentel Mota
  • 4,033
  • 16
  • 22