我ñ寧可不使用DOM,因爲它有一個更大的內存佔用。使用DOM時,首先將整個XML結構加載到內存中,然後處理它。這可能不是移動開發的最佳解決方案。
使用Android自帶的SAX解析器。這是一種事件驅動的方法。每個起始標籤,標籤之間的內容以及發生時結束標籤觸發事件。事實上它可以處理更多的事件,但這些是最常用的事件。這意味着SAX解析器會逐一處理每一行,而不會首先將整個XML結構加載到內存中。
明天我會爲你的特定問題發佈一個例子。
編輯:這是承諾的內容處理程序示例。
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
public class MyContentHandler implements ContentHandler {
private HashMap<String, Object> feed;
private HashMap<String, Object> peroidContent;
private HashMap<String, Object> callContent;
private HashMap<String, Object> callsMap;
private HashMap<String, Object> lineContent;
private HashMap<String, Object> linesMap;
private String text;
private String callId;
private String lineId;
@Override
public void startDocument() throws SAXException {
/* You can perform some action in this method
* for example to reset some sort of Collection
* or any other variable you want. It gets called
* every time a document starts. */
feed = new HashMap<String, Object>();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
// Gets called every time an opening tag is encountered.
if(localName.equalsIgnoreCase("FEED")) {
/* We've found a "feed" opening tag so we capture its
* version attribute and put it into our HashMap.*/
feed.put("Version", atts.getValue("version"));
} else if(localName.equalsIgnoreCase("PEROID")) {
peroidContent = new HashMap<String, Object>();
peroidContent.put("From", atts.getValue("from"));
peroidContent.put("to", atts.getValue("to"));
} else if(localName.equalsIgnoreCase("LINE")) {
linesMap = new HashMap<String, Object>();
} else if(localName.equalsIgnoreCase("LINE")) {
lineContent = new HashMap<String, Object>();
lineId = atts.getValue("id");
lineContent.put("name", atts.getValue("name"));
lineContent.put("shortname", atts.getValue("shortname"));
lineContent.put("status", atts.getValue("status"));
} else if(localName.equalsIgnoreCase("CALLS")) {
callsMap = new HashMap<String, Object>();
} else if(localName.equalsIgnoreCase("CALL")) {
callContent = new HashMap<String, Object>();
callId = atts.getValue("Id");
callContent.put("created", atts.getValue("created"));
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
/* Gets called every time in between an opening tag and
* a closing tag if characters are encountered. */
text = new String(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// Gets called every time a closing tag is encountered.
if(localName.equalsIgnoreCase("FEED")) {
feed.put("Peroid", peroidContent);
} else if(localName.equalsIgnoreCase("PEROID")) {
peroidContent.put("Lines", linesMap);
} else if(localName.equalsIgnoreCase("LINES")) {
linesMap.put(lineId, lineContent);
} else if(localName.equalsIgnoreCase("LINE")) {
lineContent.put("Calls", callsMap);
} else if(localName.equalsIgnoreCase("CALLS")) {
callsMap.put(callId, callContent);
} else if(localName.equalsIgnoreCase("BOOKING")) {
callContent.put("Booking", text.toString());
}
}
@Override
public void endDocument() throws SAXException {
/* You can perform some action in this method
* for example to reset some sort of Collection
* or any other variable you want. It gets called
* every time a document end is reached. */
SAXParsingFun.setHashMap(feed);
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void processingInstruction(String target, String data)
throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
@Override
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
// TODO Auto-generated method stub
}
}
有很多關於如何解析XML文件,在計算器上的解釋我已經離開了這一點,只是顯示你的更有趣的部分;內容處理程序。
現在大多數有趣的部分都有評論,所以你可以理解我正在嘗試做什麼。
我已經實現了接口ContentHandler
只是爲了向您說明有更多的方法可用,也許您將來需要其中的一種方法。但是,您可以從類DefaultHandler
延伸,只需覆蓋所需的方法。你所做的只是檢查某些標籤的發生,然後觸發某些事件。如果要保留XML文件中元素的順序,則只需使用LinkedHashMap
而不是HashMap
。
我希望它有幫助。
http://stackoverflow.com/questions/4827344/how-to-parse-xml-using-the-sax-parser – hakre 2014-10-21 19:42:41