XML数据解析之DOM解析
发布时间:2020-12-16 05:06:57 所属栏目:百科 来源:网络整理
导读:private void domParserXml(InputStream xmlInputStream) {Log.i(TAG,"begin domParserXml");try {DocumentBuilderFactory xmlBuilderFactory=DocumentBuilderFactory.newInstance();DocumentBuilder xmlBuilder= xmlBuilderFactory.newDocumentBuilder();if
private void domParserXml(InputStream xmlInputStream) { Log.i(TAG,"begin domParserXml"); try { DocumentBuilderFactory xmlBuilderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder xmlBuilder= xmlBuilderFactory.newDocumentBuilder(); if(xmlInputStream==null){ Log.e(TAG,"domParserXml xmlInputStream==null"); return; } Document xml=xmlBuilder.parse(xmlInputStream); //获取根节点 Element rootElement= xml.getDocumentElement(); parserRoot(rootElement); } catch (SAXException e) { Log.w(TAG,"SAXException"); } catch (IOException e) { Log.w(TAG,"IOException"); }catch (ParserConfigurationException e) { Log.w(TAG,"ParserConfigurationException"); } } String monthvalue=null; String dayvalue=null; private void parserRoot(Element rootElement) { NodeList nList=rootElement.getChildNodes(); int len=nList.getLength(); for (int i = 0; i < len; i++) { Node xmlNode=nList.item(i); String nameString=xmlNode.getNodeName(); short type=xmlNode.getNodeType(); if(nameString.equals("month")||nameString.equals("day")){ monthvalue=xmlNode.getAttributes().getNamedItem("value").getNodeValue(); //Log.i(TAG,nameString +"value="+value); } if(nameString.equals("day")){ dayvalue=xmlNode.getAttributes().getNamedItem("value").getNodeValue(); //Log.i(TAG,nameString +"value="+value); } if(nameString.equals("bill")){ String starttime=xmlNode.getAttributes().getNamedItem("starttime").getNodeValue(); String endtime=xmlNode.getAttributes().getNamedItem("endtime").getNodeValue(); String name=xmlNode.getAttributes().getNamedItem("name").getNodeValue(); String value1=xmlNode.getAttributes().getNamedItem("value").getNodeValue(); ContentValues values=new ContentValues(); //values.put("_id",1); values.put("month",monthvalue); values.put("day",dayvalue); values.put("start",starttime); values.put("end",endtime); values.put("name1",name); values.put("value",value1); getContentResolver().insert(Uri.parse("content://com.stamars.sqlite.sqlite/channel"),values); //Log.i(TAG,starttime+" "+starttime+" endtime "+endtime+" name "+name+" value "+value1); } //Log.i(TAG,"name="+nameString+",type="+type+",value="+value+",hasattr="+xmlNode.hasAttributes()); if(xmlNode.hasChildNodes()&&xmlNode.getNodeType()==Document.ELEMENT_NODE) parserRoot((Element) xmlNode); } } }); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Swift:一个排序的字典
- core-data – 将普通的sqlite3数据库迁移到核心数据?
- OO设计原则 -- Liskov Substitution Principle: OO设计的LS
- 为什么在WCF中忽略XmlRoot属性以及如何克服这个问题
- Swift - 网络请求报App Transport Security has blocked a
- Ajax异步添加记录和手动修改页码时遇到的问题
- 正则表达式、扩展正则表达式
- 受保护的枚举在C#中不可能
- Flex DataGrid使用itemRenderer后,拖动垂直滚动条,TextIn
- Solr DataImportHandler:我可以使用XPathEntityProcessor从