XML解析:使用原生DOM解析xml文件

1.前提

本次使用的users.xml是使用的dom4j中的users.xml文件

2.创建自定的工具用来解析当前xml文件

**
 * @description 自定义工具用来解析获得当前的xml的内容
 * @author hy
 * @date 2019-10-23
 */
public class MyDomXmlUtils {
	public static final String ATTRIBUTE_NEED_STRING="\"";
	public static final String ATTRIBUTE_SPLIT_STRING="=";

	//转换流获取当前rootElement中的数据
	public static Element getRootElement(Document document) throws ParserConfigurationException, SAXException, IOException {
		return document.getDocumentElement();	
	}
	
	//通过结点获取当前结点中的文本值
	public static String getElementTextValue(Element element) {
		return DomUtils.getTextValue(element);
	}
	
	//获取当前结点的所有的子节点
	public static List<Element> getChildElement(Element element){
		return DomUtils.getChildElements(element);
	}
	
	//通过当前的元素结点和标签名称获取当前的所有的结点
	public static List<Element> getChildElementByTagName(Element element,String... tagNames){
		return DomUtils.getChildElementsByTagName(element, tagNames);
	}
	
	//使用当前的工厂对象创建当前的xml对应的dom对象
	public static Document getDocument(InputStream is) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document document = documentBuilder.parse(is);
		return document;
	}
	
	//通过当前的标签名获取所有的标签中的内容
	public static String[] getTextByTagName(Element rootElement,String tagName) {
		List<Node> nodeList = getNodesByTagName(rootElement,tagName);
		String[] texts=new String[nodeList.size()];
		for (int i = 0; i <nodeList.size(); i++) {
			Node item = nodeList.get(i);
			texts[i]=getTextByNode(item);
		}
		return texts;
	}
	
	//获取当前结点的文本内容
	public static String getTextByNode(Node node) {
		return node.getTextContent();
	}
	
	//通过当前的标签名获取当前的标签结点
	public static List<Node> getNodesByTagName(Element rootElement,String tagName) {
		NodeList nodeList = rootElement.getElementsByTagName(tagName);
		return getAllNode(nodeList);
	}
	
	//通过结点列表获取所有的结点集合
	public static List<Node> getAllNode(NodeList nodeList){
		int length = nodeList.getLength();
		if(length==0) {
			return new ArrayList<Node>(0);
		}
		List<Node> nodes=new ArrayList<Node>(length);
		for (int i = 0; i <length; i++) {
			Node item = nodeList.item(i);
			nodes.add(item);
		}
		return nodes;
	}
	
	//获取当前的所有的结点集合
	public static List<Node> getAllNode(Element rootElement){
		NodeList childNodes = rootElement.getChildNodes();
		return getAllNode(childNodes);
	}
	
	
	//通过当前的结点获取当前结点中属性的名称
	public String[] getAllAttributeNameByNode(Node node) {
		Map<String, String> allAttributeByNode = getAllAttributeByNode(node);
		Set<String> keySet = allAttributeByNode.keySet();
		return keySet.toArray(new String[keySet.size()]);
	}
	
	//通过当前的结点或当前结点的属性
	public static Map<String,String> getAllAttributeByNode(Node node){
		NamedNodeMap namedNodeMap = node.getAttributes();
		int length = namedNodeMap.getLength();
		if(length==0) {
			return new HashMap<String, String>(0);
		}
		Map<String,String> attributes=new HashMap<String, String>();
		for (int i = 0; i < length; i++) {
			Node attributeNode = namedNodeMap.item(i);
			String attrString= attributeNode.toString();
			int indexOf = attrString.indexOf(ATTRIBUTE_SPLIT_STRING);
			int left = attrString.indexOf(ATTRIBUTE_NEED_STRING);
			int right = attrString.lastIndexOf(ATTRIBUTE_NEED_STRING);
			if(indexOf!=-1) {
				String key = attrString.substring(0, indexOf);
				String value = attrString.substring(left+1,right);
				attributes.put(key, value);
			}
		}
		return attributes;
	}
	
	//显示所有的结点
	public static void showNodeElement(NodeList nodeList,String tag) {
		if(nodeList==null||nodeList.getLength()==0) {
			return;
		}
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node item = nodeList.item(i);
			if("#text".equals(item.getNodeName())) {
				continue;
			}
			
			if(!item.getNodeName().equals(tag)) {
				System.out.println(item.getNodeName());
				System.out.println(item.getTextContent());
			}
			
			if(!item.hasChildNodes()) {
				continue;
			}else {
				NodeList childNodes = item.getChildNodes();
				showNodeElement(childNodes,tag);
			}
		}
		
	}
}

以上使用了一个DOMUtils类,这是一个我自己封装的类,具体代码就不显示了

3.编写测试用例

@Test
	public void test() throws ParserConfigurationException, SAXException, IOException {
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("users.xml");
		Document document = MyDomXmlUtils.getDocument(is);
		MyDomXmlUtils.showNodeElement(document.getChildNodes(), "user");
	}

4.结果

在这里插入图片描述

发现当前存在一些问题,有字体飘出来了

5.总结

1.使用原生的DOM解析当前的xml比那个dom4j要困难的多,语法什么的都要自己定义,并且一不小心就容易弄错

2.当前DOM解析后的对象相对来所比较复杂

以上纯属个人见解,如有问题请联系本人!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值