I want to add an attribute to an existing xml node.I don't want to add new elements (new nodes) to my xml file, I just want to add a new attribute. How can I do this?
我想为现有的xml节点添加一个属性。我不想在我的xml文件中添加新元素(新节点),我只想添加一个新属性。我怎样才能做到这一点?
In particular I've tried this lines of code:
特别是我尝试过这行代码:
Element process = doc.getElementsById("id");
process.setAttribute("modelgroup", "");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\\Users\\Blerta\\workspaceKEPLER\\XML_to_JSON\\SampleExample.xml"));
transformer.transform(source, result);
But I get the following exception:
但我得到以下异常:
Exception in thread "main" java.lang.NullPointerException
at Main.appendAttributes(Main.java:172)
at Main.displayNodes(Main.java:65)
at Main.displayNodes(Main.java:138)
at Main.main(Main.java:42)**
4 个解决方案
#1
17
in DOM parser it is very easy. get your node and simply use this function.
在DOM解析器中它非常容易。获取您的节点,只需使用此功能。
((Element)node).setAttribute("attr_name","attr_value");
then finally update your document. like this..
然后最后更新你的文件。喜欢这个..
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(tablePath));
transformer.transform(source, result);
#2
6
The easiest and the shortest is to cast the node to org.w3c.dom.Element and then invoke setAttribute on it:
最简单和最简单的方法是将节点强制转换为org.w3c.dom.Element,然后在其上调用setAttribute:
((Element)aNode).setAttribute("name", "value");
#3
3
You could do it in a few lines using xslt. Oracle have a half decent tutorial with all the code snippets http://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html
你可以使用xslt在几行中完成它。 Oracle有一个很好的教程,包含所有代码片段http://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html
The key bit for your xslt would be something like the following:
xslt的关键位如下所示:
<xsl:template match="elementToAddNewAttrTo">
<xsl:attribute name="newAttrName">NewAttrValue</xsl:attribute>
</xsl:template>
#4
0
Recommended approach:
推荐方法:
Node node = ...;
if(node.getNodeType() == Node.ELEMENT_NODE)
{
((Element) node).setAttribute("name", "value");
}
Situational approach:
情境方法:
try
{
// ...
Node node = ...;
((Element) node).setAttribute("name", "value");
// ...
}
catch(ClassCastException e)
{
// Handle exception
}
Only use the try-catch approach if you already know that all the nodes that you process should be of type 'Element' (and thus any other case is an "exception" and should break from the code).
如果您已经知道您处理的所有节点都应该是“Element”类型(因此任何其他情况都是“异常”并且应该从代码中断),则仅使用try-catch方法。
#1
17
in DOM parser it is very easy. get your node and simply use this function.
在DOM解析器中它非常容易。获取您的节点,只需使用此功能。
((Element)node).setAttribute("attr_name","attr_value");
then finally update your document. like this..
然后最后更新你的文件。喜欢这个..
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(tablePath));
transformer.transform(source, result);
#2
6
The easiest and the shortest is to cast the node to org.w3c.dom.Element and then invoke setAttribute on it:
最简单和最简单的方法是将节点强制转换为org.w3c.dom.Element,然后在其上调用setAttribute:
((Element)aNode).setAttribute("name", "value");
#3
3
You could do it in a few lines using xslt. Oracle have a half decent tutorial with all the code snippets http://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html
你可以使用xslt在几行中完成它。 Oracle有一个很好的教程,包含所有代码片段http://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html
The key bit for your xslt would be something like the following:
xslt的关键位如下所示:
<xsl:template match="elementToAddNewAttrTo">
<xsl:attribute name="newAttrName">NewAttrValue</xsl:attribute>
</xsl:template>
#4
0
Recommended approach:
推荐方法:
Node node = ...;
if(node.getNodeType() == Node.ELEMENT_NODE)
{
((Element) node).setAttribute("name", "value");
}
Situational approach:
情境方法:
try
{
// ...
Node node = ...;
((Element) node).setAttribute("name", "value");
// ...
}
catch(ClassCastException e)
{
// Handle exception
}
Only use the try-catch approach if you already know that all the nodes that you process should be of type 'Element' (and thus any other case is an "exception" and should break from the code).
如果您已经知道您处理的所有节点都应该是“Element”类型(因此任何其他情况都是“异常”并且应该从代码中断),则仅使用try-catch方法。