合并分支 'origin' 到 'master'
Origin 查看合并请求 !1
正在显示
20 个修改的文件
包含
1114 行增加
和
0 行删除
.idea/vcs.xml
0 → 100644
README.md
0 → 100644
config/config.properties
0 → 100644
config/imf_config.properties
0 → 100644
config/log4j.properties
0 → 100644
1 | +########Log4j configuration##################################### | ||
2 | +log4j.rootLogger=INFO, stdout, fileout | ||
3 | + | ||
4 | + | ||
5 | +log4j.logger.com.fiscal = INFO | ||
6 | +#******************************* | ||
7 | +log4j.logger.com.system = INFO | ||
8 | +#******************************* | ||
9 | +log4j.appender.stdout=org.apache.log4j.ConsoleAppender | ||
10 | +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout | ||
11 | +log4j.appender.stdout.layout.ConversionPattern=%d{MM-dd HH:mm:ss,SSSS}[%p] %m%n | ||
12 | + | ||
13 | +#yyyy-MM-dd HH:mm:ss | ||
14 | + | ||
15 | + | ||
16 | +log4j.appender.fileout=org.apache.log4j.RollingFileAppender | ||
17 | +log4j.appender.fileout.File=logs/imf.log | ||
18 | +log4j.appender.fileout.MaxFileSize=50MB | ||
19 | +log4j.appender.fileout.MaxBackupIndex=10 | ||
20 | +log4j.appender.fileout.layout=org.apache.log4j.PatternLayout | ||
21 | +log4j.appender.fileout.layout.ConversionPattern=%d{MM-dd HH:mm:ss,SSSS}[%p] %m%n |
nmsImfReader.iml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
3 | + <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> | ||
4 | + <output url="file://$MODULE_DIR$/target/classes" /> | ||
5 | + <output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
6 | + <content url="file://$MODULE_DIR$"> | ||
7 | + <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
8 | + <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
9 | + <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
10 | + <excludeFolder url="file://$MODULE_DIR$/target" /> | ||
11 | + </content> | ||
12 | + <orderEntry type="inheritedJdk" /> | ||
13 | + <orderEntry type="sourceFolder" forTests="false" /> | ||
14 | + <orderEntry type="library" name="imf" level="project" /> | ||
15 | + <orderEntry type="library" name="Maven: commons-io:commons-io:2.6" level="project" /> | ||
16 | + <orderEntry type="library" name="Maven: org.dom4j:dom4j:2.1.0" level="project" /> | ||
17 | + <orderEntry type="library" name="Maven: jaxen:jaxen:1.1.6" level="project" /> | ||
18 | + <orderEntry type="library" name="Maven: org.jdom:jdom:1.1.3" level="project" /> | ||
19 | + </component> | ||
20 | +</module> |
pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
5 | + <modelVersion>4.0.0</modelVersion> | ||
6 | + | ||
7 | + <groupId>com.tianbo</groupId> | ||
8 | + <artifactId>nmsImfReader</artifactId> | ||
9 | + <version>1.0-SNAPSHOT</version> | ||
10 | + | ||
11 | + | ||
12 | + <dependencies> | ||
13 | + <dependency> | ||
14 | + <groupId>commons-io</groupId> | ||
15 | + <artifactId>commons-io</artifactId> | ||
16 | + <version>2.6</version> | ||
17 | + </dependency> | ||
18 | + <dependency> | ||
19 | + <groupId>org.dom4j</groupId> | ||
20 | + <artifactId>dom4j</artifactId> | ||
21 | + <version>2.1.0</version> | ||
22 | + </dependency> | ||
23 | + <dependency> | ||
24 | + <groupId>org.jdom</groupId> | ||
25 | + <artifactId>jdom</artifactId> | ||
26 | + <version>1.1.3</version> | ||
27 | + </dependency> | ||
28 | + </dependencies> | ||
29 | +</project> |
src/META-INF/MANIFEST.MF
0 → 100644
1 | +Manifest-Version: 1.0 | ||
2 | +Class-Path: commons-io-2.6.jar dom4j-2.1.0.jar jdom-1.1.3.jar jaxen-1. | ||
3 | + 1.6.jar antlr-2.7.7.jar antlr-3.2.jar antlr-runtime-3.2.jar aopallian | ||
4 | + ce-1.0.jar apiConfig.jar asm-3.3.jar bcprov-jdk15-1.45.jar CL3Export. | ||
5 | + jar CL3Nonexport.jar com.ibm.mq.axis2.jar com.ibm.mq.commonservices.j | ||
6 | + ar com.ibm.mq.defaultconfig.jar com.ibm.mq.fta.jar com.ibm.mq.headers | ||
7 | + .jar com.ibm.mq.jar com.ibm.mq.jmqi.jar com.ibm.mq.jms.Nojndi.jar com | ||
8 | + .ibm.mq.pcf.jar com.ibm.mq.postcard.jar com.ibm.mq.soap.jar com.ibm.m | ||
9 | + q.tools.ras.jar com.ibm.mqetclient.jar com.ibm.mqjms.jar commons-coll | ||
10 | + ections-3.2.1.jar commons-lang-2.5.jar commons-logging-1.1.1.jar comm | ||
11 | + ons-pool-1.5.5.jar connector.jar cxf-2.3.2.jar cxf-xjc-boolean-2.3.2. | ||
12 | + jar cxf-xjc-bug671-2.3.2.jar cxf-xjc-dv-2.3.2.jar cxf-xjc-ts-2.3.2.ja | ||
13 | + r dhbcore.jar FastInfoset-1.2.8.jar fscontext.jar geronimo-activation | ||
14 | + _1.1_spec-1.1.jar geronimo-annotation_1.0_spec-1.1.1.jar geronimo-jav | ||
15 | + amail_1.4_spec-1.7.1.jar geronimo-jaxws_2.2_spec-1.0.jar geronimo-jms | ||
16 | + _1.1_spec-1.1.1.jar geronimo-servlet_3.0_spec-1.0.jar geronimo-stax-a | ||
17 | + pi_1.0_spec-1.0.1.jar geronimo-ws-metadata_2.0_spec-1.1.3.jar Heartbe | ||
18 | + atService.jar IMFClient.1.0.0.jar imfmsg.jar jaxb-api-2.2.1.jar jaxb- | ||
19 | + impl-2.2.1.1.jar jettison-1.2.jar jetty-continuation-7.2.2.v20101205. | ||
20 | + jar jetty-http-7.2.2.v20101205.jar jetty-io-7.2.2.v20101205.jar jetty | ||
21 | + -server-7.2.2.v20101205.jar jetty-util-7.2.2.v20101205.jar jms.jar jn | ||
22 | + di.jar jra-1.0-alpha-4.jar js-1.7R2.jar jsr173_1.0_api.jar jsr311-api | ||
23 | + -1.1.1.jar jta.jar ldap.jar log4j-1.2.16.jar neethi-2.0.4.jar oro-2.0 | ||
24 | + .8.jar ProcessService.jar providerutil.jar resolver.jar returnXml.jar | ||
25 | + rmm.jar saaj-api-1.3.jar saaj-impl-1.3.2.jar serializer-2.7.1.jar si | ||
26 | + gar.jar slf4j-api-1.6.1.jar slf4j-jdk14-1.6.1.jar spring-aop-3.0.5.RE | ||
27 | + LEASE.jar spring-asm-3.0.5.RELEASE.jar spring-beans-3.0.5.RELEASE.jar | ||
28 | + spring-context-3.0.5.RELEASE.jar spring-core-3.0.5.RELEASE.jar sprin | ||
29 | + g-expression-3.0.5.RELEASE.jar spring-jms-3.0.5.RELEASE.jar spring-tx | ||
30 | + -3.0.5.RELEASE.jar spring-web-3.0.5.RELEASE.jar stax2-api-3.0.2.jar s | ||
31 | + tringtemplate-3.2.jar UploadService.jar velocity-1.6.4.jar woodstox-c | ||
32 | + ore-asl-4.0.8.jar wsdl4j-1.6.2.jar wss4j-1.5.11.jar xalan-2.7.1.jar x | ||
33 | + bean_xpath.jar xbean.jar xml-resolver-1.2.jar xmlbeans-2.4.0.jar xmlb | ||
34 | + eans-qname.jar xmlpublic.jar XmlSchema-1.4.7.jar xmlsec-1.4.4.jar jax | ||
35 | + b-xjc-2.2.1.1.jar | ||
36 | +Main-Class: com.tianbo.Main | ||
37 | + |
src/main/java/com/tianbo/Main.java
0 → 100644
1 | +package com.tianbo; | ||
2 | + | ||
3 | +import com.caac.imf.api.IMFClient; | ||
4 | +import com.caac.imf.api.IMFClientFactory; | ||
5 | +import com.tianbo.imf.IMF_Reader; | ||
6 | +import com.tianbo.imf.IMF_Sender; | ||
7 | +import com.tianbo.util.IO.FileTool; | ||
8 | +import org.apache.log4j.Logger; | ||
9 | +import org.apache.log4j.PropertyConfigurator; | ||
10 | + | ||
11 | +import java.io.File; | ||
12 | +import java.util.List; | ||
13 | + | ||
14 | +public class Main { | ||
15 | + private static final Logger logger = Logger.getLogger(Main.class); | ||
16 | + public static boolean LOGIN_OK = false; | ||
17 | + public static boolean isSuc = true; | ||
18 | + public static IMFClient client = null; | ||
19 | + | ||
20 | + public static void sendMsg(String msg){ | ||
21 | + if (!msg.equals(null) && !msg.isEmpty()){ | ||
22 | + | ||
23 | + } | ||
24 | + | ||
25 | + } | ||
26 | + | ||
27 | + | ||
28 | + private static void start() throws Exception { | ||
29 | + PropertyConfigurator.configure("config/log4j.properties"); | ||
30 | + client = IMFClientFactory.createInstance(); | ||
31 | + String loginname = FileTool.readProperties("loginname"); | ||
32 | + String loginpass = FileTool.readProperties("loginpass"); | ||
33 | + | ||
34 | + while (true) { | ||
35 | + if (client != null) { | ||
36 | + IMF_Reader reader = new IMF_Reader(client); | ||
37 | + if (!reader.isrunning) { | ||
38 | + reader.start(); | ||
39 | + logger.info("*********KAKO读取线程已开启***********"); | ||
40 | + } else { | ||
41 | +// logger.info("*********KAKO读取线程已开启-不再启动线程*********"); | ||
42 | + } | ||
43 | + | ||
44 | + IMF_Sender kako_sender = new IMF_Sender(client); | ||
45 | + if(!kako_sender.isrunning) { | ||
46 | + kako_sender.start(); | ||
47 | + } | ||
48 | + } | ||
49 | + | ||
50 | + if (!LOGIN_OK) { | ||
51 | + loginIMF(client, loginname, loginpass, "config/imf_config.properties"); | ||
52 | + } | ||
53 | + try { | ||
54 | + Thread.sleep(30000L); | ||
55 | + } catch (InterruptedException var7) { | ||
56 | + var7.printStackTrace(); | ||
57 | + } | ||
58 | + } | ||
59 | + | ||
60 | + } | ||
61 | + | ||
62 | + | ||
63 | + | ||
64 | + private static void loginIMF(IMFClient client, String userName, String password, String confFileName) { | ||
65 | + if (client.initial(confFileName)) { | ||
66 | + String message = client.login(userName, password); | ||
67 | + logger.info("message=" + message); | ||
68 | + if (message.indexOf("<CODE>1</CODE>") > 0) { | ||
69 | + logger.info("登陆成功"); | ||
70 | + LOGIN_OK = true; | ||
71 | + } else { | ||
72 | + int times = 0; | ||
73 | + | ||
74 | + while(times <= 3) { | ||
75 | + logger.info("try connection..."); | ||
76 | + ++times; | ||
77 | + logger.info("message.=" + message); | ||
78 | + if (message.indexOf("<CODE>1</CODE>") > 0) { | ||
79 | + logger.info("登陆成功"); | ||
80 | + LOGIN_OK = true; | ||
81 | + break; | ||
82 | + } | ||
83 | + | ||
84 | + logger.info("登录失败~~~~"); | ||
85 | + message = client.login(userName, password); | ||
86 | + | ||
87 | + try { | ||
88 | + Thread.sleep(4000L); | ||
89 | + } catch (InterruptedException var7) { | ||
90 | + var7.printStackTrace(); | ||
91 | + } | ||
92 | + } | ||
93 | + | ||
94 | + if (!LOGIN_OK) { | ||
95 | + logger.info("多次尝试登录失败,退出登陆"); | ||
96 | + client.disconnect(); | ||
97 | + isSuc =false; | ||
98 | + System.exit(-1); | ||
99 | + } | ||
100 | + } | ||
101 | + } | ||
102 | + | ||
103 | + } | ||
104 | + | ||
105 | + public static void main(String[] args) throws Exception{ | ||
106 | + start(); | ||
107 | + } | ||
108 | + | ||
109 | +} |
src/main/java/com/tianbo/imf/IMF_Reader.java
0 → 100644
1 | +package com.tianbo.imf; | ||
2 | + | ||
3 | +import com.caac.imf.api.IMFClient; | ||
4 | + | ||
5 | +import com.tianbo.util.IO.FileTool; | ||
6 | +import com.tianbo.Main; | ||
7 | +import org.apache.log4j.Logger; | ||
8 | + | ||
9 | +public class IMF_Reader extends Thread{ | ||
10 | + protected static final Logger logger = Logger.getLogger(IMF_Reader.class); | ||
11 | + private IMFClient client; | ||
12 | + public static boolean isrunning; | ||
13 | + | ||
14 | + public IMF_Reader(IMFClient client) { | ||
15 | + this.client = client; | ||
16 | + } | ||
17 | + | ||
18 | + public void run() { | ||
19 | + try{ | ||
20 | + isrunning =true; | ||
21 | + while(true) { | ||
22 | + if (Main.LOGIN_OK) { | ||
23 | + String message = this.client.getMSG(); | ||
24 | + if (message != null) { | ||
25 | + FileTool.writeFileToBak(message); | ||
26 | +// IMF_Message_MainHandle mainHandle = new IMF_Message_MainHandle(); | ||
27 | +// IMF_META meta = mainHandle.messageHandle(message); | ||
28 | + } | ||
29 | + //logger.info("当前线程:"+Thread.currentThread().getName()); | ||
30 | + } else { | ||
31 | + //logger.info("***"); | ||
32 | + } | ||
33 | + | ||
34 | + try { | ||
35 | + Thread.sleep(500L); | ||
36 | + } catch (InterruptedException var3) { | ||
37 | + var3.printStackTrace(); | ||
38 | + | ||
39 | + } | ||
40 | + } | ||
41 | + | ||
42 | + }catch (Exception e){ | ||
43 | + e.printStackTrace(); | ||
44 | + } | ||
45 | + isrunning=false; | ||
46 | + logger.info("****************读取线程不在了****************"); | ||
47 | + } | ||
48 | +} |
src/main/java/com/tianbo/imf/IMF_Sender.java
0 → 100644
1 | +package com.tianbo.imf; | ||
2 | + | ||
3 | +import com.caac.imf.api.IMFClient; | ||
4 | +import com.tianbo.Main; | ||
5 | +import com.tianbo.util.Date.DateUtil; | ||
6 | +import com.tianbo.util.IO.FileTool; | ||
7 | +import com.tianbo.util.XML.MakeImfMeta; | ||
8 | +import org.apache.commons.io.FileUtils; | ||
9 | +import org.apache.log4j.Logger; | ||
10 | + | ||
11 | +import java.io.File; | ||
12 | +import java.util.List; | ||
13 | + | ||
14 | +public class IMF_Sender extends Thread{ | ||
15 | + | ||
16 | + protected static final Logger logger = Logger.getLogger(IMF_Sender.class); | ||
17 | + public static boolean isrunning; | ||
18 | + private IMFClient client; | ||
19 | + private String content; | ||
20 | + public IMF_Sender(IMFClient client) { | ||
21 | + this.client = client; | ||
22 | + } | ||
23 | + public IMF_Sender(IMFClient client, String content) { | ||
24 | + this.client = client; | ||
25 | + this.content = content; | ||
26 | + } | ||
27 | + | ||
28 | + public void run(){ | ||
29 | + try { | ||
30 | + String sendDir = FileTool.readProperties("readDirectory"); | ||
31 | + String SNDR = FileTool.readProperties("loginname"); | ||
32 | + String TYPE = FileTool.readProperties("TYPE"); | ||
33 | + String STYP = FileTool.readProperties("STYP"); | ||
34 | + String RCVR = FileTool.readProperties("RCVR"); | ||
35 | + String DDTM = DateUtil.getDDTM(); | ||
36 | + String SEQN = DDTM; | ||
37 | + | ||
38 | + if (this.client == null) { | ||
39 | + logger.info("IMFClient has been closed"); | ||
40 | + return; | ||
41 | + } | ||
42 | + isrunning=true; | ||
43 | + if(Main.isSuc){ | ||
44 | +// logger.info("<<<<<<<<<<<主线程初始化成功>>>>>>>>>>>>"); | ||
45 | + if(Main.LOGIN_OK){ | ||
46 | + while (true){ | ||
47 | + //发送报文 | ||
48 | + List<File> files = FileTool.readDirectoryFiles(new File(sendDir)); | ||
49 | + for (File file:files) { | ||
50 | + String fileContent = FileTool.readfile(file); | ||
51 | + String sendMsg = MakeImfMeta.makeImfDocument(SNDR,RCVR,TYPE,STYP,DDTM,SEQN,fileContent); | ||
52 | + String returnMsg = this.client.sendMSG(sendMsg); | ||
53 | + | ||
54 | + //发送成功处理,删除已发送成功的目录下的文件 | ||
55 | + if(returnMsg.indexOf("<CODE>9</CODE>")>=0){ | ||
56 | + FileUtils.forceDelete(file); | ||
57 | + } | ||
58 | + } | ||
59 | + try { | ||
60 | + Thread.sleep(500L); | ||
61 | + } catch (InterruptedException var3) { | ||
62 | + var3.printStackTrace(); | ||
63 | + | ||
64 | + } | ||
65 | + } | ||
66 | + | ||
67 | + | ||
68 | + | ||
69 | + }else{ | ||
70 | + logger.info("<<<<<<<<<<<登陆中>>>>>>>>>>>>"); | ||
71 | + } | ||
72 | + } | ||
73 | + | ||
74 | + }catch (Exception var14) { | ||
75 | + var14.printStackTrace(); | ||
76 | + logger.info(var14.getMessage()); | ||
77 | + } | ||
78 | + | ||
79 | + | ||
80 | + | ||
81 | + | ||
82 | + isrunning=false; | ||
83 | + logger.info("<<<<<<<<<<<发送线程结束>>>>>>>>>>>>"); | ||
84 | + } | ||
85 | +} |
1 | +package com.tianbo.util.Date; | ||
2 | + | ||
3 | +import java.text.SimpleDateFormat; | ||
4 | +import java.util.Date; | ||
5 | + | ||
6 | +public final class DateUtil { | ||
7 | + private static Date currentDate = new Date(); | ||
8 | + private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
9 | + private static SimpleDateFormat timesdf = new SimpleDateFormat("yyyyMMddHHmmss"); | ||
10 | + | ||
11 | + public static String getToday(){ | ||
12 | + return sdf.format(currentDate); | ||
13 | + } | ||
14 | + public static String getDDTM(){ | ||
15 | + return timesdf.format(currentDate); | ||
16 | + } | ||
17 | +} |
src/main/java/com/tianbo/util/Helper.java
0 → 100644
1 | +package com.tianbo.util; | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | +import java.math.BigDecimal; | ||
6 | +import java.math.BigInteger; | ||
7 | +import java.util.Map; | ||
8 | +import java.util.UUID; | ||
9 | + | ||
10 | +public class Helper { | ||
11 | + | ||
12 | + /** | ||
13 | + * Object转BigDecimal类型-MRZ-2018年5月14日09:56:26 | ||
14 | + * | ||
15 | + * @param value 要转的object类型 | ||
16 | + * @return 转成的BigDecimal类型数据 | ||
17 | + */ | ||
18 | + static public BigDecimal getBigDecimal(Object value) { | ||
19 | + BigDecimal ret = null; | ||
20 | + if (value != null) { | ||
21 | + if (value instanceof BigDecimal) { | ||
22 | + ret = (BigDecimal) value; | ||
23 | + } else if (value instanceof String) { | ||
24 | + ret = new BigDecimal((String) value); | ||
25 | + } else if (value instanceof BigInteger) { | ||
26 | + ret = new BigDecimal((BigInteger) value); | ||
27 | + } else if (value instanceof Number) { | ||
28 | + ret = new BigDecimal(((Number) value).doubleValue()); | ||
29 | + } else { | ||
30 | + throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal."); | ||
31 | + } | ||
32 | + } | ||
33 | + return ret; | ||
34 | + } | ||
35 | + | ||
36 | + | ||
37 | + | ||
38 | + public static String getUUID(){ | ||
39 | + return UUID.randomUUID().toString().replace("-", ""); | ||
40 | + } | ||
41 | +} |
1 | +package com.tianbo.util.IO; | ||
2 | + | ||
3 | +import com.tianbo.util.Date.DateUtil; | ||
4 | +import com.tianbo.util.Helper; | ||
5 | +import org.apache.commons.io.FileUtils; | ||
6 | + | ||
7 | +import java.io.*; | ||
8 | +import java.util.List; | ||
9 | +import java.util.Properties; | ||
10 | + | ||
11 | + | ||
12 | +public final class FileTool { | ||
13 | + private final static String errorRootDirectory = "errorLogs";//错误的根目录名 | ||
14 | + private final static String xmlRootDirectory = "xmlLog"; //记录已收到的报文目录 | ||
15 | + private final static String Cherector = "UTF-8"; | ||
16 | + | ||
17 | + /** | ||
18 | + * 写入文件 | ||
19 | + * @param path 二级目录 | ||
20 | + * @param content 写入内容 | ||
21 | + * @param rightOrwrong 是写入错误记录目录还是记录目录 | ||
22 | + */ | ||
23 | + public static void writeFile(String path,String content,boolean rightOrwrong){ | ||
24 | + StringBuffer stringBuffer = new StringBuffer(); | ||
25 | + | ||
26 | + if (rightOrwrong){ | ||
27 | + stringBuffer.append(xmlRootDirectory).append("/").append(path).append("/").append(DateUtil.getToday()).append("/").append(Helper.getUUID()).append(".log"); | ||
28 | + }else { | ||
29 | + stringBuffer.append(errorRootDirectory).append("/").append(path).append("/").append(DateUtil.getToday()).append("/").append(Helper.getUUID()).append(".log"); | ||
30 | + } | ||
31 | + | ||
32 | + File file = new File(stringBuffer.toString()); | ||
33 | + | ||
34 | + try{ | ||
35 | + FileUtils.writeStringToFile(file,content,Cherector); | ||
36 | + }catch (IOException e){ | ||
37 | + e.printStackTrace(); | ||
38 | + } | ||
39 | + | ||
40 | + } | ||
41 | + public static String readfile(File file) throws IOException{ | ||
42 | + String fileToString = FileUtils.readFileToString(file, "UTF-8"); | ||
43 | + return fileToString; | ||
44 | + } | ||
45 | + | ||
46 | + public static List<File> readDirectoryFiles(File dir){ | ||
47 | + List<File> files = (List<File>)FileUtils.listFiles(dir,new String[]{"xml"},false); | ||
48 | + return files; | ||
49 | + } | ||
50 | + public static void writeWaybill(String path,String content,String waybillNo){ | ||
51 | + StringBuffer stringBuffer = new StringBuffer(); | ||
52 | + stringBuffer.append(xmlRootDirectory).append("/").append(path).append("/").append(DateUtil.getToday()).append("/").append(waybillNo).append(".log"); | ||
53 | + File file = new File(stringBuffer.toString()); | ||
54 | + try{ | ||
55 | + FileUtils.writeStringToFile(file,content,Cherector); | ||
56 | + }catch (IOException e){ | ||
57 | + e.printStackTrace(); | ||
58 | + } | ||
59 | + | ||
60 | + } | ||
61 | + | ||
62 | + public static void writeFileToBak(String content){ | ||
63 | + String bakDir = readProperties("bakDirectory"); | ||
64 | + StringBuffer stringBuffer = new StringBuffer(); | ||
65 | + stringBuffer.append(bakDir).append("/").append(Helper.getUUID()).append(".txt"); | ||
66 | + | ||
67 | + File file = new File(stringBuffer.toString()); | ||
68 | + | ||
69 | + try{ | ||
70 | + FileUtils.writeStringToFile(file,content,Cherector); | ||
71 | + }catch (IOException e){ | ||
72 | + e.printStackTrace(); | ||
73 | + } | ||
74 | + | ||
75 | + } | ||
76 | + | ||
77 | + public static String readProperties(String key){ | ||
78 | + Properties properties = new Properties(); | ||
79 | + String value = ""; | ||
80 | + try{ | ||
81 | + BufferedReader bufferedReader = new BufferedReader(new FileReader("config/config.properties")); | ||
82 | + properties.load(bufferedReader); | ||
83 | + //获取key对应的value值 | ||
84 | + value= properties.getProperty(key); | ||
85 | + }catch (Exception e){ | ||
86 | + e.printStackTrace(); | ||
87 | + } | ||
88 | + return value; | ||
89 | + | ||
90 | + } | ||
91 | + | ||
92 | + | ||
93 | +} |
1 | +package com.tianbo.util.XML; | ||
2 | + | ||
3 | +import org.dom4j.Document; | ||
4 | +import org.dom4j.DocumentException; | ||
5 | +import org.dom4j.DocumentHelper; | ||
6 | +import org.dom4j.Element; | ||
7 | +import org.dom4j.io.SAXReader; | ||
8 | + | ||
9 | +import java.io.ByteArrayInputStream; | ||
10 | +import java.io.UnsupportedEncodingException; | ||
11 | + | ||
12 | +public class MakeImfMeta { | ||
13 | + | ||
14 | + public static String makeImfDocument(String SNDR,String RCVR,String TYPE,String STYP,String DDTM,String SEQN,String Content) throws DocumentException,UnsupportedEncodingException{ | ||
15 | + Document document = DocumentHelper.createDocument(); | ||
16 | + Element root = document.addElement( "MSG" ); | ||
17 | + Element meta = root.addElement("META"); | ||
18 | + meta.addElement("SNDR").addText(SNDR); | ||
19 | + meta.addElement("RCVR").addText(RCVR); | ||
20 | + meta.addElement("SEQN").addText(SEQN); | ||
21 | + meta.addElement("DDTM").addText(DDTM); | ||
22 | + meta.addElement("TYPE").addText(TYPE); | ||
23 | + meta.addElement("STYP").addText(STYP); | ||
24 | + | ||
25 | + SAXReader saxReader = new SAXReader(); | ||
26 | + Document doc = saxReader.read(new ByteArrayInputStream(Content.getBytes("UTF-8"))); | ||
27 | + Element contentRoot = doc.getRootElement(); | ||
28 | + root.add(contentRoot); | ||
29 | + | ||
30 | + | ||
31 | + return document.asXML(); | ||
32 | + } | ||
33 | +} |
1 | +package com.tianbo.util.XML; | ||
2 | + | ||
3 | +import org.jdom.Element; | ||
4 | + | ||
5 | +public class SubNode { | ||
6 | + private Element element; | ||
7 | + | ||
8 | + public SubNode(Element element) { | ||
9 | + this.element = element; | ||
10 | + } | ||
11 | + | ||
12 | + public Element getElement() { | ||
13 | + return this.element; | ||
14 | + } | ||
15 | + | ||
16 | + public void setElement(Element element) { | ||
17 | + this.element = element; | ||
18 | + } | ||
19 | +} |
1 | +package com.tianbo.util.XML; | ||
2 | + | ||
3 | +import java.util.ArrayList; | ||
4 | +import java.util.HashMap; | ||
5 | +import java.util.Iterator; | ||
6 | +import java.util.List; | ||
7 | +import java.util.Map; | ||
8 | + | ||
9 | +import org.dom4j.Attribute; | ||
10 | +import org.dom4j.Document; | ||
11 | +import org.dom4j.Element; | ||
12 | + | ||
13 | +public class XML2ENTITY { | ||
14 | + @SuppressWarnings("unchecked") | ||
15 | + /**取KEY的VALUE | ||
16 | + * | ||
17 | + */ | ||
18 | + public Map<String, Object> Dom2Map(Document doc){ | ||
19 | + Map<String, Object> map = new HashMap<String, Object>(); | ||
20 | + if(doc == null) | ||
21 | + return map; | ||
22 | + Element root = doc.getRootElement(); | ||
23 | + for (Iterator iterator = root.elementIterator(); iterator.hasNext();) { | ||
24 | + Element e = (Element) iterator.next(); | ||
25 | + List list = e.elements(); | ||
26 | + if(list.size() > 0){ | ||
27 | + map.put(e.getName(), Dom2Map(e)); | ||
28 | + }else | ||
29 | + map.put(e.getName(), e.getText()); | ||
30 | + } | ||
31 | + return map; | ||
32 | + } | ||
33 | + @SuppressWarnings("unchecked") | ||
34 | + public Map Dom2Map(Element e){ | ||
35 | + Map map = new HashMap(); | ||
36 | + List list = e.elements(); | ||
37 | + if(list.size() > 0){ | ||
38 | + for (int i = 0;i < list.size(); i++) { | ||
39 | + Element iter = (Element) list.get(i); | ||
40 | + List mapList = new ArrayList(); | ||
41 | + | ||
42 | + if(iter.elements().size() > 0){ | ||
43 | + Map m = Dom2Map(iter); | ||
44 | + if(map.get(iter.getName()) != null){ | ||
45 | + Object obj = map.get(iter.getName()); | ||
46 | + if(!obj.getClass().getName().equals("java.util.ArrayList")){ | ||
47 | + mapList = new ArrayList(); | ||
48 | + mapList.add(obj); | ||
49 | + mapList.add(m); | ||
50 | + } | ||
51 | + if(obj.getClass().getName().equals("java.util.ArrayList")){ | ||
52 | + mapList = (List) obj; | ||
53 | + mapList.add(m); | ||
54 | + } | ||
55 | + map.put(iter.getName(), mapList); | ||
56 | + }else | ||
57 | + map.put(iter.getName(), m); | ||
58 | + } | ||
59 | + else{ | ||
60 | + if(map.get(iter.getName()) != null){ | ||
61 | + Object obj = map.get(iter.getName()); | ||
62 | + if(!obj.getClass().getName().equals("java.util.ArrayList")){ | ||
63 | + mapList = new ArrayList(); | ||
64 | + mapList.add(obj); | ||
65 | + mapList.add(iter.getText()); | ||
66 | + } | ||
67 | + if(obj.getClass().getName().equals("java.util.ArrayList")){ | ||
68 | + mapList = (List) obj; | ||
69 | + mapList.add(iter.getText()); | ||
70 | + } | ||
71 | + map.put(iter.getName(), mapList); | ||
72 | + }else | ||
73 | + map.put(iter.getName(), iter.getText()); | ||
74 | + } | ||
75 | + } | ||
76 | + }else | ||
77 | + map.put(e.getName(), e.getText()); | ||
78 | + return map; | ||
79 | + } | ||
80 | + | ||
81 | + public List<Map> attrOfElement(Element e){ | ||
82 | + List attList = new ArrayList(); | ||
83 | + List<Attribute> listAttr = e.attributes(); | ||
84 | + for(Attribute attr:listAttr){//遍历当前节点的所有属性 | ||
85 | + String name=attr.getName();//属性名称 | ||
86 | + String value=attr.getValue();//属性的值 | ||
87 | + Map<String, Object> attMap = new HashMap<String, Object>(); | ||
88 | + attMap.put(name,value); | ||
89 | + attList.add(attMap); | ||
90 | + } | ||
91 | + return attList; | ||
92 | + } | ||
93 | + @SuppressWarnings("unchecked") | ||
94 | + /**遍历所有节点的属性值 | ||
95 | + * | ||
96 | + */ | ||
97 | + public Map<String, Object> Dom2Map_propety(Document doc){ | ||
98 | + Map<String, Object> map = new HashMap<String, Object>(); | ||
99 | + if(doc == null) | ||
100 | + return map; | ||
101 | + Element root = doc.getRootElement(); | ||
102 | + for (Iterator iterator = root.elementIterator(); iterator.hasNext();) { | ||
103 | + Element e = (Element) iterator.next(); | ||
104 | + List list = e.elements(); | ||
105 | + if(list.size() > 0){ | ||
106 | + map.put(e.getName(), Dom2Map_propety(e)); | ||
107 | + }else { | ||
108 | + map.put(e.getName(), attrOfElement(e)); | ||
109 | + } | ||
110 | + } | ||
111 | + return map; | ||
112 | + } | ||
113 | + @SuppressWarnings("unchecked") | ||
114 | + public Map Dom2Map_propety(Element e){ | ||
115 | + Map map = new HashMap(); | ||
116 | + List list = e.elements(); | ||
117 | + if(list.size() > 0){ | ||
118 | + for (int i = 0;i < list.size(); i++) { | ||
119 | + Element iter = (Element) list.get(i); | ||
120 | + List mapList = new ArrayList(); | ||
121 | + | ||
122 | + if(iter.elements().size() > 0){ | ||
123 | + Map m = Dom2Map_propety(iter); | ||
124 | + if(map.get(iter.getName()) != null){ | ||
125 | + Object obj = map.get(iter.getName()); | ||
126 | + if(!obj.getClass().getName().equals("java.util.ArrayList")){ | ||
127 | + mapList = new ArrayList(); | ||
128 | + mapList.add(obj); | ||
129 | + mapList.add(m); | ||
130 | + } | ||
131 | + if(obj.getClass().getName().equals("java.util.ArrayList")){ | ||
132 | + mapList = (List) obj; | ||
133 | + mapList.add(m); | ||
134 | + } | ||
135 | + map.put(iter.getName(), mapList); | ||
136 | + }else | ||
137 | + map.put(iter.getName(), m); | ||
138 | + } | ||
139 | + else{ | ||
140 | + if(map.get(iter.getName()) != null){ | ||
141 | + Object obj = map.get(iter.getName()); | ||
142 | + if(!obj.getClass().getName().equals("java.util.ArrayList")){ | ||
143 | + mapList = new ArrayList(); | ||
144 | + mapList.add(obj); | ||
145 | + mapList.add(iter.getText()); | ||
146 | + } | ||
147 | + if(obj.getClass().getName().equals("java.util.ArrayList")){ | ||
148 | + mapList = (List) obj; | ||
149 | + mapList.add(iter.getText()); | ||
150 | + } | ||
151 | + map.put(iter.getName(), mapList); | ||
152 | + }else{ | ||
153 | + map.put(iter.getName(), attrOfElement(iter)); | ||
154 | + } | ||
155 | + | ||
156 | + | ||
157 | + } | ||
158 | + } | ||
159 | + }else { | ||
160 | + map.put(e.getName(), attrOfElement(e)); | ||
161 | + } | ||
162 | + return map; | ||
163 | + } | ||
164 | + | ||
165 | + /** | ||
166 | + * 判断map是否包含key,包含返回KEY值,不包含返回NULL | ||
167 | + * @param map | ||
168 | + * @param key | ||
169 | + * @return | ||
170 | + */ | ||
171 | + | ||
172 | + public static Object getMap(Map map, String key){ | ||
173 | + if(map!=null && map.containsKey(key)){ | ||
174 | + return map.get(key); | ||
175 | + } else { | ||
176 | + return null; | ||
177 | + } | ||
178 | + } | ||
179 | + | ||
180 | + | ||
181 | +} | ||
182 | + | ||
183 | + |
1 | +package com.tianbo.util.XML; | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | +import org.jdom.Document; | ||
6 | +import org.jdom.Element; | ||
7 | +import org.jdom.JDOMException; | ||
8 | +import org.jdom.input.SAXBuilder; | ||
9 | +import org.slf4j.Logger; | ||
10 | +import org.slf4j.LoggerFactory; | ||
11 | +import org.xml.sax.InputSource; | ||
12 | + | ||
13 | +import java.io.IOException; | ||
14 | +import java.io.StringReader; | ||
15 | +import java.util.*; | ||
16 | + | ||
17 | +public class XMLParse { | ||
18 | + private static final Logger logger = LoggerFactory.getLogger(XMLParse.class); | ||
19 | + private static String xmlString; | ||
20 | + private static Document document; | ||
21 | + private static SAXBuilder builder; | ||
22 | + private static Element root; | ||
23 | + private static LinkedList<SubNode> listNode; | ||
24 | + private static StringReader xmlReader; | ||
25 | + private static InputSource xmlSource; | ||
26 | + private static List<Map> maps = new ArrayList<Map>(); | ||
27 | + private static Map<String,Map> allElementValue = new HashMap<String, Map>(); | ||
28 | + private static int domCount=0; | ||
29 | + private String value; | ||
30 | + | ||
31 | + public XMLParse() { | ||
32 | + } | ||
33 | + public XMLParse(String XmlString) { | ||
34 | + builder = new SAXBuilder(); | ||
35 | + root = null; | ||
36 | + listNode = new LinkedList(); | ||
37 | + value = ""; | ||
38 | + document =new Document(); | ||
39 | + xmlString = XmlString; | ||
40 | + } | ||
41 | + | ||
42 | + public Map getAllValuesFromXmlString() throws Exception{ | ||
43 | + try { | ||
44 | + xmlReader = new StringReader(xmlString); | ||
45 | + xmlSource = new InputSource(xmlReader); | ||
46 | + document = builder.build(xmlSource); | ||
47 | + root = document.getRootElement(); | ||
48 | + getElements(root); | ||
49 | + | ||
50 | + }catch (Exception var17) { | ||
51 | + var17.printStackTrace(); | ||
52 | + throw var17; | ||
53 | + } finally { | ||
54 | + listNode.clear(); | ||
55 | + document = null; | ||
56 | + root = null; | ||
57 | +// builder = null; | ||
58 | + | ||
59 | + } | ||
60 | + return allElementValue; | ||
61 | + } | ||
62 | + | ||
63 | + public void getElements (Element element) throws Exception{ | ||
64 | + try { | ||
65 | + domCount++; | ||
66 | + List<Element> Children = element.getChildren(); | ||
67 | + if (Children.size()>0){ | ||
68 | + for (Element elements : Children){ | ||
69 | + if(elements.getChildren().size()>0){ | ||
70 | + getElements(elements); | ||
71 | + }else {//确定是最末节点了 开始取末节点的数据 | ||
72 | + String childName = elements.getName(); | ||
73 | + String childValue = elements.getText(); | ||
74 | + Map<String,String> childMap = new HashMap<String, String>(); //用map存储每个最终节点的值 | ||
75 | + childMap.put(childName,childValue); | ||
76 | + allElementValue.put(domCount+elements.getParentElement().getName()+"-"+childName,childMap); | ||
77 | + } | ||
78 | + } | ||
79 | + } | ||
80 | + | ||
81 | + | ||
82 | + }catch (Exception e){ | ||
83 | + e.printStackTrace(); | ||
84 | + throw e; | ||
85 | + } | ||
86 | + | ||
87 | + } | ||
88 | + | ||
89 | + public List<Map> getNodeValuesFromXmlStringForPffm(String xmlNodePath) throws Exception{ | ||
90 | + String[] nodes = xmlNodePath.split("/"); | ||
91 | + try { | ||
92 | + xmlReader = new StringReader(xmlString); | ||
93 | + xmlSource = new InputSource(xmlReader); | ||
94 | + document = builder.build(xmlSource); | ||
95 | + List<Map> maps = new ArrayList<Map>(); | ||
96 | + for(int i = 1; i < nodes.length; ++i) { | ||
97 | + SubNode snode; | ||
98 | + | ||
99 | + if (i >1) { | ||
100 | + snode = (SubNode)listNode.getLast(); | ||
101 | + List<Element> eList = snode.getElement().getChildren(nodes[i]); //这里得到所有Arrival节点 从ArrivalEvent开始取 | ||
102 | + if (eList != null && eList.size() > 0) { | ||
103 | + SubNode s1 = new SubNode((Element)eList.get(0)); | ||
104 | + listNode.add(s1); | ||
105 | + | ||
106 | + } | ||
107 | + if(eList != null && eList.size() > 1 && i==nodes.length-1){ //只有进入的最尾节点的时候再循环去取同名下的一对多的ELEMENT所有子节点 | ||
108 | + | ||
109 | + for (Element element : eList){ //开始取出每个ArrivalEven每个字节点 | ||
110 | + List<Element> elements=element.getChildren(); | ||
111 | + Map<String,String> childMap = new HashMap<String, String>(); //用map存储AssociatedTransportCargo节点下每个值 | ||
112 | + for (Element childElements : elements){ | ||
113 | + if (childElements.getChildren().size()>1){ | ||
114 | + for (Element grandChildrenEle : (List<Element>)childElements.getChildren()){ //IncludedMasterConsignment重复节点 | ||
115 | + String childName = grandChildrenEle.getName(); | ||
116 | + String childValue = grandChildrenEle.getText(); | ||
117 | + childMap.put(childName,childValue); | ||
118 | + } | ||
119 | + } | ||
120 | + String childName = childElements.getName(); | ||
121 | + String childValue = childElements.getText(); | ||
122 | + childMap.put(childName,childValue); | ||
123 | + logger.info(childValue); | ||
124 | + } | ||
125 | + maps.add(childMap); | ||
126 | + } | ||
127 | + } | ||
128 | + | ||
129 | + } else if (1 == i) { | ||
130 | + root = document.getRootElement(); | ||
131 | + snode = new SubNode(root); | ||
132 | + listNode.add(snode); | ||
133 | + } | ||
134 | + | ||
135 | + } | ||
136 | + return maps; | ||
137 | + } catch (Exception var17) { | ||
138 | + var17.printStackTrace(); | ||
139 | + throw var17; | ||
140 | + } finally { | ||
141 | + listNode.clear(); | ||
142 | + document = null; | ||
143 | + root = null; | ||
144 | +// builder = null; | ||
145 | + nodes = null; | ||
146 | + } | ||
147 | + | ||
148 | + } | ||
149 | + | ||
150 | + /** | ||
151 | + * 取一对多关系的节点下的所有子节点的属性名 和 值,//这个方法作为FWB解析使用 | ||
152 | + * @return 返回重复节点下的所有节点数据 | ||
153 | + * @throws Exception | ||
154 | + */ | ||
155 | + public List<Map> getNodeValuesFromXmlString(String xmlNodePath) throws Exception{ | ||
156 | + String[] nodes = xmlNodePath.split("/"); | ||
157 | + try { | ||
158 | + xmlReader = new StringReader(xmlString); | ||
159 | + xmlSource = new InputSource(xmlReader); | ||
160 | + document = builder.build(xmlSource); | ||
161 | + List<Map> maps = new ArrayList<Map>(); | ||
162 | + for(int i = 1; i < nodes.length; ++i) { | ||
163 | + SubNode snode; | ||
164 | + | ||
165 | + if (i >1) { | ||
166 | + snode = (SubNode)listNode.getLast(); | ||
167 | + List<Element> eList = snode.getElement().getChildren(nodes[i]); //这里等于3的时候得到associatedPary节点 两个,下面应该循环这两个element,取他下面的所有children | ||
168 | + if (eList != null && eList.size() > 0) { | ||
169 | + SubNode s1 = new SubNode((Element)eList.get(0)); | ||
170 | + listNode.add(s1); | ||
171 | + | ||
172 | + } | ||
173 | + if(eList != null && eList.size() > 1 && i==nodes.length-1){ //只有进入的最尾节点的时候再循环去取同名下的一对多的ELEMENT所有子节点 | ||
174 | + | ||
175 | + for (Element element : eList){ //开始取出每个字节点 | ||
176 | + List<Element> elements=element.getChildren(); | ||
177 | + Map<String,String> childMap = new HashMap<String, String>(); //用map存储每个值 | ||
178 | + for (Element childElements : elements){ | ||
179 | + if (childElements.getChildren().size()>1){ | ||
180 | + for (Element grandChildrenEle : (List<Element>)childElements.getChildren()){ | ||
181 | + String childName = grandChildrenEle.getName(); | ||
182 | + String childValue = grandChildrenEle.getText(); | ||
183 | + childMap.put(childName,childValue); | ||
184 | + } | ||
185 | + } | ||
186 | + String childName = childElements.getName(); | ||
187 | + String childValue = childElements.getText(); | ||
188 | + childMap.put(childName,childValue); | ||
189 | + logger.info(childValue); | ||
190 | + } | ||
191 | + maps.add(childMap); | ||
192 | + } | ||
193 | + } | ||
194 | + | ||
195 | + } else if (1 == i) { | ||
196 | + root = document.getRootElement(); | ||
197 | + snode = new SubNode(root); | ||
198 | + listNode.add(snode); | ||
199 | + } | ||
200 | + | ||
201 | + } | ||
202 | + return maps; | ||
203 | + } catch (Exception var17) { | ||
204 | + var17.printStackTrace(); | ||
205 | + throw var17; | ||
206 | + } finally { | ||
207 | + listNode.clear(); | ||
208 | + document = null; | ||
209 | + root = null; | ||
210 | +// builder = null; | ||
211 | + nodes = null; | ||
212 | + } | ||
213 | + | ||
214 | + } | ||
215 | + | ||
216 | + /*** | ||
217 | + * 获取单一节点的值 | ||
218 | + * @return 单一节点值 | ||
219 | + * @throws Exception | ||
220 | + */ | ||
221 | + public final String getNodeValueFromXmlString(String xmlNodePath) throws Exception { | ||
222 | + String[] nodes = xmlNodePath.split("/"); | ||
223 | + try { | ||
224 | + xmlReader = new StringReader(xmlString); | ||
225 | + xmlSource = new InputSource(xmlReader); | ||
226 | + document = builder.build(xmlSource); | ||
227 | + | ||
228 | + for(int i = 1; i < nodes.length; ++i) { | ||
229 | + SubNode snode; | ||
230 | + if (i > 1) { | ||
231 | + snode = (SubNode)listNode.getLast(); | ||
232 | + List<Element> eList = snode.getElement().getChildren(nodes[i]); | ||
233 | + if (eList != null && eList.size() > 0) { | ||
234 | + SubNode s1 = new SubNode((Element)eList.get(0)); | ||
235 | + listNode.add(s1); | ||
236 | + } | ||
237 | + } else if (1 == i) { | ||
238 | + root = document.getRootElement(); | ||
239 | + snode = new SubNode(root); | ||
240 | + listNode.add(snode); | ||
241 | + } | ||
242 | + } | ||
243 | + | ||
244 | + SubNode n = (SubNode)listNode.getLast(); | ||
245 | + value = n.getElement().getText(); | ||
246 | + n = null; | ||
247 | + return value; | ||
248 | + } catch (Exception var17) { | ||
249 | + var17.printStackTrace(); | ||
250 | + throw var17; | ||
251 | + } finally { | ||
252 | + listNode.clear(); | ||
253 | + document = null; | ||
254 | + root = null; | ||
255 | +// builder = null; | ||
256 | + nodes = null; | ||
257 | + } | ||
258 | + | ||
259 | + } | ||
260 | + | ||
261 | + public static final String getNodeAttribute(String xmlFile, String xmlNodePath, String attributeName, String defaultValue) { | ||
262 | + String[] nodes = xmlNodePath.split("/"); | ||
263 | + new Document(); | ||
264 | + SAXBuilder builder = new SAXBuilder(); | ||
265 | + Element root = null; | ||
266 | + LinkedList<SubNode> listNode = new LinkedList(); | ||
267 | + String value = ""; | ||
268 | + | ||
269 | + Document document; | ||
270 | + try { | ||
271 | + document = builder.build(xmlFile); | ||
272 | + | ||
273 | + for(int i = 1; i < nodes.length; ++i) { | ||
274 | + SubNode snode; | ||
275 | + if (i > 1) { | ||
276 | + snode = (SubNode)listNode.getLast(); | ||
277 | + List<Element> eList = snode.getElement().getChildren(nodes[i]); | ||
278 | + if (eList != null && eList.size() > 0) { | ||
279 | + SubNode s1 = new SubNode((Element)eList.get(0)); | ||
280 | + listNode.add(s1); | ||
281 | + } | ||
282 | + } else if (1 == i) { | ||
283 | + root = document.getRootElement(); | ||
284 | + snode = new SubNode(root); | ||
285 | + listNode.add(snode); | ||
286 | + } | ||
287 | + } | ||
288 | + | ||
289 | + SubNode n = (SubNode)listNode.getLast(); | ||
290 | + value = n.getElement().getAttributeValue(attributeName, defaultValue); | ||
291 | + n = null; | ||
292 | + } catch (JDOMException var18) { | ||
293 | + var18.printStackTrace(); | ||
294 | + } catch (IOException var19) { | ||
295 | + var19.printStackTrace(); | ||
296 | + } finally { | ||
297 | + listNode.clear(); | ||
298 | + document = null; | ||
299 | + root = null; | ||
300 | + builder = null; | ||
301 | + nodes = null; | ||
302 | + } | ||
303 | + | ||
304 | + System.out.println("key=" + xmlNodePath + " node attrivte value=" + value); | ||
305 | + return value; | ||
306 | + } | ||
307 | +} |
1 | +package com.tianbo.util.XML; | ||
2 | + | ||
3 | +import org.dom4j.Attribute; | ||
4 | +import org.dom4j.Document; | ||
5 | +import org.dom4j.Node; | ||
6 | + | ||
7 | +import java.util.Iterator; | ||
8 | +import java.util.List; | ||
9 | +import java.util.Map; | ||
10 | + | ||
11 | +public class XMLXPath { | ||
12 | + | ||
13 | + public static String getSingleValueByPath(Document document,String path){ | ||
14 | + Node node = document.selectSingleNode(path); | ||
15 | + if (node!=null){ | ||
16 | + String nodeValue = node.getStringValue(); | ||
17 | + return nodeValue; | ||
18 | + }else { | ||
19 | + return null; | ||
20 | + } | ||
21 | + | ||
22 | + } | ||
23 | + public static List<Node> getPathValues(Document document,String path){ | ||
24 | + List<Node> nodes= document.selectNodes(path); | ||
25 | + return nodes; | ||
26 | + } | ||
27 | + | ||
28 | + public static void getPathValues2(Document document,String path){ | ||
29 | + List list = document.selectNodes(path); | ||
30 | + for (Iterator it = list.iterator(); it.hasNext();) { | ||
31 | + Attribute attr = (Attribute) it.next(); | ||
32 | + //TODO | ||
33 | + } | ||
34 | + } | ||
35 | +} |
-
请 注册 或 登录 后发表评论