我创建了一个删除方法。 我的方法有一个索引作为参数来自jlist位置。 该方法将从用户单击的jlist中获取位置,并在xml中查找该元素并删除它和所有子项。
因此,如果用户单击TrainCategorySpeed,它具有索引122并将在XML中查找该类型
<type>
<OBJECT_TYPE>TrainCategorySpeed</OBJECT_TYPE>
- <prop>
<DESCRIPTION>Differential speed limit</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>0</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>NULL</HELP>
<MIN_NO>NULL</MIN_NO>
<MAX_NO>NULL</MAX_NO>
<NAME_FORMAT>NULL</NAME_FORMAT>
</prop>
- <param>
<PARAMETER>trainCategory</PARAMETER>
<DATA_TYPE>INTEGER</DATA_TYPE>
<DESCRIPTION>Train category</DESCRIPTION>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<ORDER1>1</ORDER1>
<NESTED>1</NESTED>
<DEFAULT1>NULL</DEFAULT1>
<FORMAT>0:15</FORMAT>
</param>
- <param>
<PARAMETER>speed</PARAMETER>
<DATA_TYPE>INTEGER</DATA_TYPE>
<DESCRIPTION>Speed (km/h (V_DIFF))</DESCRIPTION>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<ORDER1>2</ORDER1>
<NESTED>1</NESTED>
<DEFAULT1>NULL</DEFAULT1>
<FORMAT>0:600:5</FORMAT>
</param>
</type>
但是当我试图实现这一点时,它给了我一个nullpointer异常
在下面一行:Node type = doc.getElementsByTagName("type").item(x);
这里的x确实给出了正确的位置,而type是xml中的元素。
但类型为null。
这是方法的其余部分:
public void deleteObjType(int x) {
System.out.println("index : " + x); // This one will return index : 122 in the console and it does
File file = new File("xmlFiles/CoreDatamodel.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(file);
Node type = doc.getElementsByTagName("type").item(x);
System.out.println("type : " + type);
NodeList nodes = type.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node element = nodes.item(x);
System.out.println("element : " + element);
// remove node
if ("OBJECT_TYPE".equals(element.getNodeName())) {
type.removeChild(element);
}
}
// Save the new update
save(file, doc);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
完整的堆栈跟踪:
java.lang.NullPointerException
at cxmleditor.service.XMLEditorService.deleteObjType(XMLEditorService.java:106)
at xmleditor.gui.XmlEditorMain$deleteObjType.actionPerformed(XmlEditorMain.java:383)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:0)
我认为您的问题如下:
for (int i = 0; i < nodes.getLength(); i++) {
请注意,删除节点时我不应该增加。
答案 1 :(得分:0)
问题不一定是您的类型对象为null。当deleteObjType(0);在你提供的简单xml上调用
System.out.println("type : " + type);
只使用类型对象实例的toString()方法并打印出来:
type : [type: null]
如果您改为
System.out.println("type : " +type);
if(type == null) {
System.out.println("type is null!");
}else {
System.out.println("type is not null but an instance of ["+type.getClass().getCanonicalName()+"]");
}
它打印在我的情况下:
type is not null but an instance of [com.sun.org.apache.xerces.internal.dom.DeferredElementImpl]
DOM也比具有子元素as you can see here的直接元素节点的元素节点复杂一点。我通过实践了解到,简单地打印节点通常是不够的。 你真的想要删除xml的哪一部分?