我正在尝试从 xml 字符串中读取数据并使用 setter getter 方法设置相应的标记元素,但我的 xml 在 xml 文件中显示格式错误。我在这里做错了什么是我的代码。
在 oncreate..
SAXHelper2 sh = null;
try {
sh = new SAXHelper2(newxml);
} catch (MalformedURLException e) {
e.printStackTrace();
}
sh.parseContent("");
return null;
}
}
/*
*
*/
class SAXHelper2 {
private String data;
StringBuffer chars = new StringBuffer();
public SAXHelper2(String xmlstring) throws MalformedURLException {
this.data = new String(xmlstring);
}
DefaultHandler handler = new DefaultHandler();
public RSSHandler parseContent(String parseContent) {
RSSHandler df = new RSSHandler();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new InputSource(newxml), new RSSHandler());
} catch (Exception e) {
e.printStackTrace();
}
return df;
}
class RSSHandler extends DefaultHandler {
private ComptePost currentPost = new ComptePost();
StringBuffer chars = new StringBuffer();
public void startElement(String uri, String localName, String qName, Attributes atts) {
chars = new StringBuffer();
if (localName.equalsIgnoreCase("comptes")) {
}
}
DefaultHandler handler = new DefaultHandler() {
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("numCompte")
&& currentPost.getNumComtpe() == null) {
System.out.println("Post: "+currentPost.getNumComtpe());
Log.i("numCompte", currentPost.getNumComtpe());
currentPost.setNumComtpe(chars.toString());
}
if (localName.equalsIgnoreCase("authCompte")
&& currentPost.getAuthCompte() == null) {
currentPost.setAuthCompte(chars.toString());
}
if (localName.equalsIgnoreCase("typeCompte")
&& currentPost.getTypeCompte() == null) {
currentPost.setTypeCompte(chars.toString());
}
if (localName.equalsIgnoreCase("libelleCompte")
&& currentPost.getLibelleCompte()== null) {
currentPost.setLibelleCompte(chars.toString());
}
if (localName.equalsIgnoreCase("soldeCompte")
&& currentPost.getSoldeCompte() == null) {
currentPost.setSoldeCompte(chars.toString());
}
if (localName.equalsIgnoreCase("deviseCompte")
&& currentPost.getDeviseCompte() == null) {
currentPost.setDeviseCompte(chars.toString());
}
if (localName.equalsIgnoreCase("dateSolde")
&& currentPost.getDateSolde()== null) {
currentPost.setDateSolde(chars.toString());
}
if (localName.equalsIgnoreCase("droitVirement")
&& currentPost.getDroitVirement()== null) {
currentPost.setDroitVirement(chars.toString());
}
if (localName.equalsIgnoreCase("carteBancaire")
&& currentPost.getCarteBancaire()== null) {
currentPost.setCarteBancaire(chars.toString());
}
if (localName.equalsIgnoreCase("debitMin")
&& currentPost.getDebitMin()== null) {
currentPost.setDebitMin(chars.toString());
}
if (localName.equalsIgnoreCase("debitMax")
&& currentPost.getDebitMax()== null) {
currentPost.setDebitMax(chars.toString());
}
if (localName.equalsIgnoreCase("creditMin")
&& currentPost.getCreditMin()== null) {
currentPost.setCreditMin(chars.toString());
}
if (localName.equalsIgnoreCase("creditMax")
&& currentPost.getCreditMax()== null) {
currentPost.setCreditMax(chars.toString());
}
if (localName.equalsIgnoreCase("echeanceMax")
&& currentPost.getEcheanceMax()== null) {
currentPost.setEcheanceMax(chars.toString());
}
if (localName.equalsIgnoreCase("comptes")) {
PostList.add(currentPost);
currentPost = new ComptePost();
}
}
@Override
public void characters(char ch[], int start, int length) {
chars.append(new String(ch, start, length));
}
};
}
}
java.io.IOException:无法打开原因:java.net.MalformedURLException:找不到协议:
04-05 15:24:52.699: W/System.err(4784): at org.apache.harmony.xml.ExpatParser.openUrl(ExpatParser.java:760)
04-05 15:24:52.703: W/System.err(4784): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:289)
04-05 15:24:52.707: W/System.err(4784): at javax.xml.parsers.SAXParser.parse(SAXParser.java:390)
04-05 15:24:52.707: W/System.err(4784): at .details.CompteDetails$SAXHelper2.parseContent(CompteDetails.java:222)
04-05 15:24:52.707: W/System.err(4784): at .details.CompteDetails$loadingTask.doInBackground(CompteDetails.java:193)
04-05 15:24:52.710: W/System.err(4784): at .details.CompteDetails$loadingTask.doInBackground(CompteDetails.java:1)
04-05 15:24:52.710: W/System.err(4784): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-05 15:24:52.710: W/System.err(4784): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-05 15:24:52.714: W/System.err(4784): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-05 15:24:52.714: W/System.err(4784): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-05 15:24:52.714: W/System.err(4784): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-05 15:24:52.718: W/System.err(4784): at java.lang.Thread.run(Thread.java:1019)
04-05 15:24:52.718: W/System.err(4784): at java.net.URL.<init>(URL.java:273)
04-05 15:24:52.722: W/System.err(4784): at java.net.URL.<init>(URL.java:157)
04-05 15:24:52.722: W/System.err(4784): at org.apache.harmony.xml.ExpatParser.openUrl(ExpatParser.java:753)