1

I am working on an android application that reads and writes XML. No doubt it is not a fancy application. I am working with the XmlPullParser in order to read/parse an XML file. When I call getText, I am getting a strange value and not the value from the XML file. And I am starting to pull out what little hair I have to resolve the problem.

The application is very simple. I have five edit boxes that allow the user to enter alphanumeric data. Upon pressing the save button, the data is saved as XML to a local file. The XML is not well-formed, in that it does not have the declaration.

The XML file looks like

<TESTXML>
<Something1>value</Something1>
<Something2>value</Something2>
<Something3>value</Something3>
</TESTXML>

In the writing of the XML, I am doing the following:

String sSome1= xmlSome1EditText.getText().toString();
String sSome2= xmlSome2EditText.getText().toString();
String sSome3= xmlSome3EditText.getText().toString();

XmlSerializer xmlSerializer = Xml.newSerializer();

xmlSerializer.startTag("", "TESTXML");

xmlSerializer.startTag("", "Something1");
xmlSerializer.text("value");
xmlSerializer.endTag("", "Something1");

xmlSerializer.startTag("", "Something2");
xmlSerializer.text("value");
xmlSerializer.endTag("", "Something2");

xmlSerializer.startTag("", "Something3");
xmlSerializer.text("value");
xmlSerializer.endTag("", "Something3");

xmlSerializer.endTag("", "TESTXML");
xmlSerializer.endDocument();

And I am using RandomAccessFile to write the XML data to a local file.

In the read/parse of the XML, I am doing the following:

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

InputStream is = openFileInput("myxml.xml"); 

xpp.setInput(is);

int eventType = xpp.getEventType();

String sSome1 = "";
String sSome2 = "";
String sSome3 = "";

bool bSome1 = false;
bool bSome2 = false;
bool bSome3 = false;

while (eventType != XmlPullParser.END_DOCUMENT) 
{
    if(eventType == XmlPullParser.START_TAG) 
    {
      if( xpp.getName().equals("Something1") )
      {          
         bSome1 = true;
      } 
      else if( xpp.getName().equals("Something2") )
      {          
         bSome2 = true;
      } 
      else if( xpp.getName().equals("Something3") )
      {          
         bSome3 = true;
      } 
    }
    else if(eventType == XmlPullParser.END_TAG) 
    {
       bSome1 = false;
       bSome2 = false;
       bSome3 = false;
    } 
    else if(eventType == XmlPullParser.TEXT) 
    {
        if( bSome1 )
        {
           sSome1 = xpp.getText();
        }
        else if ( bSome2 )
        {
           sSome2 = xpp.getText();
        }
        else if ( bSome3 )
        {
           sSome3 = xpp.getText();
        }
   }

   eventType = xpp.next();
}

Please excuse any copy/paste errors.

I am not performing any complex processing, just simple parsing of the XML. I have verified that the the node names are correct. Every article that I have read indicates this approach should get the proper value. But each time the getText is called, I expect that the value from the XML file will be returned but I am getting a value similar to "android.widget.EditText.."

Any ideas?

2
  • it seems correct. can you check the xml file content after you have run the code ?! Commented Oct 9, 2013 at 15:50
  • I am testing the application on an original Nexus 7 that has been rooted. I have opened the XML file via FX Text Editor and it is showing valid XML. It is almost as if I have some type of binding between the EditText controls and the XML values. Commented Oct 9, 2013 at 15:52

1 Answer 1

5

Why are you bothering with the booleans? You can shorten your code significantly by doing this:

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

  InputStream is = openFileInput("myxml.xml"); 

  xpp.setInput(is);

  int eventType = xpp.getEventType();

  String sSome1 = "";
  String sSome2 = "";
  String sSome3 = "";

  while (eventType != XmlPullParser.END_DOCUMENT) {
    switch(eventType) {
      case XmlPullParser.START_TAG:
        String tagName = xpp.getName();

        if (tagName.equalsIgnoreCase("Something1")) {
          sSome1 = xpp.nextText();
        }
        else if (tagName.equalsIgnoreCase("Something2")) {          
          sSome2 = xpp.nextText();
        } 
        else if (tagName.equalsIgnoreCase("Something3")) {          
          sSome3 = xpp.nextText();
        } 
        break;
    }
    eventType = xpp.next();
  }
} catch (XmlPullParserException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Pay attention to nextText() instead of getText() since you are still in the start-tag.

Try this, maybe something went wrong because of your implementation. Other than that it should work. Also, notice that I put this in a try/catch so you can actually see if something went wrong while parsing.

Sign up to request clarification or add additional context in comments.

7 Comments

I was using the booleans for test purposes only. The code was very inefficient but I wanted to get the parsing to work correctly before revamping the functionality for efficiency. I implemented the suggested changes but the problem persists. I wanted to verify if the XML creation in my app was the cause of the issue so I deleted the XML file and created it manually. Unfortunately, same result as before where it can get to the proper nodes in the XML but the text field in the parser contains the strange value.
Very hard to say then. Have you debugged already? Go through the whole chain and check your values: what are you getting from the EditTexts, what values are you serializing, are they really in the written XML, and then debug each row in the parser. Maybe try to set the proper namespaces and encoding (UTF-8) for the XML. Also, you are setting the default namespace for every tag, check if that's necessary. Try null for the first parameter instead of an empty String in 'startTag()' for your text-tags. Just ideas.. go through everything.
BTW, use 'startDocument(String encoding, Boolean standalone)' for a proper XML declaration. You could use UTF-8 encoding, 2nd parameter true if you want the declaration as a standalone line. Check the documentation: developer.android.com/reference/org/xmlpull/v1/…
I appreciate all the ideas. If it were C++ or C#, I would have been done in a few minutes. But I am a newbie to Android development. In doing additional testing/debugging, I found something interesting. I manually modified the XML to include a bogus node with a bogus value. Upon parsing the XML, the code returned the correct result (the one from the file). The problem seems to be related to values in the XML that are derived from or will be populated to the EditText fields.
BTW - the lack of XML declaration is intentional. At some point, the XML from this app will be ingested into a 3rd party application. The 3rd party app does not support the XML declaration.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.