java读取xml文件(Dom,Dom4j)

1.Dom解析XML

package com.fjh.xml;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParseXml {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		//1、获取解析工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//2、解析器
		DocumentBuilder builder = factory.newDocumentBuilder(); //alt  shift l
		//3、获取文档对象
		Document document = builder.parse("product.xml");
		//测试
		System.out.println(document.getElementsByTagName("products"));
		
		//获取products 节点
		Node products = document.getElementsByTagName("products").item(0);
		//获取products节点中所有的子节点
		NodeList childNodes = products.getChildNodes();
		//集合大小
		System.out.println(childNodes.getLength());//5个子节点  包含元素节点和非元素节点(空白节点)
		for(int index = 0; index < childNodes.getLength(); index ++) {
			//获取每一个子节点
			Node node = childNodes.item(index);
			//System.out.println( "节点的名称   "+ node.getNodeName());
			//System.out.println( "节点的类型   "+ node.getNodeType());
			//System.out.println( "节点的值   "+ node.getNodeValue());
			if(node.getNodeType() == Node.ELEMENT_NODE) {
				//获取属性的值 获取名称为num的属性节点
				Node namedItem = node.getAttributes().getNamedItem("num");
				//属性的值
				String nodeValue = namedItem.getNodeValue();
				System.out.println(nodeValue);
				//获取子节点
				NodeList childNodes2 = node.getChildNodes();
				for(int i = 0; i < childNodes2.getLength(); i ++) {
					Node item = childNodes2.item(i);
					System.out.println( "节点的名称   "+ item.getNodeName());
					System.out.println( "节点的类型   "+ item.getNodeType());
					System.out.println( "节点的值   "+ item.getTextContent());
				}
			}
		}
		
	}

}

2.Dom4j

  • 需要下载第三方jar包(Dom4j.jar)
package com.fjh.xml;

import java.io.File;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Dom4jParseXml {

	
	public static void parse(String fileName) throws DocumentException {
		
		
		//解析器
		SAXReader reader = new SAXReader();
		// 获取了文档对象
		Document document = reader.read(new File(fileName));
		//<products>
		Element rootElement = document.getRootElement();
		System.out.println(rootElement.getName());
		//迭代子节点
		for(Iterator<Element> it = rootElement.elementIterator(); it.hasNext();) {
			Element product = it.next();
			//获取属性的值
			System.out.println("获取属性的值" + product.attributeValue("num"));
			
			for(Iterator<Element> it2 = product.elementIterator(); it2.hasNext();) {
				Element item = it2.next();
				//获取属性的值
				System.out.println("获取节点的名称:" + item.getName() + "  节点的值:" + item.getStringValue());
			}
		}
		
	}
	
	
	
	
	
	
	public static void main(String[] args) throws DocumentException {
		
		parse("product.xml");
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值