⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdomxml.java

📁 java中关于XML的解析
💻 JAVA
字号:
package temp;

import javax.xml.parsers.*;
import org.w3c.dom.*;

/**该程序是在java中实现读xml文件的程序,读入的文件名为"result/rDomXml.xml"
   其中的classpath需要加入crimson.jar路径。**/

public class rDomXml
{
	public static void main(String args[])throws Exception
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder bulider=factory.newDocumentBuilder();
		Document doc=bulider.parse("result/rDomXml.xml");	//读rDomXml.xml文件
		
		NodeList nl=doc.getElementsByTagName("Book");	//计算Book属性的个数
		for(int i=0;i<nl.getLength();i++){
			Element node=(Element)nl.item(i);
			//读入各项属性
			System.out.println("BookName is :" + 
								node.getElementsByTagName("bookName").item(0).getFirstChild().getNodeValue().trim());

			System.out.println("BookAuthor is :" + 
								node.getElementsByTagName("bookAuthor").item(0).getFirstChild().getNodeValue().trim());

			System.out.println("BookISBN is :" + 
								node.getElementsByTagName("bookISBN").item(0).getFirstChild().getNodeValue().trim());

			System.out.println("BookPrice is :" + 
								node.getElementsByTagName("bookPrice").item(0).getLastChild().getNodeValue().trim()+"\n\n");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -