XML数据解析之DOM解析


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);
				}
				
			}
		});
	}

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念