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

📄 xmlparse.java

📁 XML解析解析XML解析解析XML解析解析
💻 JAVA
字号:
package parse;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Text;
public class XmlParse {

	public Document getDocument(File f) {
		Document doc = null;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(f);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return doc;
	}

	public void getAllElement(Document doc) {
		// 获得所有元素的名称
		NodeList nodelist = doc.getElementsByTagName("*");
		int length = nodelist.getLength();
		Element element = null;
		Node txtNode = null;
		Text text = null;
		for (int i = 0; i < length; i++) {
			element = (Element) nodelist.item(i);
//			txtNode = element.getFirstChild();
//			System.out.println("元素名称:" + element.getNodeName());
//			System.out.println("元素的值:"+txtNode.getNodeValue());
//			System.out.println("元素文本内容:"+element.getTextContent());
			text = (Text)element.getFirstChild();
			//System.out.println("文本名称:"+text.getNodeName());
			System.out.println("文本的值:"+text.getNodeValue());
			System.out.println("<---------------------------->");
		}
	}

	public void getAllNode(Document doc) {
		NodeList nodelist = doc.getElementsByTagName("*");
		int length = nodelist.getLength();
		Node node = null;
		Node txtNode  = null;
		for (int i = 0; i < length; i++) {
			node = nodelist.item(i);
			System.out.println("节点名称: " + node.getNodeName());
			txtNode = node.getFirstChild();
			System.out.println("文本节点值: "+txtNode.getNodeValue());
		}
	}
	//获得某类元素(节点)的所有属性
	public void getNodeAttr(Document doc,String nodeName)
	{
		NodeList nodelist  = doc.getElementsByTagName(nodeName);
		int length =nodelist.getLength();
		Node node  = null;
		for (int i = 0; i < length; i++) {
			node = nodelist.item(i);
			NamedNodeMap attrMap = node.getAttributes();
			int attrLength = attrMap.getLength();
			Attr attr = null;
			for (int j = 0; j < attrLength; j++) {
			attr =(Attr)attrMap.item(j);
				System.out.println("属性名称:"+attr.getName());
				System.out.println("属性的值:"+attr.getValue());
			}
		}
	}
	public void getElementAttr(Document doc,String elementName)
	{
		NodeList nodelist = doc.getElementsByTagName(elementName);
		int length = nodelist.getLength();
		Element element = null;
		for (int i = 0; i < length; i++) {
			element = (Element)nodelist.item(i);
			NamedNodeMap map = element.getAttributes();
			int maplength = map.getLength();
			Attr attr = null;
			for (int j = 0; j < maplength; j++) {
				attr = (Attr) map.item(j);
				System.out.println("属性名称:"+attr.getName());
				System.out.println("属性的值:"+attr.getValue());
			}
		}
	}
	public void getAllChildNode(Document doc,String id)
	{
		NodeList nodelist = doc.getElementsByTagName("table");
		int length = nodelist.getLength();
		Node node = null;
		Node tempNode  = null;
		boolean flag = false;
		for (int i = 0; i < length; i++) {
			node = nodelist.item(i);
			NamedNodeMap nodeMap = node.getAttributes();
			int attrLength = nodeMap.getLength();
			String attrValue = null;
			for (int j = 0; j < attrLength; j++) {
				Attr  attr  =  (Attr)nodeMap.item(j);
				attrValue = attr.getValue();
				if(attrValue.equals(id))
				{
					tempNode = node;
					flag =true;
				}
			}
			if(flag)
			{
				break;
			}
		}
		NodeList childs = 
	}

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		File f = new File("src\\xml\\dept.xml");
		XmlParse parse = new XmlParse();
		Document doc = parse.getDocument(f);
		parse.getElementAttr(doc, "table");
		parse.getAllElement(doc);
	}

}

⌨️ 快捷键说明

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