java - 使用 SaxParser 修改 xml 属性
问题描述
我有一个 XML,在其中我想使用 SAXParser 为特定节点修改链接属性的值。
我确实找到了https://stackoverflow.com/a/51662488/9681353并且我在我的案例中使用了 Attributes2Impl。
到目前为止我所拥有的:
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
UpdateLinksHandler linksHandler = new UpdateLinksHandler();
saxParser.parse(xml, linksHandler);
@Override
public void startElement(String uri, String key, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase(USEROBJECT)) {
String link = attributes.getValue("link");
if (link != null) {
Attributes2 attrs = (Attributes2) attributes;
Attributes2Impl newAttrs = new Attributes2Impl();
for (int i = 0; i < attrs.getLength(); i++) {
if (attrs.isSpecified(i)) {
String type = attrs.getType(i);
String value = attrs.getValue(i);
String name = attrs.getQName(i);
if (name == "link") {
value = modifyLink(value);
}
newAttrs.addAttribute(null, null, name, type, modifyLink(value));
}
}
super.startElement(uri, key, qName, newAttrs);
}
}
但我不知道我的方法是否正确,或者我应该如何获取修改后的 xml。还是 SAXParser 仅用于阅读目的?
谢谢
解决方案
推荐阅读
- c++ - 如何在CUDA中更有效地做复数的模?
- java - 调用WS GET api时播放Framework + Java + 504网关超时
- node.js - $lookup 返回一个空数组 mongoose
- python-3.x - python 3中带有两个返回的单行for循环
- iphone - 在离子中选择后再次打开离子丢失时,如何在单选按钮中显示选中的值
- function - Ada - 包内的函数。错误说包应该在函数结束的地方结束
- python - 使用python执行导出命令到当前终端
- android - 如何在颤动中延迟 Visibilty 动画?比如等待2秒然后开始动画
- reactjs - 错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能由于以下原因之一而发生:
- python - sklearn.feature_selection chi2 为不同的标签识别相同的一元和二元