简体   繁体   English

使用Java从XML文件删除节点时出现问题

[英]Problems removing nodes from XML file using java

currently I'm required to remove a specific node and its child in an XML file , however I always encountered null pointer exception whenever I'm trying to remove the nodes. 目前,我需要在XML文件中删除特定的节点及其子节点,但是无论何时尝试删除节点,我总是会遇到空指针异常。 The "position" parameter would be the # of node to remove. “ position”参数将是要删除的节点数。 eg position 3 should remove reservation id(04113049)and everything under it. 例如,位置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();
    }
}

Here are the contents of the xml file: 这是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>

First, filtering using item() is zero-based ( starts from index 0 ), there is no item(3) in your file. 首先,使用item()进行的过滤基于零(从索引0开始),文件中没有item(3)。

Second, you should always check that you are able to find a reservation for that position before you are trying to remove it. 其次,在尝试删除该职位之前,应始终检查是否能够找到该职位的保留。 In your case, I think you're trying to do .getParentNode() on a null element which is why you're seeing the NullPointer. 就您而言,我认为您正在尝试对null元素执行.getParentNode(),这就是为什么您看到NullPointer的原因。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM