gpt4 book ai didi

java - 使用 java 从 XML 文件中删除节点时出现问题

转载 作者:行者123 更新时间:2023-12-02 03:47:48 28 4
gpt4 key购买 nike

目前我需要删除 XML 文件中的特定节点及其子节点,但是每当我尝试删除节点时,我总是遇到空指针异常。 “位置”参数是要删除的节点的编号。例如,位置 3 应删除预订 ID(04113049) 及其下的所有内容。

public void removeReservation(int position){

try{

File file = new File("reservations.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
Element element = (Element)doc.getElementsByTagName("reservation").item(position);
element.getParentNode().removeChild(element);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);

StreamResult result = new StreamResult(new File("reservations.xml"));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);


}
catch(Exception e){
e.printStackTrace();
}
}

以下是 xml 文件的内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
<reservation_list>

<reservation>
<resID>01014664</resID>
<roomNo>0101</roomNo>
<roomType>VIPSuite</roomType>
<noOfGuest>3</noOfGuest>
<bedType>Master</bedType>
<smoking>Y</smoking>
<startDate>121313</startDate>
<endDate>121316</endDate>
<wifi>Y</wifi>
<roomView>Y</roomView>
<availability>Reserved</availability>
<name>Johnny depp</name>
<address>NTU Hall 17 #01-111</address>
<country>Singapore</country>
<gender>Male</gender>
<nationality>Singaporean</nationality>
<contact>92003239</contact>
<creditCardNo>1234567812345678</creditCardNo>
<creditCardCSV>432</creditCardCSV>
<creditCardExpDate>11/16</creditCardExpDate>
<identity>U0000000I</identity>
</reservation>

<reservation>
<resID>11025652</resID>
<roomNo>1102</roomNo>
<roomType>Double</roomType>
<noOfGuest>3</noOfGuest>
<bedType>Master</bedType>
<smoking>Y</smoking>
<startDate>1212</startDate>
<endDate>1213</endDate>
<wifi>Y</wifi>
<roomView>Y</roomView>
<availability>Reserved</availability>
<name>Thomas</name>
<address>Mountbatten #2-12 Garden ave</address>
<country>Singapore</country>
<gender>Male</gender>
<nationality>Singaporean</nationality>
<contact>93482032</contact>
<creditCardNo>1234567812345678</creditCardNo>
<creditCardCSV>588</creditCardCSV>
<creditCardExpDate>3/16</creditCardExpDate>
<identity>U1234567I</identity>
</reservation>

<reservation>
<resID>04113049</resID>
<roomNo>0411</roomNo>
<roomType>VIPSuite</roomType>
<noOfGuest>7</noOfGuest>
<bedType>Master</bedType>
<smoking>Y</smoking>
<startDate>121112</startDate>
<endDate>232333</endDate>
<wifi>Y</wifi>
<roomView>Y</roomView>
<availability>Reserved</availability>
<name>elaine</name>
<address>punggol</address>
<country>Singapore</country>
<gender>Female</gender>
<nationality>Singaporean</nationality>
<contact>12345672</contact>
<creditCardNo>1234123412341234</creditCardNo>
<creditCardCSV>123</creditCardCSV>
<creditCardExpDate>1212</creditCardExpDate>
<identity>S96777777777F</identity>
</reservation>
</reservation_list>
</data>

最佳答案

首先,使用 item() 进行过滤是从零开始的(从索引 0 开始),文件中没有 item(3)。

其次,在尝试删除该职位之前,您应该始终检查是否能够找到该职位的预订。就您而言,我认为您正在尝试对 null 元素执行 .getParentNode(),这就是您看到 NullPointer 的原因。

Element element = (Element)doc.getElementsByTagName("reservation").item(position);
if ( null != element) {
element.getParentNode().removeChild(element);
//etc
}

关于java - 使用 java 从 XML 文件中删除节点时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36157958/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com