无法删除java中xml文件中的节点

时间:2014-01-24 12:59:23

标签: java xml nodes

我正在尝试删除XML文件中的特定节点。 我能够获取我必须删除的属性ID,但后来无法删除该节点。它给出了例外。

这是我的代码:

public class RemoveXmlElement {

        public static final String xmlFilePath = "C:\\Xfile.xml";
        int s = 0;
        String username = "";
        String fullname = "";
        String message = "";
        List getFiledata = null;

        public List readXML(int x) {
            System.out.println("x :" + x);
            try {
                File xmlFile = new File(xmlFilePath);
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document doc = documentBuilder.parse(xmlFile);
                doc.getDocumentElement().normalize();


                Node node = doc.getElementsByTagName("main").item(x);
                System.out.println("node :" + node);
                NodeList nodeList = node.getChildNodes();
                System.out.println("In Remove==================================" + nodeList.getLength());


                if (nodeList.getLength() > 0) {
                    getFiledata = new ArrayList();

                    Element element = (Element) doc.getElementsByTagName("SNo").item(x);
                    NodeList y = doc.getElementsByTagName("SNo");
                    element.getAttributeNode("" + x);
                    System.out.println("doc.getElementsByTagName('SrNo').item(x); " + element);

                    doc.removeChild(element);

                    for (int c = 0; c < y.getLength(); c++) {
    //                    Node node1 = nodeList.item(c);
    //                    node.getParentNode().removeChild(node1);
                        //doc.removeChild(y[c].);
                    }



                    //element.getParentNode().removeChild(element);

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

我想只删除与servlet中作为参数接收的数字对应的S_no节点。当我使用NodeList nodeList = node.getChildNodes();时 我得到空指针异常,否则没有任何反应。代码运行但不删除节点。 我也尝试使用for循环来获取属性值但没有成功。 我想我需要获得属性ID。或者是因为Attribute Id是String而我试图找到使用整数?

这是我得到的例外:

java.lang.NullPointerException
    at com.idbi.dp.Model.RemoveXmlElement.readXML(RemoveXmlElement.java:43)
    at com.idbi.dp.Controller.remove_servlet.doPost(remove_servlet.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)
java.lang.NullPointerException
    at com.idbi.dp.Model.RemoveXmlElement.readXML(RemoveXmlElement.java:43)
    at com.idbi.dp.Controller.remove_servlet.doPost(remove_servlet.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)

我被困在这里.. 请指出我的错误,如果可能的话,提供一个解决我的问题的提示。

忘记包含XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
- <main>
- <SrNo id="1">
  <username>XRG</username> 
  <fullname>Xavier</fullname> 
  <message>sfdgsdgdf<@@@/message> 
  </SrNo>
- <SrNo id="2">
  <username>ROB</username> 
  <fullname>Robert</fullname> 
  <message>adsadda$$$</message> 
  </SrNo>
  </main>

0 个答案:

没有答案