Dom4j解析XML文本,遍历指定节点下的节点内容和属性
Dom4j简单介绍
dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的。dom4j是一个十分优秀的JavaXML API,具有性能优异、功能强大和极其易使用的特点,它的性能超过sun公司官方的dom技术,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。
Dom4j基本知识点
1.读取XML文件,获得document对象
SAXReader reader = new SAXReader();
Document document = reader.read(new File("csdn.xml"));
2.解析XML形式的文本,得到document对象.
String text = "";
Document document = DocumentHelper.parseText(text);
3.主动创建document对象.
Document document = DocumentHelper.createDocument(); //创建根节点
Element root = document.addElement("csdn");
1.获取文档的根节点.
Element root = document.getRootElement();
2.取得某个节点的子节点.
Element element=node.element(“四大名著");
3.取得节点的文字
String text=node.getText();
4.取得某节点下所有名为“FourFamousNovels”的子节点,并进行遍历.
List nodes = rootElm.elements("FourFamousNovels");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
}
5.对某节点下的所有子节点进行遍历.
for(Iterator it=root.elementIterator();it.hasNext();){
Element element = (Element) it.next();
}
6.在某节点下添加子节点
Element elm = newElm.addElement("水浒传");
7.设置节点文字.elm.setText("及时雨-宋江");
8.删除某节点.
9.添加一个CDATA节点.Element contentElm = infoElm.addElement("content");contentElm.addCDATA(“cdata区域”);
1.取得某节点下的某属性:Element root=document.getRootElement();
Attribute attribute=root.attribute("id");
2.取得属性的文字:String text=attribute.getText();
3.删除某属性:Attribute attribute=root.attribute("size");
root.remove(attribute);
4.遍历某节点的所有属性:Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute = (Attribute) it.next();
String text=attribute.getText();
System.out.println(text);
}
5.设置某节点的属性和文字:newMemberElm.addAttribute("name","四大名著");
6.设置属性的文字:Attribute attribute=root.attribute("name");
attribute.setText("水浒传");
实例:遍历指定节点下的节点内容和属性
/**
* @方法功能描述: 读取XML形式的文本,获取document对象
* @方法返回类型:Document
* @param xmlText
* xml字符串
* @return
*/
public static Document getDocumentFromText(String xmlText) {
Document document = null;
try {
document = DocumentHelper.parseText(xmlText);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/**
* @方法功能描述: 根据document对象获取根节点
* @方法返回类型:Element
* @param document
* @return
*/
public static Element getRootElement(Document document) {
Element element = null;
if (Objects.isNotNull(document)) {
element = document.getRootElement();
}
return element;
}
/**
* @方法功能描述: 根据DOM树路径,获取指定节点的子节点(包含该指定节点)
* @方法返回类型:List
* @param xmlText
* @param xPath
* @return
*/
@SuppressWarnings("unchecked")
public static List getAppointedElementList(String xmlText, String xPath) {
List elements = new ArrayList();
Document document = getDocumentFromText(xmlText);
Element rooElement = getRootElement(document);
if (Objects.isNull(xPath) || Objects.isEmpty(xPath)) {
elements.add(rooElement);
return elements;
} else {
elements = rooElement.selectNodes(xPath);
if (elements.size() == 0) {
logger.error("xPath(DOM树路径)出现错误!");
}
}
return elements;
}
/**
* 遍历当前节点元素下面的所有(元素的)子节点
*
* @param node
*/
public static void arrayNodes(Map nodeMap, Element node, Element root) {
System.out.println("----------------------------");
System.out.println("当前节点名称:" + node.getName());
if (!(node.getTextTrim().equals(""))) {
System.out.println("当前节点的内容:" + node.getTextTrim());
}
String nodeName = node.getName();
String nodeValue = node.getTextTrim();
nodeMap.put(nodeName, nodeValue);
@SuppressWarnings("unchecked")
Iterator it = node.elementIterator();
while (it.hasNext()) {
Element e = it.next();
arrayNodes(nodeMap, e, root);
}
}
- 解析XML,并将节点为KEY,值为VALUE,存入Map
/**
* @方法功能描述: 解析XML,并将节点为KEY,值为VALUE,存入Map
* @方法返回类型:Map
* @param xmlText
* @param xPath
* @return
*/
@SuppressWarnings("unchecked")
public static Map getAppointedElementTextMap(String xmlText, String xPath) {
Map elementMap = new HashMap();
List elements = new ArrayList();
Document document = getDocumentFromText(xmlText);
Element rooElement = getRootElement(document);
if (Objects.isNull(xPath) || Objects.isEmpty(xPath)) {
elements.add(rooElement);
} else {
elements = rooElement.selectNodes(xPath);
if (Objects.isNotNull(elements)) {
for (Element element : elements) {
arrayNodes(elementMap, element, rooElement);
}
}
if (elements.size() == 0) {
logger.error("xPath(DOM树路径)出现错误!");
}
}
return elementMap;
}
- 解析XML,并将节点为KEY,值为VALUE,存入List
/**
* @方法功能描述: 解析XML,并将节点为KEY,值为VALUE,存入List
@SuppressWarnings("unchecked")
public static List