DOM解析XML

待解析的XML

<?xml version="1.0" encoding="utf-8"?>
<musices>
	<music>
		<name>Hello</name>
		<size>8622</size>
	</music>
	<music>
		<name>World</name>
		<size>200</size>
	</music>
</musices>	

 

File f=new File("E:\\music.xml");
	
	DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
	
	DocumentBuilder builder=factory.newDocumentBuilder();
	
	Document doc = builder.parse(f);

	
	NodeList nl = doc.getElementsByTagName("music");//获得名称为music的标签
	for (int i=0;i<nl.getLength();i++){
	System.out.print("歌曲名称" + doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue());
	System.out.print("歌曲大小:" + doc.getElementsByTagName("size").item(i).getFirstChild().getNodeValue());}

你可能感兴趣的:(dom解析xml)