You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java修改XML文件:如何为vaext:state节点添加文本内容

解决XML节点添加文本内容的问题

你已经成功修改了vaext:state节点的name和href属性,要给该节点添加文本内容,直接调用节点的setTextContent()方法即可——该方法会覆盖节点原有的所有子节点(包括文本),正好适合给空节点设置新文本。

修改后的完整代码如下:

InputSource src = new InputSource(new StringReader(xmlString));
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src);
Node state = document.getElementsByTagName("state").item(0);

// 修改name属性
Node currentstate = state.getAttributes().getNamedItem("name");
currentstate.setTextContent("ready");

// 修改href属性为目标链接
Node href = state.getAttributes().getNamedItem("href");
href.setTextContent("http://test.comt/content/state/workflow/online_staging/published?role=editor");

// 给vaext:state节点添加文本内容
state.setTextContent("un-publish");

补充提示:

如果XML文档中存在多个命名空间下的state节点,建议使用带命名空间的查找方法getElementsByTagNameNS()精准定位,避免误选其他节点:

// 指定vaext的命名空间URI和节点名
Node state = document.getElementsByTagNameNS("http://www.vizrt.com/atom-ext", "state").item(0);

内容的提问来源于stack exchange,提问作者Maymanaf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 18:11:03