I would like to parse an xml file. I am trying to locate the error, but i can not find it. Could you help me, pls?
xml:
<maintag>
<data>
<id>1</id>
<name>x y</name>
<age>16</age>
<phone>06/30 123-4567</phone>
<address>Veszprem Valami ut 10.</address>
</data>
<data>
<id>2</id>
<name>p q</name>
<age>18</age>
<phone>06/70 987-6543</phone>
<address>Budapest Ulloi ut 21.</address>
</data>
</maintag>
XMLParser class:
package com.example.xmlproba;
import java.util.jar.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class XMLParser extends DefaultHandler {
boolean xmlStartLine = false;
boolean id = false;
boolean data = false;
boolean maintag = false;
boolean age = false;
boolean name = false;
boolean phone = false;
boolean address = false;
Data currentData;
DataContainer dataContainer;
public XMLParser(DataContainer dataContainer){
this.dataContainer=dataContainer;
Log.d("FUNC","XMLPARSER()");
// Data d = new Data();
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.d("START","START");
if (qName.equalsIgnoreCase("MAINTAG")) {
Log.d("Maintagfound","mtf");
} else if (qName.equalsIgnoreCase("DATA")) {
//create new data object
currentData = new Data();
Log.d("NEWDATA","NEWDATA");
} else if (qName.equalsIgnoreCase("ID")) {
id = true;
Log.d("id","id");
} else if (qName.equalsIgnoreCase("NAME")) {
name = true;
Log.d("name","name");
} else if (qName.equalsIgnoreCase("AGE")) {
age = true;
} else if (qName.equalsIgnoreCase("PHONE")) {
phone = true;
} else if (qName.equalsIgnoreCase("ADDRESS")) {
address = true;
} else {
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
Log.d("END","END");
if (qName.equalsIgnoreCase("DATA")) {
//todo at the end of a data node
//dataContainer.addDataToList(currentData);
//dataContainer.dataList.add(currentData);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
Log.d("CHARS","CHARS");
if (id) {
String s = new String(ch, start, length);
currentData.setId(Integer.parseInt(s));
Log.d("IDIDID",s);
id = false;
} else if (name) {
String s = new String(ch, start, length);
Log.d("NAME1",s);
currentData.setName(s);
Log.d("NAME2",currentData.getName());
name = false;
} else if (age) {
String s = new String(ch, start, length);
currentData.setAge(Integer.parseInt(s));
age = false;
} else if (phone) {
String s = new String(ch, start, length);
currentData.setPhone(s);
phone = false;
} else if (address) {
String s = new String(ch, start, length);
currentData.setAdress(s);
address = false;
} else {
}
}
}
And the parsing part of my activity:
private void readXML() {
// TODO Auto-generated method stub
Log.d("FUNC","READXML");
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XMLParser xp = new XMLParser(dataContainer);
xr.setContentHandler(xp);
InputStream is = getResources().openRawResource(
R.raw.data);
xr.parse(new InputSource(is));
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
Log.d("PARSERCONFEX","PARSERCONFEX");
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
Log.d("SAXEX","SAXEX");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("IOEX","IOEX");
e.printStackTrace();
}
}
From the log.d() i can see that only the log from constructor, the “CHARS” and “END” logs are created.
Here is my log:
12-06 06:38:11.063: E/Trace(629): error opening trace file: No such file or directory (2)
12-06 06:38:11.623: D/FUNC(629): INIT
12-06 06:38:11.623: D/FUNC(629): READXML
12-06 06:38:11.643: D/FUNC(629): XMLPARSER()
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.673: D/END(629): END
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/END(629): END
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/END(629): END
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/END(629): END
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/END(629): END
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/END(629): END
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.703: D/CHARS(629): CHARS
12-06 06:38:11.703: D/END(629): END
12-06 06:38:11.703: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/END(629): END
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/END(629): END
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/END(629): END
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/END(629): END
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/END(629): END
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/END(629): END
12-06 06:38:12.063: I/Choreographer(629): Skipped 77 frames! The application may be doing too much work on its main thread.
12-06 06:38:12.093: D/gralloc_goldfish(629): Emulator without GPU emulation detected.
Here is an example that what you wanna do using SAX
JAVA CLASSES
Data
DatasHandler
XMLManager
Main
Console output
You seem want to use it on Android. So I think that you can do the following in XMLManager class :
Tell me if it works,