java xml dom4j 解析_XML文件解析java工具类dom4J使用实例

package com.declan.mibBrowser.util;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.Node;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

/**

* 使用dom4j解析xml 工具类

* @author Declan

*/

public class XmlParser {

/**

* 生成空的xml文件

*

* @param xmlPath

* @return Document

*/

public static void createEmptyXmlFile(String xmlPath) {

if (xmlPath == null || xmlPath.equals("")) {

return ;

}

Document document = DocumentHelper.createDocument();

// 将doc转换为xml文档

doc2XML(document, xmlPath);

}

/**

* 将doc转换为xml文档

*

* @param doc

* @param fileName 文件名

*/

public static void doc2XML(Document doc, String fileName) {

OutputFormat format = OutputFormat.createPrettyPrint();

format.setEncoding("UTF-8");

XMLWriter writer = null;

try {

writer = new XMLWriter(new FileWriter(new File(fileName)), format);

writer.write(doc);

writer.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 根据xml文件路径取得document对象

*

* @param xmlPath

* @return Document

*/

public static Document getDocument(String xmlPath) {

if (xmlPath == null || xmlPath.equals("")) {

return null;

}

File file = new File(xmlPath);

if (!file.exists()) {

return null;

}

SAXReader reader = new SAXReader();

Document document = null;

try {

document = reader.read(xmlPath);

} catch (DocumentException e) {

e.printStackTrace();

}

return document;

}

/**

* 得到XML的根节点

*

* @param document

* @return

*/

public static Element getRootNode(Document document) {

if (document == null) {

return null;

}

Element root = document.getRootElement();

return root;

}

/**

* 根据路径直接拿到根节点

*

* @param xmlPath

* @return Element

*/

public static Element getRootNode(String xmlPath) {

if (xmlPath == null || (xmlPath.trim()).equals("")) {

return null;

}

Document document = getDocument(xmlPath);

if (document == null) {

return null;

}

return getRootNode(document);

}

/**

* 得到指定元素的迭代器

*

* @param parent

* @return Iterator

*/

public static Iterator getIterator(Element parent) {

if (parent == null) {

return null;

}

Iterator iterator = parent.elementIterator();

return iterator;

}

/**

* 根据子节点名称得到指定的子节点

*

* @param parent

* @param childName

* @return List

*/

public static List getChildElements(Element parent, String childName) {

childName = childName.trim();

if (parent == null) {

return null;

}

childName += "//";

List childElements = parent.selectNodes(childName);

return childElements;

}

/**

* 根据一个节点获取他所有的子节点

*

* @param node

* @return List

*/

public static List getChildList(Element node) {

if (node == null) {

return null;

}

Iterator itr = getIterator(node);

if (itr == null) {

return null;

}

List childList = new ArrayList();

while (itr.hasNext()) {

Element kidElement = itr.next();

if (kidElement != null) {

childList.add(kidElement);

}

}

return childList;

}

/**

* 查询没有子节点的节点,使用xpath方式

*

* @param parent

* 父节点名称

* @param nodeNodeName

* 子节点名称

* @return

*/

public static Node getSingleNode(Element parent, String nodeNodeName) {

nodeNodeName = nodeNodeName.trim();

String xpath = "//";

if (parent == null) {

return null;

}

if (nodeNodeName == null || nodeNodeName.equals("")) {

return null;

}

xpath += nodeNodeName;

Node kid = parent.selectSingleNode(xpath);

return kid;

}

/**

* 得到子节点,不使用xpath

*

* @param parent

* @param childName

* @return Element

*/

public static Element getChild(Element parent, String childName) {

childName = childName.trim();

if (parent == null) {

return null;

}

if (childName == null || childName.equals("")) {

return null;

}

Element e = null;

Iterator it = getIterator(parent);

while (it != null && it.hasNext()) {

Element k = (Element) it.next();

if (k == null) {

continue;

}

if (k.getName().equalsIgnoreCase(childName)) {

e = k;

break;

}

}

return e;

}

/**

* 判断节点是否还有子节点

*

* @param element

* @return boolean

*/

public static boolean hasChild(Element element) {

if (element == null) {

return false;

}

return element.hasContent();

}

/**

* 得到指定节点的属性的迭代器

*

* @param element

* @return Iterator

*/

public static Iterator getAttrIterator(Element element) {

if (element == null) {

return null;

}

Iterator attrIterator = element.attributeIterator();

return attrIterator;

}

/**

* 遍历指定节点的所有属性

*

* @param element

* @return 节点属性的list集合

*/

public static List getAttributeList(Element element) {

if (element == null) {

return null;

}

List attributeList = new ArrayList();

Iterator atrIterator = getAttrIterator(element);

if (atrIterator == null) {

return null;

}

while (atrIterator.hasNext()) {

Attribute attribute = atrIterator.next();

attributeList.add(attribute);

}

return attributeList;

}

/**

* 得到指定节点的指定属性

*

* @param element

* @param attrName

* @return Attribute

*/

public static Attribute getAttribute(Element element, String attrName) {

attrName = attrName.trim();

if (element == null) {

return null;

}

if (attrName == null || attrName.equals("")) {

return null;

}

Attribute attribute = element.attribute(attrName);

return attribute;

}

/**

* 获取指定节点指定属性的值

*

* @param element

* @param attrName

* @return String 属性值

*/

public static String attrValue(Element element, String attrName) {

attrName = attrName.trim();

if (element == null) {

return null;

}

if (attrName == null || attrName.equals(""))

return null;

return element.attributeValue(attrName);

}

/**

* 得到指定节点的所有属性及属性值

*

* @param element

* @return Map 属性名--属性值

*/

public static Map getNodeAttrMap(Element element) {

Map attrMap = new HashMap();

if (element == null) {

return null;

}

List attributes = getAttributeList(element);

if (attributes == null) {

return null;

}

for (Attribute attribute : attributes) {

String attrValueString = attrValue(element, attribute.getName());

attrMap.put(attribute.getName(), attrValueString);

}

return attrMap;

}

/**

* 遍历指定节点的下没有子节点的元素的text值

*

* @param element

* @return Map 节点名--text值

*/

public static Map getSingleNodeText(Element element) {

Map map = new HashMap();

if (element == null) {

return null;

}

List kids = getChildList(element);

for (Element tempElement : kids) {

if (tempElement.getTextTrim() != null) {

map.put(tempElement.getName(), tempElement.getTextTrim());

}

}

return map;

}

/**

* 遍历根节点下,没有子节点的元素节点,并将此节点的text值放入map中返回

*

* @param xmlFilePath

* @return Map

*/

public static Map getSingleNodeText(String xmlFilePath) {

xmlFilePath = xmlFilePath.trim();

if (xmlFilePath == null || xmlFilePath.equals("")) {

return null;

}

Element rootElement = getRootNode(xmlFilePath);

if (rootElement == null || !hasChild(rootElement)) {

return null;

}

return getSingleNodeText(rootElement);

}

/**

* 得到指定节点下所有子节点的属性集合

*

* @param parent

* @return Map

*/

public static Map getNameNodeAllKidsAttributeMap(Element parent) {

Map allAttrMap = new HashMap();

if (parent == null)

return null;

List childlElements = getChildList(parent);

if (childlElements == null)

return null;

for (int i = 0; i < childlElements.size(); i++) {

Element childElement = childlElements.get(i);

Map attrMap = getNodeAttrMap(childElement);

allAttrMap.put(i, attrMap);

}

return allAttrMap;

}

/**

* 遍历指定的节点下所有的节点 (递归)

*

* @param element

* @param allkidsList

* @return List

*/

public static List ransack(Element element, List allkidsList) {

if (element == null)

return null;

if (hasChild(element)) {

List kids = getChildList(element);

for (Element e : kids) {

allkidsList.add(e);

ransack(e, allkidsList);

}

}

return allkidsList;

}

/**

* 得到指定节点下的指定节点集合

*

* @param element

* @param nodeName

* @return List

*/

public static List getNameElement(Element element, String nodeName) {

nodeName = nodeName.trim();

List kidsElements = new ArrayList();

if (element == null) {

return null;

}

if (nodeName == null || nodeName.equals("")) {

return null;

}

List allKids = ransack(element, new ArrayList());

if (allKids == null) {

return null;

}

for (int i = 0; i < allKids.size(); i++) {

Element kid = allKids.get(i);

if (nodeName.equals(kid.getName())) {

kidsElements.add(kid);

}

}

return kidsElements;

}

/**

* 验证节点是否唯一

*

* @param element

* @return int 节点唯一返回1,节点不唯一返回大于一的整型数据

*/

public static int validateSingle(Element element) {

int j = 1;

if (element == null) {

return j;

}

Element parent = element.getParent();

List kids = getChildList(parent);

for (Element kid : kids) {

if (element.equals(kid)) {

j++;

}

}

return j;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值