XML转化为具体的实体类**
⼤体思路:
**
先将报⽂转化为List< HashMap<String, Object>>集合,在将HashMap转化为具体实体类。entity:
@Data
public class ClearRoutingClearTask {
private String index;
private String taskType;
private Long priority;
}
第⼀种xml报⽂:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<routing-task name=\"销售费R1001-187501-0\">
<clear-task>
<index>1</index>
<task-type>20902</task-type>
<priority>1</priority>
</clear-task><clear-task>
<index>2</index>
<task-type>20903</task-type>
<priority>2</priority>
</clear-task>
</routing-task>
/**
* xml报⽂解析成list集合
* @param xml
* @return
* @throws DocumentException
*/
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.springframework.stereotype.Component;
public static List< HashMap<String, Object>> TransferSubTask(String xml) throws DocumentException { List<HashMap<String, Object>> subList = new ArrayList<>();
Document document = DocumentHelper.parseText(xml);
Element clearTaskElt = RootElement();
Iterator iterator = clearTaskElt.elementIterator(“clear-task”);
while (iterator.hasNext()) {
Element clearSubTaskElt = (Element) ();
String index = clearSubTaskElt.elementTextTrim(“index”);
String taskType = clearSubTaskElt.elementTextTrim(“task-type”);
String priority = clearSubTaskElt.elementTextTrim(“priority”);
HashMap<String, Object> subTaskTemplateMap = new HashMap<>();
subTaskTemplateMap.put(“index”, index);
subTaskTemplateMap.put(“taskType”, taskType);
subTaskTemplateMap.put(“priority”, priority);
subList.add(subTaskTemplateMap);
}
return subList;
}
@Test
public void test6() throws AppException, DocumentException, InvocationTargetException, IllegalAccessException {
List clearRoutingClearTasks = new ArrayList<>();
String str ="{\n" +
" “createTime”: “2021-07-27T11:17:48.000+0800”,\n" +
" “remark”: “销售费”,\n" +
" “updateTime”: “2021-07-27T11:17:48.000+0800”,\n" +
" “clearRoutingKey”: “R1001-187501-0”,\n" +
" “clearTask”: “<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><routing-task name=\“销售费R1001-187501-0\”> 1209021 2209032”,\n" +
" “returnType”: “0”\n" +
“}”;
JSONObject jsonObject = JSONObject.parseObject(str);
System.out.println(“jsonObject-----”+jsonObject);
ClearRoutingConfigDO clearRoutingConfigDO = JavaObject(jsonObject,ClearRoutingConfigDO.class);
System.out.println(“clearRoutingConfigDO------”+clearRoutingConfigDO);
String clearTask = ClearTask();
List<HashMap<String, Object>> beans = XmlEntityConvertHelper.TransferSubTask(clearTask);
for (HashMap<String, Object> bean : beans) {
ClearRoutingClearTask clearRoutingClearTask = new ClearRoutingClearTask();
BeanUtils.populate(clearRoutingClearTask, bean);
clearRoutingClearTasks.add(clearRoutingClearTask);
}
}
第⼆种xml格式:
使用dom4j解析xml文件<?xml version="1.0" encoding="UTF-8" standalone="no"?>
< routing-clear-judge name="证⾦债⼀级外部买⼊RIV2004O">
< organization>1< /organization>
< new-process>1< /new-process>
< bank-settlement-mode>condition1< /bank-settlement-mode>
< exchange-settlement-mode>0< /exchange-settlement-mode>
< registration-institution>0< /registration-institution>
< counterparty>0< /counterparty>
< /routing-clear-judge>
/**
* xml报⽂解析成清算任务
* @param xml
* @return
*/
public static ClearRoutingClearTypeConfig TransferClearType(String xml) throws IOException, JDOMException { ClearRoutingClearTypeConfig clearRoutingClearTypeConfig = new ClearRoutingClearTypeConfig();
Map<String, Object> map = new HashMap<>();
//将xml中的回车和换⾏去掉
if (xml != null && !xml.equals("")) {
Pattern p = compile("\r|\n");
Matcher m = p.matcher(xml);
xml = m.replaceAll("");
//创建⼀个新的字符串
StringReader read = new StringReader(xml);
//创建新的输⼊源SAX解析器将使⽤ InputSource对象来确定如何读取 XML输⼊
InputSource source = new InputSource(read);
//创建⼀个新的SAXBuilder
SAXBuilder sb = new SAXBuilder();
//通过输⼊源构造⼀个Document
org.jdom.Document doc = sb.build(source);
//取的根元素
org.jdom.Element root = RootElement();
System.out.Name());//输出根元素的名称(测试)
/
/得到根元素所有⼦元素的集合
List node = Children();
org.jdom.Element et = null;
//循环依次得到⼦元素
for (int i = 0; i < node.size(); i++) {
et = (org.jdom.Element) (i);
map.Name(), et.getValue());
}
//将map集合中的数据取出
clearRoutingClearTypeConfig.setOrganization(String.(“organization”))); clearRoutingClearTypeConfig.setNewProcess(String.(“new-process”))); clearRoutingClearTypeConfig.setBankSettlementMode(String.(“bank-settlement-mo
de”))); clearRoutingClearTypeConfig.setExchangeSettlementMode(String.(“exchange-settlement-mode”))); clearRoutingClearTypeConfig.setRegistrationInstitution(String.(“registration-institution”))); clearRoutingClearTypeConfig.setCounterParty(String.(“counterparty”))); clearRoutingClearTypeConfig.setIsTgPay(String.(“is-tg-pay”)));
}
return clearRoutingClearTypeConfig;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论