解析具体xml获取节点的属性和标签的名称和内容
因为业务需求需要判断节点中response和zappsettlement中success的值为true才能进⾏下⼀步操作:主要是针对某个具体的xml进⾏解
析:
<response id="d5bdf222-812a-4686-bb57-05f063dc5e2a" name="chunaServ" Success="true" inTime="2018-04-02 16:18:08 596" analysisTime="2" proce
<zappsettlement chdrcoy="1" chdrnum="11292836" batctrcde="T642" validflag="2" Success="true"><validflag>2</validflag><acctyear>2014</acctyear><acctmon
<response id="d5bdf222-812a-4686-bb57-05f063dc5e2a" name="chunaServ" Success="true" inTime="2018-04-02 16:18:08 596"
analysisTime="2" processTime"18" provider="10.137.103.11"><zappsettlement chdrcoy="1" chdrnum="11292836"
batctrcde="T642" validflag="2" Success="true">
<validflag>2</validflag>
<acctyear>2014</acctyear>
<acctmonth>2</acctmonth>
</zappsettlement></response>
⼯具类为:
package com.taikang.util;
import java.io.File;
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.io.SAXReader;
/**
* 解析Xml
*
* @author itw_zhanghui01
*
*/
public class XmlAnaly {
private static Map<String, String> map;
public static Map<String, String> domjxml(String paramstr) throws Exception {
map = new HashMap<String, String>();
try {
// 将xml格式字符串转化为DOM对象
org.dom4j.Document document = DocumentHelper.parseText(paramstr);
// 获取根结点对象
Element rootElement = RootElement();
Attribute rootattr = rootElement.attribute("Success");
map.put("rootSuccess", Value());
// 循环根节点,获取其⼦节点
for (Iterator iter = rootElement.elementIterator(); iter.hasNext();) {
Element element = (Element) (); // 获取标签对象
// 获取该标签对象的属性
// 获取该标签对象的属性
Attribute attr = element.attribute("Success");
if (null != attr) {
String attrVal = Value();
String attrName = Name();
map.put("zappSuccess", attrVal);
}
// 循环第⼀层节点,获取其⼦节点
for (Iterator iterInner = element.elementIterator(); iterInner
.hasNext();) {
// 获取标签对象
Element elementOption = (Element) ();
// 获取该标签对象的名称
String tagName = Name();
// 获取该标签对象的内容
String tagContent = TextTrim();
/
/ 输出内容
map.put(tagName, tagContent);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
完全的xml解析⼯具:
package com.taikang.util;
import java.io.File;
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.io.SAXReader;
/**
* 解析Xml
*
* @author itw_zhanghui01
*
*/
public class XmlAnaly {
private static Map<String, String> map;
public static Map<String, String> execute(String paramstr) throws Exception { map = new HashMap<String, String>();
// 创建xml的Document对象
Document document = DocumentHelper.parseText(paramstr);
// 遍历节点的所有属性
// 遍历节点的所有属性
Element root = RootElement();
getNodes(root);
return map;
}
/**
* 从指定节点开始,递归遍历所有⼦节点
*
* @param args
* @throws Exception
*/
public static void getNodes(Element node) {
String key = "";
String val = "";
// 获取当前节点的属性
List<Attribute> listAttr = node.attributes();
// 递归遍历当前节点所有⼦节点
List<Element> listElement = node.elements();
for (Element e : listElement) {
String attrValue = "";
if(e.attributes().size() > 0){
使用dom4j解析xml文件Attribute attr = (Attribute) e.attributes().get(0);
attrValue = Value();
}
if (e.getTextTrim() != "" && e.getTextTrim() != null) {
if (attrValue != "" && attrValue != null) {
key = e.getName()+"_"+attrValue;
} else {
key = e.getName();
}
val = e.getTextTrim();
}
map.put(key, val);
getNodes(e);// 递归
}
}
// ⽤于判断是否有⼦节点,若有就将⼦节点也进⾏拼接,若⽆则返回""
public static String checkChildEle(Element element) throws DocumentException {
String json = "";
List<Element> list = new ArrayList<Element>();
list = element.elements();
if (list.size() > 0) {
for (Element ele : list) {
List<Attribute> listAttr = ele.attributes();
String attrvalue = "";
if(listAttr.size()>0){
attrvalue = (0).getValue();
System.out.println("12####"+attrvalue);
}
json += "\""+Name().trim()+"_"+attrvalue + "\""+":" + "\""+Text().trim() + "\""+"," + "\n" + checkChildEle(ele);
}
}
return json;
}
// 这个⽅法是将xml字符串转成Json
public static String xmlChangeJson(String XML) throws DocumentException {
if(XML.indexOf("<!DOCTYPE")>=0){
XML = place(XML.substring(XML.indexOf("<!DOCTYPE"), XML.indexOf("dtd\">")+5), ""); }
Document document = DocumentHelper.parseText(XML);
Element root = RootElement();
Iterator it = root.elementIterator();
String json = "{";
while (it.hasNext()) {
Element element = (Element) it.next();
String j = checkChildEle(element);
if (j == "") {
json += "\""+Name() .trim()+"\""+":"+ "\""+Text().trim() + "\"" + ",";
} else {
json += j;
}
}
json = json.substring(0, json.lastIndexOf(","));
json += "}";
return json;
}
// 这个⽅法是将xml⽂件转成Json
public static String xmlChangeJson(File XML) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = ad(XML);
Element root = RootElement();
Iterator it = root.elementIterator();
String json = "{";
while (it.hasNext()) {
Element element = (Element) it.next();
String j = checkChildEle(element);
if (j == "") {
json += "\""+Name() +"\""+":"+ "\""+Text().trim() + "\""
+ ",";
} else {
json += j;
}
}
json = json.substring(0, json.lastIndexOf(","));
json += "}";
return json;
}
public static Map<String, String> domjxml(String paramstr) throws Exception {
map = new HashMap<String, String>();
try {
// 将xml格式字符串转化为DOM对象
org.dom4j.Document document = DocumentHelper.parseText(paramstr);
// 获取根结点对象
Element rootElement = RootElement();
Attribute rootattr = rootElement.attribute("Success");
map.put("rootSuccess", Value());
// 循环根节点,获取其⼦节点
for (Iterator iter = rootElement.elementIterator(); iter.hasNext();) {
Element element = (Element) (); // 获取标签对象
// 获取该标签对象的属性
Attribute attr = element.attribute("Success");
if (null != attr) {
String attrVal = Value();
String attrName = Name();
map.put("zappSuccess", attrVal);
}
// 循环第⼀层节点,获取其⼦节点
for (Iterator iterInner = element.elementIterator(); iterInner
.hasNext();) {
/
/ 获取标签对象
Element elementOption = (Element) (); // 获取该标签对象的名称
String tagName = Name();
// 获取该标签对象的内容
String tagContent = TextTrim(); // 输出内容
map.put(tagName, tagContent);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论