Results 1 to 6 of 6
  1. #1
    krr
    krr is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Exclamation How can I parse the data ?

    Hi, I am new to J2me,In my MIDlet I received XML content from wed server and then parsed It. How can I parsed ? Here I used Kxml ?

  2. #2
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    Try this,

    Java Code:
    package parsingXml;
    
    import javax.microedition.midlet.*;
    import org.kxml.*;
    import org.w3c.dom.*;
    import java.io.*;
    import java.io.InputStreamReader;
    import javax.microedition.io.Connector;
    import javax.microedition.io.file.FileConnection;
    import org.kxml.parser.ParseEvent;
    import org.kxml.parser.XmlParser;
    //import net.n3.nanoxml.XMLElement;
    
    public class parseXmlFile extends MIDlet
    {
        public String emp="/employees.xml";
        XmlParser parser;
        public void startApp()
        {
         try
         {
            parser=new XmlParser(new InputStreamReader(this.getClass().getResourceAsStream(emp)));
            ParseEvent event = null;
            while((event = parser.read()).getType() != Xml.END_DOCUMENT)
            {
                if(event.getType() == Xml.START_TAG)
                {
                    String name = event.getName();
                    if(name != null && name.equals("details"))
                    {
                        System.out.println("----------[ EMP-DETAILS ]----------");
                        parseAddressTag( parser );
                        System.out.println("-----------------------------------\n");
                    }
                    name = null;
                }
                event = null;
            }
            //createNode();
           }catch (IOException ioe){System.out.println("XML Parsing Error: " + ioe);ioe.printStackTrace();
            }finally{parser = null;}
        }
    
        public void pauseApp()
        {
        }
    
        public void destroyApp(boolean unconditional)
        {
        }
    
        public void parseAddressTag( XmlParser parser ) throws IOException
        {
            ParseEvent event = null;
            while ((event = parser.peek()).getType() != Xml.END_DOCUMENT)
            {
                String name = event.getName();
                int type = event.getType();
                if (type == Xml.END_TAG && name.equals("details"))
                {
                    event = null;
                    name = null;
                    return;
                }
                event = parser.read();
                if (type != Xml.START_TAG)
                {
                    event = null;
                    continue;
                }
                ParseEvent next = parser.read();
                if (next.getType() != Xml.TEXT)
                {
                    event = null;
                    next = null;
                    continue;
                }
              String text = next.getText();
              System.out.println( name + ": " + text );
              event = null;
              text = null;
              next = null;
            }
        }
    
        /*public void createNode()
        {
            XMLElement Element=new XMLElement();
            Element.setName("Details");
    
            XMLElement child=new XMLElement();
            child.setName("name");
    
            XMLElement child1=new XMLElement();
            child1.setName("designaton");
    
            XMLElement child2=new XMLElement();
            child2.setName("city");
    
            XMLElement child3=new XMLElement();
            child3.setName("state");
        }*/
    }
    Last edited by Eranga; 08-20-2010 at 02:07 PM. Reason: code tags added

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    @nitinverma, please use code tags when you are posting again. Unformated code are really hard to read.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by krr View Post
    Hi, I am new to J2me,In my MIDlet I received XML content from wed server and then parsed It. How can I parsed ? Here I used Kxml ?
    It depends on how you want to pass, or how the device accepting the data. Could you provide more details so that it's possible to comment on title.

  5. #5
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    Ok Eranga,

    I will,

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Using Tokenizer to parse file data--CODE
    By Cylab in forum Java Software
    Replies: 0
    Last Post: 07-26-2010, 11:48 AM
  2. How to parse this value
    By Ajitha in forum New To Java
    Replies: 2
    Last Post: 06-03-2010, 07:26 AM
  3. using parse
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 01-15-2010, 01:32 AM
  4. SAX Parse
    By bluefloyd8 in forum New To Java
    Replies: 1
    Last Post: 01-25-2008, 03:57 PM
  5. How to parse in html
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •