Skip to content

Commit

Permalink
常用工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyze committed Aug 25, 2019
1 parent 74babbe commit 90eec6d
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 0 deletions.
51 changes: 51 additions & 0 deletions xu-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,56 @@

<artifactId>xu-common</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.3.RELEASE</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>

<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
32 changes: 32 additions & 0 deletions xu-common/src/main/java/com/xuyz/common/utils/json/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (C), 2015-2019, XXX有限公司
* FileName: Test
* Author: xuyz
* Date: 2019/8/25 15:17
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.xuyz.common.utils.json;

import com.alibaba.fastjson.JSON;

/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author xuyz
* @date 2019/8/25
* @since 1.0.0
*/
public class Test {

public static void main(String[] args) {
TestObject testObject = new TestObject();
testObject.setMsg("11");
testObject.setMsg2("333");
testObject.setMsg1("222");
System.out.println(JSON.toJSON(testObject));
}
}
59 changes: 59 additions & 0 deletions xu-common/src/main/java/com/xuyz/common/utils/json/TestObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (C), 2015-2019, XXX有限公司
* FileName: TestObject
* Author: xuyz
* Date: 2019/8/25 15:13
* Description: message对象
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.xuyz.common.utils.json;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
* 〈一句话功能简述〉<br>
* 〈message对象〉
*
* @author xuyz
* @date 2019/8/25
* @since 1.0.0
*/
@Data
@Setter
@Getter
public class TestObject {

private String msg;

private String msg1;

private String msg2;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public String getMsg1() {
return msg1;
}

public void setMsg1(String msg1) {
this.msg1 = msg1;
}

public String getMsg2() {
return msg2;
}

public void setMsg2(String msg2) {
this.msg2 = msg2;
}
}
170 changes: 170 additions & 0 deletions xu-common/src/main/java/com/xuyz/common/utils/xml/xpath/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* Copyright (C), 2015-2019, XXX有限公司
* FileName: Test
* Author: xuyz
* Date: 2019/8/25 15:29
* Description: xpath
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.xuyz.common.utils.xml.xpath;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

