我正在尝试解析 xml 资源,但我无法解析。
xml 文件如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<elencoanagrafiche trovati="7" errStatus="0">
<anagrafica>
<idAnagrafica>75</idAnagrafica>
<nome><![CDATA[Alberto]]></nome>
<cognome><![CDATA[Pippo]]></cognome>
<email><![CDATA[aaa.bbb@ccc.it]]></email>
<dataDiNascita><![CDATA[26/07/1478]]></dataDiNascita>
</anagrafica>
<anagrafica>
.
.
</anagrafica>
</elencoanagrafiche>
我的萨克斯解析课:
package mypackage;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.parsers.SAXParser;
import net.rim.device.api.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLSAXParser extends MainScreen {
private ButtonField _bIndietro = null;
private UiApplication _uiApp;
LabelField lbLine = new LabelField("-------------");
public XMLSAXParser() {
//setup della pagina(layout, bottoni, text)
add(new RichTextField(ConnectionClass.url));
_bIndietro = new ButtonField("Indietro", Field.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
_bIndietro.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ConnectionClass.connect();
_uiApp.popScreen(XMLSAXParser.this);
}
});
add(_bIndietro);
_uiApp = UiApplication.getUiApplication();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean bidAnagrafica, belencoanagrafiche = false;
boolean bname, banagrafica = false;
boolean bcognome = false;
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
System.out.println("Start Element :" + qName);
if (qName.equalsIgnoreCase("elencoanagrafiche")) {
belencoanagrafiche = true;
}
if (qName.equalsIgnoreCase("anagrafica")) {
banagrafica = true;
}
if (qName.equalsIgnoreCase("idAnagrafica")) {
bidAnagrafica = true;
}
if (qName.equalsIgnoreCase("nome")) {
bname = true;
}
if (qName.equalsIgnoreCase("cognome")) {
bcognome = true;
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println("End Element :" + qName);
}
public void characters(char ch[], int start, int length) throws SAXException {
if (belencoanagrafiche) {
LabelField lb = new LabelField("elencoanagrafiche : " + new String(ch, start, length));
HorizontalFieldManager sr = new HorizontalFieldManager();
sr.add(lb);
add(sr);
belencoanagrafiche = false;
}
if (banagrafica) {
LabelField lb = new LabelField("anagrafica : " + new String(ch, start, length));
HorizontalFieldManager sr = new HorizontalFieldManager();
sr.add(lb);
add(sr);
add(new LabelField("-----"));
banagrafica = false;
}
if (bidAnagrafica) {
LabelField lb = new LabelField("idAnagrafica : " + new String(ch, start, length));
HorizontalFieldManager sr = new HorizontalFieldManager();
sr.add(lb);
add(sr);
bidAnagrafica = false;
}
if (bname) {
LabelField lb = new LabelField("Nome :" + new String(ch, start, length));
HorizontalFieldManager sr = new HorizontalFieldManager();
sr.add(lb);
add(sr);
bname = false;
}
if (bcognome) {
LabelField lb = new LabelField("Cognome : " + new String(ch, start, length));
HorizontalFieldManager sr = new HorizontalFieldManager();
sr.add(lb);
add(sr);
bcognome = false;
}
}
};
InputStream inputStream = ConnectionClass.httpConn.openInputStream();
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
//InputStream inputStream = ConnectionClass.httpConn.openInputStream();
saxParser.parse(inputStream, handler);
} catch (Exception e) {
e.printStackTrace();
Dialog.alert("Exception Error: " + e.toString());
}
}
}
现在我的问题是我没有得到任何元素值,如姓名、认知、电子邮件等。我只得到了 id 标识符,但没有得到 . 所以我的问题是如何获取 CDATA 中的数据?