解析xml、生成xml
1. 解析-分析:
Document doc = DocumentHelper.parseText(xmlStr);
Element rootElement = doc.getRootElement();
parseXml(rootElement);
一层一层获得 Element.
2. 解析-xml文件:
<?xml version="1.0" encoding="utf-8"?>
<VTTSEventS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="2020.xsd">
<VTTSEvent>
<eventBodyList>
<eventBody>
<datasetName>WareHouseStock1</datasetName>
<recTime>2019-10-21 10:00:01</recTime>
<eventID>100001</eventID>
<evtBasic>
<YSDSJH>331648d272954932a982b9bd9096c50b1</YSDSJH>
<CXYY>单据错误1</CXYY>
<CXSJ>2019-10-21 10:00:01</CXSJ>
</evtBasic>
</eventBody>
<eventBody>
<datasetName>WareHouseStock2</datasetName>
<recTime>2019-10-21 10:00:02</recTime>
<eventID>100002</eventID>
<evtBasic>
<YSDSJH>331648d272954932a982b9bd9096c50b2</YSDSJH>
<CXYY>单据错误2</CXYY>
<CXSJ>2019-10-21 10:00:02</CXSJ>
</evtBasic>
</eventBody>
</eventBodyList>
</VTTSEvent>
</VTTSEventS>
3. 解析-xml:
package com.sy.synergism.base.dzip.web; import com.alibaba.fastjson.JSONObject; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.io.*; import java.util.ArrayList; import java.util.List; public class ParseXml { public static void main(String[] args) throws Exception { System.out.println("hello java"); // File -》byte[] -》InputStream -》String or byte[] File file = new File("D:\\test\\2020.xml"); byte[] b = file2byte(file); try (InputStream fileStream = new ByteArrayInputStream(b)) { String xmlStr = input2Str(fileStream); Document doc = DocumentHelper.parseText(xmlStr); if (doc != null) { Element rootElement = doc.getRootElement(); if (rootElement != null) { List<JSONObject> list = parseXml(rootElement); System.out.println(list); } } } } /** * 解析xml * * @param rootElement * @return */ private static List<JSONObject> parseXml(Element rootElement) { List<JSONObject> list = new ArrayList<>(); if (rootElement != null) { Element vttsEventS = rootElement.element("VTTSEvent"); Element eventBodyList = vttsEventS.element("eventBodyList"); List<Element> eventBodys = eventBodyList.elements("eventBody"); for (Element eventBody : eventBodys) { Element evtBasic = eventBody.element("evtBasic"); Element ysdsjh = evtBasic.element("YSDSJH"); Element cxyy = evtBasic.element("CXYY"); Element cxsj = evtBasic.element("CXSJ"); JSONObject data = new JSONObject(); data.put("ysdsjh", ysdsjh == null ? "" : ysdsjh.getText()); data.put("cxyy", cxyy == null ? "" : cxyy.getText()); data.put("cxsj", cxsj == null ? "" : cxsj.getText()); list.add(data); } } return list; } /** * 将文件,转成byte数组 * * @param file * @return */ public static byte[] file2byte(File file) { byte[] buffer = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); FileInputStream fis = new FileInputStream(file); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return buffer; } /** * InputStream 转成 String * * @param fileStream * @return * @throws IOException */ public static String input2Str(InputStream fileStream) throws IOException { StringBuffer sb = new StringBuffer(); BufferedReader br = new BufferedReader(new InputStreamReader(fileStream)); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); return sb.toString(); } }
4. 解析-结果:
hello java
[{"cxsj":"2019-10-21 10:00:01","ysdsjh":"331648d272954932a982b9bd9096c50b1","cxyy":"单据错误1"}, {"cxsj":"2019-10-21 10:00:02","ysdsjh":"331648d272954932a982b9bd9096c50b2","cxyy":"单据错误2"}]
5. 生成-xml:
package com.sy.synergism.base.dzip.web; import org.springframework.util.CollectionUtils; import java.util.Arrays; import java.util.List; public class CreateXml { public static void main(String[] args) throws Exception { System.out.println("hello java"); List<String> list = Arrays.asList("1111", "1112", "1113"); String xml = createXml("2020-12-12", 12, list); System.out.println(xml); } private static String createXml(String time, int total, List<String> eventIds) { StringBuilder builder = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); builder.append("<resp>"); builder.append("<time>").append(time).append("</time>"); builder.append("<total>").append(total).append("</total>"); builder.append("<eventIdList>"); if (!CollectionUtils.isEmpty(eventIds)) { for (String eventId : eventIds) { builder.append("<eventId>").append(eventId).append("</eventId>"); } } builder.append("</eventIdList>"); builder.append("</resp>"); return builder.toString(); } }
6. 生成-结果:
hello java
<?xml version="1.0" encoding="utf-8" ?><resp><time>2020-12-12</time><total>12</total><eventIdList><eventId>1111</eventId><eventId>1112</eventId><eventId>1113</eventId></eventIdList></resp>