日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Java利用dom4j解析XML任意节点和属性

發布時間:2023/12/8 asp.net 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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任意节点和属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。