Reputation: 11
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?
Upvotes: 1
Views: 1170
Reputation: 3827
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.
Upvotes: 5