当前位置:网站首页>Tool class xmlutil (parse and return soap and XML messages to obtain the value of the target node)
Tool class xmlutil (parse and return soap and XML messages to obtain the value of the target node)
2022-04-22 21:16:00 【Dubbo Luo】
1. Support jar package ( There may be version conflicts )
<!-- https://mvnrepository.com/artifact/org.jdom/jdom -->
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
2.XMLUtil
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** * Parsing returns soap and xml message , Get target node value * @author luoyan * @date 2019-06-04 */
public class XMLUtil {
/** * The string type of XML Turn it into Docunent Document structure * @param parseStrXml To be converted XML character string * @return */
public static Document strXmlToDocument(String parseStrXml) {
StringReader read = new StringReader(parseStrXml);
// Create a new input source SAX The parser will use InputSource Object to determine how to read XML Input
InputSource source = new InputSource(read);
// Create a new SAXBuilder
// Newly created constructor
SAXBuilder sb = new SAXBuilder();
Document doc = null;
try {
doc = sb.build(source);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
/** * Get the value according to the target node name * @param doc Document structure * @param finalNodeName The node name * @return */
public static String getValueByElementName(Document doc, String finalNodeName) {
Element root = doc.getRootElement();
HashMap<String, Object> map = new HashMap<String, Object>();
Map<String, Object> resultmap = getChildAllText(doc, root, map);
String result = (String) resultmap.get(finalNodeName);
return result;
}
/** * Recursively obtain the value of the child node * @param doc Document structure * @param e Node elements * @param resultmap Recursively store values in map in * @return */
public static Map<String, Object> getChildAllText(Document doc, Element e, HashMap<String, Object> resultmap) {
if (e != null) {
// If there are child nodes
if (e.getChildren() != null) {
List<Element> list = e.getChildren();
// Cyclic output
for (Element el : list) {
// If child nodes still exist , Then recursively get
if (el.getChildren().size() > 0) {
getChildAllText(doc, el, resultmap);
} else {
// Press the leaf node value into map
resultmap.put(el.getName(), el.getTextTrim());
}
}
}
}
return resultmap;
}
public static void main(String[] args) {
String str = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:sendtransactioninformationResponse xmlns:ns2=\"http://webservice.information.xqx.com/\"><response></response></ns2:sendtransactioninformationResponse></soap:Body></soap:Envelope>";
Document document = strXmlToDocument(str);
String response = getValueByElementName(document, "response");
System.out.println("---->"+response);
}
}
版权声明
本文为[Dubbo Luo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221806301232.html
边栏推荐
- Navicat for MySQL 连接 Docker中运行的Mysql出现1251- Client does not support authentication protocol 错误
- 【MySQL从入门到精通】:关于常用like子句中通配符的总结
- After five years of graduation, how to increase the monthly salary from 5K to 50W +, what core skills do you need to master?
- Force buckle - 64 Minimum path sum
- nodejs笔记2
- .103Navigator
- TensorFlow基础知识
- Use of consul
- OpenVX 的 立即模式(immediate mode)和图模式(graph mode)和示例讲解
- [database learning 01]
猜你喜欢

Introduction and hardware implementation of sereds deserialization module

Based on paogd_ HW1's pop-up ball - simple modeling, interpolation animation

Selenium_Webdriver视频自动化脚本分享

Design and implementation of Snake game based on OpenGL

TensorFlow基础知识

News Express I mobtech a réussi l'évaluation spéciale de la sécurité de l'Académie chinoise des communications

MySQL尚硅谷课程查缺补漏适合基础(一)

【MySQL从入门到精通】:关于常用like子句中通配符的总结

Development of infectious disease prediction software based on SEIR model

Basic design of character animation based on paogd
随机推荐
最新版去水印+外卖cps小程序源码
Force buckle - 64 Minimum path sum
R language data reading, cleaning and univariate linear regression
Time report填写规则
【MySQL从入门到精通】:关于常用like子句中通配符的总结
【工具配置篇】Visual Studio 中由 designer 生成的. ui 文件未能及时更新问题的解决方案
JVM topic (XII) - garbage collection (III)
东吴证券X袋鼠云:数据轻松可取、毫秒级反应能力,东吴证券做对了什么?
MySql指定字段排序、自定义排序位置
K8s deploy redis cluster
JMeter data and software
nodejs笔记2
Force buckle -931 Minimum sum of descent path
345-Leetcode 赎金信
Nodejs notes 2
DSPACE simulates simple accident scene
最新版去水印+外賣cps小程序源碼
Autodesk products cannot install the solution
Big talk test data (II): acquisition of concept test data
Selenium_Webdriver视频自动化脚本分享