-- 作者:yggycxb
-- 发布时间:6/25/2007 11:34:00 AM
-- 一个XML解析问题
我是新手,碰到这么一个问题,请帮我下: links.xml文件如下: <links> <link id="1"> <text>JSP Insider</text> </link> </links> 想把text 内容改下 程序如下: SAXReader reader = new SAXReader(); doc = reader.read(new File("links.xml")); //修改ID为"1"的link节点 设置text 为 it's a modify List list =doc.selectNodes("//link[@id=4]"); Iterator it = list.iterator(); while(it.hasNext()){ Element el = (Element)it.next(); el.elementByID("text").setText("it's a modify"); } 以上用的是DOM4J解析的 运行报错: java.lang.NullPointerException at com.qware.sjjh.tool.dom4j.DeployFileLoaderSample.editXml(DeployFileLoaderSample.java:117) at com.qware.sjjh.tool.dom4j.DeployFileLoaderSampleTest.testEditXml(DeployFileLoaderSampleTest.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71) at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) 什么原因?正确程序要怎么写呢?
|