hutool使用之XmlUtil工具类

获取xml节点进行重新组合返回

<!--必须5.7以上,因为XmlUtil.str(节点)功能只有高版本才有-->

package com.neusoft.nhip.demo;

import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Test {
    public static void main(String[] args) {
        String input ="<RESULT>\n" +
                "\t<id root=\"2.999.1.96\" extension=\"e92acb6f-87cf-4084-8b14-3f04d2bef3d2\"/>\n" +
                "\t<creationTime value=\"20130501130624001\"/>\n" +
                "\t<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"PRPA_IN000011UV01\"/>\n" +
                "\t<receiver typeCode=\"RCV\">\n" +
                "\t\t<telecom value=\"\"/>\n" +
                "\t\t<device classCode=\"DEV\" determinerCode=\"INSTANCE\">\n" +
                "\t\t\t<id root=\"2.999.1.97\" extension=\"XXX\"/>\n" +
                "\t\t\t<telecom value=\"设备编号\"/>\n" +
                "\t\t\t<softwareName code=\"XXX\" displayName=\"XXXX系统\" codeSystem=\"2.999.2.3.2.84\" codeSystemName=\"平台系统域代码表\"/>\n" +
                "\t\t</device>\n" +
                "\t</receiver>\n" +
                "\t<sender typeCode=\"SND\">\n" +
                "\t\t<telecom value=\"\"/>\n" +
                "\t\t<device classCode=\"DEV\" determinerCode=\"INSTANCE\">\n" +
                "\t\t\t<id root=\"2.999.1.98\" extension=\"MDM\"/>\n" +
                "\t\t\t<telecom value=\"设备编号\"/>\n" +
                "\t\t\t<softwareName code=\"MDM\" displayName=\"主数据管理系统\" codeSystem=\"2.999.2.3.2.84\" codeSystemName=\"平台系统域代码表\"/>\n" +
                "\t\t</device>\n" +
                "\t</sender>\n" +
                "\t<controlActProcess classCode=\"CACT\" moodCode=\"APT\">\n" +
                "\t\t<authorOrPerformer typeCode=\"AUT\">\n" +
                "\t\t\t<signatureText>签名字符串</signatureText>\n" +
                "\t\t\t<assignedDevice classCode=\"ASSIGNED\"/>\n" +
                "\t\t</authorOrPerformer>\n" +
                "\t\t<subject typeCode=\"SUBJ\">\n" +
                "\t\t\t<request>\n" +
                "\t\t\t\t<!--主数据内容 1..1-->\n" +
                "\t\t\t\t<!-- 单位代码 -->\n" +
                "\t\t\t\t<deptCode>DT0001</deptCode>\n" +
                "\t\t\t\t<DOEVENT/>\n" +
                "\t\t\t</request>\n" +
                "\t\t</subject>\n" +
                "\t</controlActProcess>\n" +
                "</RESULT>";

        Document doc = XmlUtil.parseXml(input);
        Element rootElement =XmlUtil.getRootElement(doc);
        Element elementId = XmlUtil.getElement(rootElement, "id");
        Element creationTime = XmlUtil.getElement(rootElement, "creationTime");
        //当前时间戳
        creationTime.setAttribute("value", System.currentTimeMillis()+"");
        Element interactionId = XmlUtil.getElement(rootElement, "interactionId");
        //修改返回标签
        interactionId.setAttribute("extension", "PRPA_IN000011UV03");

        Element receiver = XmlUtil.getElement(rootElement, "receiver");
        //返回参数中返回变发送
        receiver.setAttribute("typeCode", "SND");
        Element sender = XmlUtil.getElement(rootElement, "sender");
        //返回参数中发送变返回
        sender.setAttribute("typeCode", "RCV");

        //拼接新的结果xml节点
        String typeCode = "AE";
        String text = "失败";
        String rootValue = elementId.getAttribute("root");
        String extensionVaue = elementId.getAttribute("extension");
        String acknowledgement ="<acknowledgement typeCode=\""+typeCode+"\"><targetMessage><id root=\""+rootValue+"\" extension=\""+extensionVaue+"\"/></targetMessage><acknowledgementDetail><resultMessage><text>"+text+"</text></resultMessage></acknowledgementDetail></acknowledgement>";

        //拼接新的xml
        StringBuffer content = new StringBuffer();
        content.append(XmlUtil.toStr(elementId,"UTF-8",false,true));
        content.append(XmlUtil.toStr(creationTime,"UTF-8",false,true));
        content.append(XmlUtil.toStr(interactionId,"UTF-8",false,true));
        content.append(XmlUtil.toStr(sender,"UTF-8",false,true));
        content.append(XmlUtil.toStr(receiver,"UTF-8",false,true));
        content.append(acknowledgement);

        String result = "<RESULT3>"+content.toString()+"</RESULT3>";
        Document newDoc = XmlUtil.parseXml(result);

        String str = XmlUtil.toStr(newDoc,"UTF-8",false,true);
        System.out.println(str.replace("\n",""));

    }
}
<!--必须5.7以上,因为XmlUtil.str(节点)功能只有高版本才有-->
<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>5.8.16</version>
</dependency>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值