/**
* 〈一句话功能简述〉<br>
* 〈xpath〉
*
* @author xuyz
* @date 2019/8/25
* @since 1.0.0
*/
public class Test {
/**
* @param document Document对象(读xml生成的)
* @return String字符串
* @throws Throwable
*/
public String xmlToString(Document document) throws Throwable {
TransformerFactory ft = TransformerFactory.newInstance();
Transformer ff = ft.newTransformer();
ff.setOutputProperty("encoding", "UTF-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ff.transform(new DOMSource(document), new StreamResult(bos));
return bos.toString();
}

/**
* @param
* @return Document 对象
*/
public Document StringTOXml(String str) {

StringBuilder sXML = new StringBuilder();
sXML.append(str);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
InputStream is = new ByteArrayInputStream(sXML.toString().getBytes("utf-8"));
doc = dbf.newDocumentBuilder().parse(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}

/**
* @param document
* @return 某个节点的值 前提是需要知道xml格式,知道需要取的节点相对根节点所在位置
*/
public String getNodeValue(Document document, String nodePath) {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath path = xPathFactory.newXPath();
String servInitrBrch = "";
try {
servInitrBrch = path.evaluate(nodePath, document);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return servInitrBrch;
}

/**
* @param document
* @param nodePath 需要修改的节点相对根节点所在位置
* @param vodeValue 替换的值
*/
public void setNodeValue(Document document, String nodePath, String vodeValue) {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath path = xPathFactory.newXPath();
Node node = null;
;
try {
node = (Node) path.evaluate(nodePath, document, XPathConstants.NODE);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
node.setTextContent(vodeValue);
}

public static void main(String[] args) throws Throwable {
// 读取xml文件,生成document对象
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// 文件的位置在工作空间的根目录(位置随意,只要写对就ok)
//Document document = builder.parse(new File("classpath:a.xml"));

Test t = new Test();
// XML————》String
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<transaction>\n" +
" <body>\n" +
" <request>\n" +
" <tranTyp>批量业务现存</tranTyp>\n" +
" <acctNm>0085213560</acctNm>\n" +
" <acctNo>6225885517843413</acctNo>\n" +
" <avlBal>201958.65</avlBal>\n" +
" <acctTyp>0</acctTyp>\n" +
" <tranTime>20170801101030</tranTime>\n" +
" <currencyTyp>CNY</currencyTyp>\n" +
" <tranDesc></tranDesc>\n" +
" <bal>201958.65</bal>\n" +
" <tranAmt>100000.00</tranAmt>\n" +
" </request>\n" +
" </body>\n" +
" <header>\n" +
" <msg>\n" +
" <sndTm>101019</sndTm>\n" +
" <msgCd>WCS0000200</msgCd>\n" +
" <seqNb>632376531000009</seqNb>\n" +
" <sndMbrCd>5200</sndMbrCd>\n" +
" <rcvMbrCd>0000</rcvMbrCd>\n" +
" <sndDt>20170821</sndDt>\n" +
" <sndAppCd>CBS</sndAppCd>\n" +
" <rcvAppCd>WCS</rcvAppCd>\n" +
" <callTyp>SYN</callTyp>\n" +
" </msg>\n" +
" <ver>1.0</ver>\n" +
" <pnt>\n" +
" <sndTm>101216</sndTm>\n" +
" <sndMbrCd>0000</sndMbrCd>\n" +
" <rcvMbrCd>0000</rcvMbrCd>\n" +
" <sndDt>20170809</sndDt>\n" +
" <sndAppCd>ESB</sndAppCd>\n" +
" <rcvAppCd>WCS</rcvAppCd>\n" +
" </pnt>\n" +
" </header>\n" +
"</transaction>";//t.xmlToString(document);
System.out.println("str:" + str);
long time = System.currentTimeMillis();
// String ————》XML
Document doc = t.StringTOXml(str);
String nodePath = "/transaction/header/msg/sndMbrCd";
// getNodeValue
String nodeValue = t.getNodeValue(doc, nodePath);
System.out.println("修改前nodeValue:" + nodeValue);
// setNodeValue
t.setNodeValue(doc, nodePath, nodeValue + "hello");
System.out.println("修改后nodeValue:" + t.getNodeValue(doc, nodePath));
long time1 = System.currentTimeMillis();
System.out.println("修改后的xml:"+t.xmlToString(doc));
System.out.println("耗时:"+(time1-time));
}
}
39 changes: 39 additions & 0 deletions xu-common/src/main/resources/a.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<transaction>
<body>
<request>
<tranTyp>批量业务现存</tranTyp>
<acctNm>0085213560</acctNm>
<acctNo>6225885517843413</acctNo>
<avlBal>201958.65</avlBal>
<acctTyp>0</acctTyp>
<tranTime>20170801101030</tranTime>
<currencyTyp>CNY</currencyTyp>
<tranDesc></tranDesc>
<bal>201958.65</bal>
<tranAmt>100000.00</tranAmt>
</request>
</body>
<header>
<msg>
<sndTm>101019</sndTm>
<msgCd>WCS0000200</msgCd>
<seqNb>632376531000009</seqNb>
<sndMbrCd>5200</sndMbrCd>
<rcvMbrCd>0000</rcvMbrCd>
<sndDt>20170821</sndDt>
<sndAppCd>CBS</sndAppCd>
<rcvAppCd>WCS</rcvAppCd>
<callTyp>SYN</callTyp>
</msg>
<ver>1.0</ver>
<pnt>
<sndTm>101216</sndTm>
<sndMbrCd>0000</sndMbrCd>
<rcvMbrCd>0000</rcvMbrCd>
<sndDt>20170809</sndDt>
<sndAppCd>ESB</sndAppCd>
<rcvAppCd>WCS</rcvAppCd>
</pnt>
</header>
</transaction>

0 comments on commit 90eec6d

Please sign in to comment.