我不是开发人员,我只是涉猎编程.我从未理解的一个领域是XML解析.可悲的是,对于我最新的"项目",我需要为Android应用程序做这件事.它是我为工作做的原型.
我有这个XML(一个模拟文件):
<feed version="201010011221" >
<period from="2010-10-01T10:08:34Z" to="2010-10-01T10:08:34Z">
<lines>
<line id="SKI" name="Ski" shortname="ski" status="1 calls queued">
<calls>
<call id="6584" created="2010-10-01T11:22:42Z">
<booking>1275243</booking>
</call>
</calls>
</line>
<line id="CRU" name="Cruise" shortname="cruise" status="0 calls queued">
<calls />
</line>
<line id="VIL" name="Villas" shortname="villas" status="2 calls queued">
<calls>
<call id="25878" created="2010-10-01T10:22:42Z">
<booking>1077244</booking>
</call>
<call id="25878" created="2010-10-01T10:22:42Z">
<booking>1077244</booking>
</call>
</calls>
</line>
</lines>
</period>
</feed>
Run Code Online (Sandbox Code Playgroud)
我有一些代码,让我得到每个的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");
Run Code Online (Sandbox Code Playgroud)
我不知道下一步该做什么.我的代码似乎很长.我用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<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
}
}
Run Code Online (Sandbox Code Playgroud)
StackOverflow上有很多关于如何解析XML文件的解释,我已经把它留下来了,只是向你展示了更有趣的部分; 内容处理程序.
现在,大多数有趣的部分都会被评论,以便您了解我正在尝试做什么.
我已经实现了界面,ContentHandler只是为了向您展示有更多可用的方法,也许您将来需要其中一种方法.但是,您可以从类扩展DefaultHandler而只是覆盖所需的方法.您所做的只是检查某些标签的出现,然后触发某些事件.如果要保留XML文件中元素的顺序,则只需使用a LinkedHashMap而不是a HashMap.
我希望它有所帮助.
| 归档时间: |
|
| 查看次数: |
5787 次 |
| 最近记录: |