android操作xml

android操作xml封装后的类,包括创建xml和读xml。

public class XmlParserUtil
{
	//创建xml文件 
	public static void createXmlFile(final String xmlPath)
	{ 
		File xmlFile = new File(xmlPath); 
		FileOutputStream fileOPStream = null; 
		try
		{ 
			fileOPStream = new FileOutputStream(xmlFile); 
		}
		catch (FileNotFoundException e) 
		{ 
			Log.e("FileNotFoundException", "can't create FileOutputStream"); 
		} 

		XmlSerializer serializer = Xml.newSerializer(); 
		try
		{ 
			serializer.setOutput(fileOPStream,"UTF-8"); 
			serializer.startDocument(null, true); 
			serializer.startTag(null, "books"); 

			for(int i = 0; i < 5; i ++)
			{ 
				serializer.startTag(null, "book"); 
				serializer.startTag(null, "bookname"); 
				serializer.text("Android教程" + i); 
				serializer.endTag(null, "bookname"); 
				serializer.startTag(null, "bookauthor"); 
				serializer.text("Frankie" + i); 
				serializer.endTag(null, "bookauthor"); 
				serializer.endTag(null, "book"); 
			} 

			serializer.endTag(null, "books"); 
			serializer.endDocument(); 

			serializer.flush(); 
			fileOPStream.close(); 
		} 
		catch (Exception e) 
		{ 
			Log.e("XmlParserUtil","error occurred while creating xml file"); 
		} 
		Toast.makeText(getApplicationContext(), "创建xml文件成功!", Toast.LENGTH_SHORT).show(); 
	} 

	/** dom解析xml文件 
	* xmlPath xml的路径
	*/
	public static void domParseXML(final String xmlPath)
	{ 
		File file = new File(xmlPath); 
		if(!file.exists()||file.isDirectory())
		{
			Log.e("domParseXML", "file not exists");
			return;
		}
			
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
		DocumentBuilder db = null; 
		try 
		{ 
			db = dbf.newDocumentBuilder(); 
		} 
		catch (ParserConfigurationException e) 
		{ 
			e.printStackTrace(); 
		} 

		Document doc = null; 
		try 
		{ 
			doc = db.parse(file); 
		} 
		catch (SAXException e) 
		{ 
			e.printStackTrace(); 
		} 
		catch (IOException e)
		{ 
			e.printStackTrace(); 
		} 

		Element root = doc.getDocumentElement(); 
		NodeList books = root.getElementsByTagName("book"); 
		String res = "本结果是通过dom解析:" + "\n"; 

		for(int i = 0; i < books.getLength();i++)
		{ 
			Element book = (Element)books.item(i); 
			Element bookname = (Element)book.getElementsByTagName("bookname").item(0); 
			Element bookauthor = (Element)book.getElementsByTagName("bookauthor").item(0); 
			res += "书名: " + bookname.getFirstChild().getNodeValue() + " " + 
					"作者: " + bookauthor.getFirstChild().getNodeValue() + "\n"; 
		} 

	} 

	/** xmlPullParser解析xml文件 
	* xmlPath xml的路径
	*/
	public static void xmlPullParseXML(final String xmlPath)
	{ 
		String res = "本结果是通过XmlPullParse解析:" + "\n"; 
		try
		{ 
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
			XmlPullParser xmlPullParser = factory.newPullParser(); 
			try
			{
				xmlPullParser.setInput(new StringReader(bufferedReaderFile(xmlPath)));
			}
			catch (Exception e) 
			{
				Log.e("xmlPullParseXML", e.toString());
			}

			int eventType = xmlPullParser.getEventType(); 
			try
			{ 
				while (eventType != XmlPullParser.END_DOCUMENT)
				{ 
					String nodeName = xmlPullParser.getName(); 
					switch (eventType)
					{ 
					case XmlPullParser.START_TAG: 
						if("bookname".equals(nodeName))
						{ 
							res += "书名: " + xmlPullParser.nextText() + " "; 
						}
						else if("bookauthor".equals(nodeName))
						{ 
							res += "作者: " + xmlPullParser.nextText() + "\n"; 
						} 
						break; 

					default: 
						break; 
					} 
					eventType = xmlPullParser.next(); 
				} 
			} 
			catch (IOException e)
			{ 
				e.printStackTrace(); 
			} 
		} 
		catch (XmlPullParserException e)
		{ 
			e.printStackTrace(); 
		} 
	} 
	
	//从sd卡中读取xml文件的内容
	private String bufferedReaderFile(final String path) throws IOException
	{
		File file=new File(path);
		if(!file.exists()||file.isDirectory())
			throw new FileNotFoundException();

		BufferedReader br=new BufferedReader(new FileReader(file));
		String temp=null;
		StringBuffer sb=new StringBuffer();
		temp=br.readLine();
		while(temp!=null)
		{
			sb.append(temp+" ");
			temp=br.readLine();
		}
		br.close();

		return sb.toString();
	}

}


 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

singwhatiwanna

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值