ANDROID:解析XML

我不是开发人员,我只是涉猎编程。 我从未理解的一个领域是XML解析。 可悲的是,对于我最新的“项目”,我需要为Android应用程序做这件事。 它是我为工作做的原型。

我有这个XML(一个模拟文件):

      1275243          1077244   1077244       

我有一些代码,让我得到每个的NodeList:

 inputStream = OpenHttpConnection(URL); Document document = null; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder; documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.parse(inputStream); document.getDocumentElement().normalize(); NodeList lineNodes = document.getElementsByTagName("line"); 

我不知道下一步该做什么。 我的代码似乎很长。 我用Google搜索了更好的方法,但是我发现了一些更清晰的代码,我无法开始工作。

有没有好的Android XML教程? 或者有人可以帮助我使用此代码?

我需要将每个进入HashMap,其中String是ID。

Line是显示的所有信息的类。

谢谢尼尔

我宁愿不使用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 feed; private HashMap peroidContent; private HashMap callContent; private HashMap callsMap; private HashMap lineContent; private HashMap 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(); } @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(); peroidContent.put("From", atts.getValue("from")); peroidContent.put("to", atts.getValue("to")); } else if(localName.equalsIgnoreCase("LINE")) { linesMap = new HashMap(); } else if(localName.equalsIgnoreCase("LINE")) { lineContent = new HashMap(); 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(); } else if(localName.equalsIgnoreCase("CALL")) { callContent = new HashMap(); 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 } } 

StackOverflow上有很多关于如何解析XML文件的解释我已经把它留下来了,只是向你展示了更有趣的部分; 内容处理程序。

现在大多数有趣的部分都被评论,这样你就可以理解我正在尝试做什么。

我已经实现了接口ContentHandler只是为了向您展示有更多可用的方法,也许您将来需要其中一种方法。 但是,您可以从类DefaultHandler扩展而只是覆盖所需的方法。 您所做的只是检查某些标签的出现,然后触发某些事件。 如果要保留XML文件中元素的顺序,则只需使用LinkedHashMap而不是HashMap

我希望它有所帮助。