gpt4 book ai didi

java - 使用java更改xml标签的值

转载 作者:行者123 更新时间:2023-12-02 01:30:02 26 4
gpt4 key购买 nike

我有以下 xml 格式:

<component>
<observation classCode="OBS" moodCode="EVN">
<value unit="mm[Hg]" value="120.0" xsi:type="PQ"/>
</observation>
</component>

我想将值“120.0”更改为“120”。因此,使用 java 代码删除小数部分。

目前,我的代码如下,但不完整。

    NodeList nodePhysical = dom.getElementsByTagName("observation");

for (int i = 0; i < nodePhysical.getLength(); i++) {
Node node = nodePhysical.item(i);

NodeList childNodes = node.getChildNodes();

}

最佳答案

试试这个,

    NodeList nodePhysical = dom.getElementsByTagName("observation");
for (int i = 0; i < nodePhysical.getLength(); i++) {
Node node = nodePhysical.item(i);
NodeList childNodes = node.getChildNodes();
Element ele;
for (int count = 0; count < childNodes.getLength(); count++) {
ele= (Element) childNodes.item(count);
ele.setAttribute("value",ele.getAttribute("value").split("\\.")[0]);
}
}

关于java - 使用java更改xml标签的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56222194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com