0

我正在尝试在黑莓中使用 SAX 解析器并使用了本教程:

http://javarevisited.blogspot.com/2011/12/parse-read-xml-file-java-sax-parser.html#ixzz2OZJ6vB8O

这是我的解析类的代码

import java.io.ByteArrayInputStream;

import java.io.InputStream;


import net.rim.device.api.xml.parsers.SAXParser;
import net.rim.device.api.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Samp extends DefaultHandler{

    String url = "http://ravee.clientarea.in/exhibitr/getData.php?db_table=events";
    TheVectorClass tcv ;

    boolean currentElement = false;
    String currentValue = "";

//   private Account acct;
     private String temp;


    public void parsingmethod()

     {     

        System.out.println("in parsingmethod");
         SAXParserFactory spfac = SAXParserFactory.newInstance();

         //Now use the parser factory to create a SAXParser object
         //Create an instance of this class; it defines all the handler methods
        Samp handler;    
        try {
            SAXParser sp = spfac.newSAXParser();


             handler = new Samp();


//           XMLReader = new 

             System.out.println("url is     "
                     + url );
             InputStream is  = new ByteArrayInputStream(url.getBytes());
             //Finally, tell the parser to parse the input and notify the handler
             System.out.println("value of is     " +is);
             System.out.println("value of is     " +handler);

//           System.out.println("            sp.parse(is, handler)                "+            
//          here in next line I get the error 
             sp.parse(is, handler);

        }  catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("here in samp error 2");
            e.printStackTrace();
            System.out.println("e.gemessage  2  " +e.getMessage() );
        }



    //Read more: http://javarevisited.blogspot.com/2011/12/parse-read-xml-file-java-sax-parser.html#ixzz2OZJ6vB8O

     }



    /*
     * Every time the parser encounters the beginning of a new element,
     * it calls this method, which resets the string buffer
     */ 
    public void startElement(String uri, String localName,
                  String qName, Attributes attributes) throws SAXException {
           temp = "";

           System.out.println("reached");
           if (qName.equalsIgnoreCase("parameters")) {
//                  acct = new Account();
//                  acct.setType(attributes.getValue("type"));
                  System.out.println("val " +temp);

           }
    }

    /*
     * When the parser encounters the end of an element, it calls this method
     */
    public void endElement(String uri, String localName, String qName)
                  throws SAXException {

           if (qName.equalsIgnoreCase("nme")) {
                  // add it to the list
//                  accList.add(acct);

               System.out.println("val1 " +temp);

           }

           else if (qName.equalsIgnoreCase("descriptin")) {
//                  acct.setName(temp);

               System.out.println("val1 " +temp);
           }



}

并在我的屏幕类中调用它

public final class HomeScreen extends MainScreen
{
    /**
    * Creates a new HomeScreen object
    */

    Samp smp;
    String elemtn;

    Connection coon;
    boolean currentElement = false;
    String currentValue = "";

    HomeScreen hm;

    //     Samp smp ;
    TheVectorClass tcv ;
    public HomeScreen()
    {
        // Set the displayed title of the screen
        setTitle("parsing  ");
        final TheVectorClass tcv ;

        //        hm = new HomeScreen();

        smp = new Samp();

        try {
            MainScreenUpdaterThread thread = new MainScreenUpdaterThread(this , smp);
            thread.start();

            System.out.println("before");
            //            smp.parsingmethod();
            System.out.println("after");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("e.get message   "+e.getMessage());
        }   
    }
}

我得到的错误是

期待一个元素

我该如何解决这个错误?

4

1 回答 1

0

错误是由于制作输入流的方式

InputStream is  = new ByteArrayInputStream(url.getBytes());

当 xml 文件在本地时,我们使用这种方式创建输入流,当文件在服务器上时,我们需要打开连接,以便我们像这样打开连接

首先将url的字符串转换为streamconnection

StreamConnection stream = (StreamConnection)Connector.open(url);

and then open the input stream 

InputStream is =  stream.openInputStream();

然后使用输入流解析xml

于 2013-04-03T13:50:29.243 回答