来自SD卡的Android XMLPullParser ......有什么不对吗?

时间:2014-06-18 06:27:41

标签: android xmlpullparser

我一直在尝试很多不同的教程来尝试从xml中读取一个简单的用户名和密码。它用于作业。

 <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
 <UserDetails>
 <Username User="Bryan" Pass="Boo" />
 </UserDetails>

我一直在使用的连接代码

public void UserCheck() throws FileNotFoundException, IOException{


    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        //file ref
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"/UserDetails.xml");
        //inputstream
        FileInputStream fis = new FileInputStream(file.getPath());
        //set the input
        xpp.setInput(new InputStreamReader(fis));
       //Tutorial didnt use the eventType
        int eventType = xpp.getEventType();

        String nodeName = xpp.getName();
        if (nodeName=="Username"){

        }
    } catch (XmlPullParserException ex) {
        Logger.getLogger(Comp4Task2.class.getName()).log(Level.SEVERE, null, ex);
    }

我看到的主要问题是xpp.getName始终返回null。我不确定我做错了什么。谢谢参观。我试过看了..这是我试过的 how to parse xml file from Sdcard in Android Read/write to external XML file in Android

1 个答案:

答案 0 :(得分:0)

这就是我设法做到的......

    public boolean UserCheck(String Username, String Password) throws FileNotFoundException, IOException{


    try {

        String data;
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"/UserDetails.xml");
        FileInputStream fis = new FileInputStream(file.getPath());
       InputStreamReader isr = new InputStreamReader(fis);
        /////////////////////
        char[] InputBuffer = new char[fis.available()];
        isr.read(InputBuffer);
         data = new String(InputBuffer);
        isr.close();
        fis.close();
        XmlPullParser xpp = null;
        xpp = factory.newPullParser();
        xpp.setInput(new StringReader(data));
        ///////////////////////////////////////

        int eventType = 0;
        eventType = xpp.getEventType();


        while (eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_DOCUMENT) {
    System.out.println("Start document");
}
else if (eventType == XmlPullParser.START_TAG) {
    String tagName = xpp.getName();
    if (tagName.contentEquals("User"))
    {
        String StoredUsername = xpp.getAttributeValue(null,"Username");
        String StoredPassword = xpp.getAttributeValue(null, "Password");
        if (Username.equals(StoredUsername)){
            if(Password.equals(StoredPassword)){
                return true;
            }
        }
    }
}
else if (eventType == XmlPullParser.END_TAG) {
    System.out.println("End tag "+xpp.getName());
}
        eventType = xpp.next();
}
    } catch (XmlPullParserException ex) {
        Logger.getLogger(Comp4Task2.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}  

我想我也会添加用于创建xml的代码。 1站车..

public void SaveToFile(){
Username = inputUsername.getText().toString();
Password = inputPassword.getText().toString();


try{

    FileOutputStream fos = new FileOutputStream(myXML);

    //FileOutputStream fil = getApplicationContext().openFileOutput("UserDetails", Context.MODE_WORLD_WRITEABLE);

    XmlSerializer xSe = Xml.newSerializer();
   xSe.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);


    StringWriter writer = new StringWriter();
    xSe.setOutput(writer);
    xSe.startDocument("UTF-8", true);
    xSe.startTag(null, "UserDetails");
    xSe.startTag(null, "User");
    xSe.attribute(null,"Username", Username);
    xSe.attribute(null, "Password",Password);
    xSe.endTag(null, "User");
    xSe.endTag(null, "UserDetails");

    xSe.endDocument();
    xSe.flush();
    String dataWrite = writer.toString();
    fos.write(dataWrite.getBytes());
    fos.close();
}
catch(Exception e){}

}