0

I have been trying a lot of different tutorials to try read a simple username and password from an xml. Its for an assignment.

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

My Connection codes I have been using

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);
    }

The main issue I see is the xpp.getName returns null all the time. I am not sure what I am doing wrong. Thanks for taking a look. I have tried looking it up.. here is what I have tried how to parse xml file from Sdcard in Android Read/write to external XML file in Android

Community
  • 1
  • 1

1 Answers1

0

This is how I managed to do it...

    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;
}  

I thought I would add the code for creating the xml as well. 1 stop shop..

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){}

}