Skip to main content

how to read xml file that stored in folder

4 replies [Last post]
rashmitasahoo
Offline
Joined: 2010-10-26
Points: 0

Hello,

I have an application that have an data folder(user defined folder) in which i stored some xml files. Now i write code for xml parser to parse the xml file and get data of it so i can use in my application.

 <font color="#000000">public</font> <font color="#000000">void</font> <font color="#000099">parse</font>(<font color="#000000">final</font> String url) <font color="#000000">{</font>
    Thread t <font color="#000000">=</font> <font color="#000000">new</font> <font color="#000099">Thread</font>() <font color="#000000">{</font>
      <font color="#000000">public</font> <font color="#000000">void</font> <font color="#000099">run</font>() <font color="#000000">{</font>
        <font color="#009900"><em>// set up the network connection</em></font>
        HttpConnection hc <font color="#000000">=</font> <font color="#404040">null</font>;
       
        <font color="#000000">try</font> <font color="#000000">{</font>
          hc <font color="#000000">=</font> (HttpConnection)Connector.<font color="#000099">open</font>(url);
          <font color="#000099">parse</font>(hc.<font color="#000099">openInputStream</font>());
        <font color="#000000">}</font>
        <font color="#000000">catch</font> (IOException ioe) <font color="#000000">{</font>
          mRSSListener.<font color="#000099">exception</font>(ioe);
        <font color="#000000">}</font>
        <font color="#000000">finally</font> <font color="#000000">{</font>
          <font color="#000000">try</font> <font color="#000000">{</font> <font color="#000000">if</font> (hc <font color="#000000">!=</font> <font color="#404040">null</font>) hc.<font color="#000099">close</font>(); <font color="#000000">}</font>
          <font color="#000000">catch</font> (IOException ignored) <font color="#000000">{}</font>
        <font color="#000000">}</font>
      <font color="#000000">}</font>
    <font color="#000000">}</font>;
    t.<font color="#000099">start</font>();
  <font color="#000000">}  In th above code as you see that after run() method start then you go to the set up the network connection with HttpConnection protocol. But i want to use my stored xml file in the application to parsing. So it is possible? Here is HttpConnection is used but what i can use to call  my xml file that was stored in my application instead of HttpConnection.  Please help me as soon as earlier. and sorryyyyyyyy for my poor and horrible english.  Advance Thank You!    <br /></font>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
asims
Offline
Joined: 2011-04-14
Points: 0

It depends if you intend to write your own text parser using java, or use existing XML parsing classes integrated into Java. Some questions you need to answer:

Can the entire XML document be read, and then parsed? Or is the XML document to be read by a stream and parsed character by character for best performance?

If you intend to write your own non-standard XML parser, is it for a single XML document format?

Will you be parsing depth structure of nested children? Is ordering of data elements important to the structure?

If you intend to use JDK in box XML parsing functionality which version of the JDK are you using? Each major JDK version tends to have more enhanced XML functionality.

To parse an XML Stream chunk by chunk then implement a SAX parser for best performance. If document format, and structure validation is important go with DOM although its much less efficient and more memory and resource intensive. Stax parsing is a balance between both SAX and DOM that performs better then DOM, yet not as well as SAX.

More information is needed to answer this better. If you want to parse a particular XML document post an example document for people to post code examples using each method.

Thanks,

Aaron

rashmitasahoo
Offline
Joined: 2010-10-26
Points: 0

Hello

And Thanks for your reply. But I can't get it so please help me.

I create my own xml file that is look like it

<AddressBook>
<Person>
<Name>Raj</Name>
<IsDeleted>1</IsDeleted>
</Person>
<Person>
<Name>Pooja</Name>
<IsDeleted>0</IsDeleted>
</Person>
<Person>
<Name>Nimesh</Name>
<IsDeleted>1</IsDeleted>
</Person>
</AddressBook>

Now this xml file is saved as name addressBook.xml in my lwuit Application(Named as XMLDemo) that was created by Latest Resourse Editor GUI Theme Editor in which Application->Generate Netbeans Project->Please Pick Main Screen is used. In XMLDemo Project i placed my addressBook.xml file in src folder of main root folder of the project not in the src of MIDP, not in src of RIM and also not src of Desktop.

So now i want to read my addressBook.xml file into StateMachine.java file using any KXML Parser. But not used like DOM or SAX parser to parse rather than that to how can i now read my xml file and parse it and also get the values of xml to do further process.

Is it possible to do like that? If Possible then please help me as soon as earlier because i need it fastly.

Advance Thank you and sorry for my bad and poor english.

asims
Offline
Joined: 2011-04-14
Points: 0

The following web page has a good example of reading an XML file using a KXML Parser:

http://www.roseindia.net/j2me/kxml/j2me-xml-parser.shtml

While reading your XML file keep in mind it must be in your classpath, and more likely then not that means the XML file must be packaged in your Jar file when you deploy it. Your src directory should be less important if your project is built as they will likely all be deployed inside the same Jar file with the package names starting from the same root. If your XML file is in the root directory it would be /your.xml, if it is com.app it would be /com/app/your.xml from the Java code in the example InputStream in = this.getClass().getResourceAsStream(emp); where emp is a String referring to "/com/app/your.xml"

If you need further assistance let me know.

Good luck,

Aaron

rashmitasahoo
Offline
Joined: 2010-10-26
Points: 0

Thank you for reply

Your answer will work and it can solve my problem.