xml-java中的编辑属性
我想知道如何在一个节点内编辑另一个节点内的属性。这是我的xml的一个示例:xml-java中的编辑属性,java,xml,dom,xml-parsing,Java,Xml,Dom,Xml Parsing,我想知道如何在一个节点内编辑另一个节点内的属性。这是我的xml的一个示例: 规则1 我想将每个规则的属性rightTerm更改为20,但我无法访问它,并且得到了NullReferenceException 我正在使用JavaDOM解析器 Node rule = doc.getElementsByTagName("rule").item(0); // update IF attribute Node if=rule.getFirstChild(); Na
规则1
我想将每个规则的属性rightTerm更改为20,但我无法访问它,并且得到了NullReferenceException
我正在使用JavaDOM解析器
Node rule = doc.getElementsByTagName("rule").item(0);
// update IF attribute
Node if=rule.getFirstChild();
NamedNodeMap attr = if.getAttributes();
Node nodeAttr = attr.getNamedItem("rightTerm");
nodeAttr.setTextContent("20");
或者你可以直接做
Node If= doc.getElementsByTagName("if").item(0);
NamedNodeMap attr = If.getAttributes();
Node nodeAttr = attr.getNamedItem("rightTerm");
nodeAttr.setTextContent("20");
Node If= doc.getElementsByTagName("if").item(0);
NamedNodeMap attr = If.getAttributes();
Node nodeAttr = attr.getNamedItem("rightTerm");
nodeAttr.setTextContent("20");