JavaでDOMパーサーを使用して、既存のノードに子ノードを追加しようとしています。Java DOM getElementByID
私のXMLは
<?xml version="1.0" encoding="iso-8859-1"?>
<chart>
<chart renderTo="pieContainer" defaultSeriesType="pie" zoomType="xy" plotBackgroundColor="null" plotBorderWidth="null" plotShadow="false"></chart>
<legend id="legendNode">
<align>center</align>
<backgroundColor>null</backgroundColor>
<borderColor>#909090</borderColor>
<borderRadius>25</borderRadius>
</legend>
</chart>
直接、既存のものの下に子ノードを追加する方法はありますか?このようなものを使うことはできますか?
Node myNode = nodesTheme.item(0);
this.widgetDoc.getElementById("/chart/legend").appendChild(myNode);
マイコード
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class TestGetElementById {
public static void main(String[] args) throws Exception {
String widgetXMLFile = "piechart.xml";
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = domFactory.newDocumentBuilder();
Document doc = docBuilder.parse(widgetXMLFile);
Node n = doc.getElementById("/chart/legend");
//Node n = doc.getElementById("legendTag");
Element newNode = doc.createElement("root");
n.appendChild(newNode);
}
}
はいノードは問題なく追加できます。それは何か例外や問題を投げているのですか? – Phani
はいそのスロー例外**スレッド "main"の例外java.lang.NullPointerException ** –
@AnupSingh - Javaエラーメッセージです。あなたが提供したコードはそのエラーを生成しません。 – Quentin