对xml文本元素赋值

一、添加依赖(版本自行更改)
<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.3</version>
</dependency>
二、使用Document对象的方法来操作XML文档。例如,获取根元素、获取指定元素、修改元素属性等:
方法一:
SAXReader reader = new SAXReader();
Document doc = reader.read("example.xml");

方法二:
private byte[] medContent;

InputStream inputStream = new ByteArrayInputStream(new String(MedVO.getContent(), StandardCharsets.UTF_8).getBytes(StandardCharsets.UTF_8));
			Document doc = new SAXReader().read(inputStream);
			inputStream.close();

方法三:
import org.dom4j.DocumentHelper;

Document doc = DocumentHelper.parseText(XmlStrings.HOSPITALIZATION_DIAGNOSIS);
public static void main(String[] args) {
         Map map = ....;//数据
        Iterator it = doc.getRootElement().elementIterator();//doc是xml模板
 		//将元素ID与输入框的值放在map中
        while (it.hasNext()) {
            org.dom4j.Element nextRoot = (org.dom4j.Element) it.next();
            Iterator nextIt = nextRoot.elementIterator();
            if (nextIt.hasNext()) {
                //当前xml标签下,存嵌套了其他标签
                getTemplateMap(nextIt, map);
            } else {
                //当前xml标签下,只有值,未嵌套其他标签
                if ("InnerValue".equals(nextRoot.getName())) {
                    if (null != map.get(nextRoot.getText())) {
                        nextRoot.getParent().element("XElements").element("Element").element("Text").setText(map.get(nextRoot.getText()).toString());
                    }
                }
                if ("AttributeNameForContactAction".equals(nextRoot.getName())) {
                    if (null != map.get(nextRoot.getText())) {
                        nextRoot.setText(map.get("daqtaskid").toString());
                    }
                }
            }
        }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值