×

Discussion Board

Results 1 to 3 of 3

Thread: kxml error

  1. #1
    Regular Contributor
    Join Date
    Nov 2010
    Posts
    118

    kxml error

    hi i want to add new elements on my xml file..please help me.. i have this code from a tutorial. it has an error...

    Code:
    /*
    * XML Parsing using kxml2
    * Author : Naveen Balani
    */
    
    //KXML Apis
    import org.kxml2.io.*;
    import org.xmlpull.v1.*;
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    
    import java.io.*;
    import java.util.Vector;
    import javax.microedition.io.file.FileConnection;
    
    
    public class XMLJ2MEService extends MIDlet implements CommandListener {
    
        //Form Name
        Form mainForm = new Form ("SampleJ2MEXML");
    
        //Location of xml file
        static final String URL = System.getProperty("fileconn.dir.photos");
        Vector bookVector = new Vector();
    
        StringItem resultItem = new StringItem ("", "");
    
        private final static Command xmlCommand = new Command("Get XML Data", Command.OK,1);
    
    
        class ReadXML extends Thread {
    
    		public void run() {
    		try {
    		        //Open http connection
    		        FileConnection fconn = (FileConnection)Connector.open(URL+"tae.xml",Connector.READ_WRITE);
                            if (!fconn.exists())
                {
                    fconn.create();
                }
    			//Initilialize XML parser
    			KXmlParser parser = new KXmlParser();
    
    			parser.setInput(new InputStreamReader(fconn.openInputStream()));
    
    			parser.nextTag();
    
    			parser.require(XmlPullParser.START_TAG, null, "catalog");
    
    			//Iterate through our XML file
    			while (parser.nextTag () != XmlPullParser.END_TAG)
    		        	 readXMLData(parser);
    
    			parser.require(XmlPullParser.END_TAG, null, "catalog");
    			parser.next();
    
    			parser.require(XmlPullParser.END_DOCUMENT, null, null);
    
    
    		}
    		 catch (Exception e) {
    	    	  	e.printStackTrace ();
    	    		resultItem.setLabel ("Error:");
    	    		resultItem.setText (e.toString ());
    
    		}
    	    }
         }
    
    
        public XMLJ2MEService () {
           	mainForm.append (resultItem);
        	mainForm.addCommand (xmlCommand);
    	mainForm.setCommandListener (this);
    
    
         }
    
    
        public void startApp () {
    	Display.getDisplay (this).setCurrent (mainForm);
    	new ReadXML().start();
        }
    
        public void pauseApp () {
        }
    
        public void destroyApp (boolean unconditional) {
        }
    
       public void commandAction(Command c, Displayable d) {
    
         StringBuffer sb = new  StringBuffer();
    
         if (c == xmlCommand) {
    
         	    //Display parsed  XML file
         	    for(int i= 0 ; i< bookVector.size() ;i++){
         	    	Book book = (Book) bookVector.elementAt(i);
         	    	sb.append("\n");
         	    	sb.append("Name : ");
         	    	sb.append(book.getName());
         	    	sb.append("\n");
         	    	sb.append("Descrition : ");
         	    	sb.append(book.getDescription());
         	    	sb.append("\n");
    
    
         	      }
         	      resultItem.setLabel("Book Information");
         	      resultItem.setText(sb.toString());
           }
    
        }
    
        private void readXMLData(KXmlParser parser)
    			throws IOException, XmlPullParserException {
    
    
    			//Parse our XML file
    			parser.require(XmlPullParser.START_TAG, null, "title");
    
    			Book book = new Book();
    
    			while (parser.nextTag() != XmlPullParser.END_TAG) {
    
    
    				parser.require(XmlPullParser.START_TAG, null, null);
    				String name = parser.getName();
    
    				String text = parser.nextText();
    
    				System.out.println ("<"+name+">"+text);
    
    				if (name.equals("name"))
    					book.setName(text);
    				else if (name.equals("description"))
    					book.setDescription(text);
    				else if (name.equals("author"))
    					book.setAuthor(text);
    				else if (name.equals("rating"))
    					book.setRating(text);
    				else if (name.equals("available"))
    					book.setAvailable(text);
    
    
    
    
    			parser.require(XmlPullParser.END_TAG, null, name);
    			}
    
    			bookVector.addElement(book);
    
    			parser.require(XmlPullParser.END_TAG, null, "title");
    
    
    
    		}
    	}
    an error occurs saying:

    org.xmlpull.v1.XmlPullParserException:
    unexpected type(position:END_DOCUMENT null@1:0 in
    java.io.InputStreamReader@1ccf1ccf)

    what does this mean? please help me..thanks

  2. #2
    Super Contributor
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,191

    Re: kxml error

    Could be something in your code as well, but have you checked that the XML you are parsing is well-formed and there are no errors in there?
    Run it for example through some XML Validator like http://validator.w3.org/

    Hartti

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105

    Re: kxml error

    Please, let us continue in your original thread …

Similar Threads

  1. KXML, I need help (anyone?)
    By binsint in forum Mobile Java Tools & SDKs
    Replies: 7
    Last Post: 2011-01-26, 12:50
  2. Replies: 6
    Last Post: 2010-12-21, 10:44
  3. ERROR: java.lang.NoClassDefFoundError: org/kxml/parser/XmlParser
    By dhiraj.deka in forum Mobile Java General
    Replies: 9
    Last Post: 2009-11-07, 09:34
  4. Http error | Kxml parsing
    By javame in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2003-09-12, 09:35

Posting Permissions

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