Java利用dom4j解析XML任意节点和属性
生活随笔
收集整理的這篇文章主要介紹了
Java利用dom4j解析XML任意节点和属性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
分享一個Java解析XML字符串的方法,利用了dom4j,遞歸。可解析任意節點及節點屬性值。
package test; import java.io.IOException; import java.io.StringWriter; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Set;import org.dom4j.*; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class XMLUtils {HashMap<String, String> nodeMap=new HashMap<String,String>();HashMap<String, String> attributeMap=new HashMap<String,String>();String fileNodeName="";/*** @author: shen* @date : 2019年2月22日 下午3:54:56* @Title : getNodeValue * @Description: 獲取xml中指定節點名字的值 * @param xml字符串* @param nodeName 節點名字* @return nodeValue 節點值 */public String getNodeValue(String xml, String nodeName) {Document document = null;String value = "";nodeMap.clear(); //先清空,如果想加快效率,就一個請求結果存一次map,然后再從map里取,用完就清空attributeMap.clear(); //先清空try {document = DocumentHelper.parseText(xml);Element root = document.getRootElement();getNodes(root);value=nodeMap.get(nodeName);//System.out.println("---->"+value);} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}return value;}/*** @author: shen* @date : 2019年2月22日 下午3:55:56* @Title : getAttributeValue * @Description: 獲取xml中指定節點名字指定屬性名字的值 * @param xml字符串* @param nodeName 節點名字* @param attributeName 屬性名字* @return attributeValue 屬性值 */public String getAttributeValue(String xml,String nodeName, String attributeName) {Document document = null;String attributeValue = "";fileNodeName=nodeName;try {document = DocumentHelper.parseText(xml);Element root = document.getRootElement();getNodes(root);attributeValue=attributeMap.get(attributeName);//System.out.println("---->"+value);} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}return attributeValue;}public void getNodes(Element node) {//System.out.println("------------------");//System.out.println("當前節點名稱:"+node.getName());//System.out.println("當前節點的內容:"+node.getTextTrim());nodeMap.put(node.getName(), node.getTextTrim());if (node.getName().equals(fileNodeName)) {// 當前節點所有屬性的listList<Attribute> list = node.attributes();// 遍歷當前節點的所有屬性for (Attribute attribute : list) {//System.out.println("屬性名稱:" + attribute.getName() + "屬性值:" + attribute.getValue());attributeMap.put(attribute.getName(), attribute.getValue());}}// 遞歸遍歷當前節點所有的子節點List<Element> listElement = node.elements();// 所有一級子節點的listfor (Element e : listElement) {// 遍歷所有一級子節點this.getNodes(e);// 遞歸}}public static void main(String[] args) {// TODO Auto-generated method stubString xmlStr = "<?xml version=\"1.0\" encoding=\"gbk\"?>" + "<business id=\"97008\" comment=\"證書操作\">"+ "<body id=\"123456\">" + "<returncode>返回代碼</returncode>" + "<returnmsg>返回信息</returnmsg>" + "</body>"+ "</business>";//AqdlAPI testXML = new AqdlAPI();// testXML.CreatZSCZXML();//testXML.CreateXML_ZXBBXXCXXML("", "");//System.out.println(testXML.readNodeValue(xmlStr, "returnmsg"));System.out.println("returnmsg:"+new XMLUtils().getNodeValue(xmlStr, "returnmsg"));System.out.println("business->id:"+new XMLUtils().getAttributeValue(xmlStr,"business", "id"));System.out.println("body->id:"+new XMLUtils().getAttributeValue(xmlStr,"body", "id"));} }運行結果:
總結
以上是生活随笔為你收集整理的Java利用dom4j解析XML任意节点和属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: D类数字功放输出接到传统模拟功放,结果效
- 下一篇: 穷鬼、酒鬼、色鬼、赌鬼