1

有这个奇怪的问题我在android中使用这个代码来获取XML数据

  public class MainActivity extends Activity {
private Object[] lv_arr = {};
 // static String url = "C:\\Users\\Administrator\\Desktop\books.xml";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText edt = (EditText)findViewById(R.id.editText1);
    try {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {

        boolean bfname = false;
        boolean blname = false;
        boolean bnname = false;
        boolean bsalary = false;

        public void startElement(String uri, String localName,String qName, 
                    Attributes attributes) throws SAXException {

            System.out.println("Start Element :" + qName);
            edt.setText(qName); //Notice the text field

            if (qName.equalsIgnoreCase("FIRSTNAME")) {
                bfname = true;
            }

            if (qName.equalsIgnoreCase("LASTNAME")) {
                blname = true;
            }

            if (qName.equalsIgnoreCase("NICKNAME")) {
                bnname = true;
            }

            if (qName.equalsIgnoreCase("SALARY")) {
                bsalary = true;
            }

        }

        public void endElement(String uri, String localName,
            String qName) throws SAXException {

            System.out.println("End Element :" + qName);

        }

        public void characters(char ch[], int start, int length) throws SAXException {

            if (bfname) {
                System.out.println("First Name : " + new String(ch, start, length));

                bfname = false;
            }

            if (blname) {
                System.out.println("Last Name : " + new String(ch, start, length));
            //  edt.setText(new String(ch, start, length));
                blname = false;
            }

            if (bnname) {
                System.out.println("Nick Name : " + new String(ch, start, length));
                bnname = false;
            }

            if (bsalary) {
                System.out.println("Salary : " + new String(ch, start, length));
                bsalary = false;
            }

        }

         };

           saxParser.parse("c:\\file.xml", handler);

         } catch (Exception e) {
           e.printStackTrace();
         }
}

但是这段代码在java中是否可以正常工作,但在android中没有语法错误,但我没有输出,而且文本字段也是空的,这里有什么问题我的log-cat RESULT:

03-10 00:42:51.222: W/System.err(366):  at javax.xml.parsers.SAXParser.parse(SAXParser.java:392)
03-10 00:42:51.232: W/System.err(366):  at javax.xml.parsers.SAXParser.parse(SAXParser.java:268)
03-10 00:42:51.320: W/System.err(366):  at dalvik.system.NativeStart.main(Native Method)
03-10 00:42:51.832: W/InputManagerService(59): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@43f4ae90 (uid=10025 pid=132)

这里可能是什么问题,任何人都可以提供帮助。

4

2 回答 2

0
saxParser.parse("c:\\file.xml", handler);

这不适用于设备...那些无法访问您的C:\\ Insead 放入资源或您将从 apk、运行时加载它的文件夹

于 2013-03-09T19:49:48.300 回答
0

不确定这是否适合您,但如果您将 file.xml 保存在 res/xml 目录中,您可以XmlResourceParser使用以下方法在 MainActivity 中获取:

XmlResourceParser myXmlParser = getResources().getXml(R.xml.file);
于 2013-03-09T20:02:05.670 回答