从servlet到达xml

时间:2012-08-15 17:30:12

标签: java xml servlets saxparser classnotfoundexception

尝试从servlet写入位于项目中的本地xml文件。 当从另一个java类运行相同的代码时,不会发生错误,但是当代码从servlet运行时,我收到以下错误:

java.lang.ClassNotFoundException:org.jdom2.JDOMException

在servlet的doGet方法末尾粘贴到servlet的代码部分如下:

            try {

                SAXBuilder builder = new SAXBuilder();
                File xmlFile = new File("C:\\eclipse\\workspace1\\BeamII\\WebContent\\Data.xml");

                Document doc = (Document)builder.build(xmlFile);

                Element Users = doc.getRootElement();
                List<Element> myUsers = Users.getChildren();

                Element beamUser = myUsers.get(0);

                Element Data= beamUser.getChildren().get(2);

                Element data1= new Element ("data1");

                Data.addContent(data1);


                XMLOutputter xmlOutput = new XMLOutputter();


                xmlOutput.setFormat(Format.getPrettyFormat());
                xmlOutput.output(doc, new FileWriter("C:\\eclipse\\workspace1\\BeamII\\WebContent\\Data.xml"));

                // xmlOutput.output(doc, System.out);

                System.out.println("done!");
          } catch (IOException io) {
                io.printStackTrace();
          } catch (JDOMException e) {
                e.printStackTrace();
          }

我的导入如下:

                import java.io.File;
                import java.io.FileWriter;
                import java.io.IOException;
                import java.util.ArrayList;
                import java.util.List;

                import org.jdom2.input.SAXBuilder;
                import org.jdom2.output.Format;
                import org.jdom2.output.XMLOutputter;
                import org.jdom2.Document;
                import org.jdom2.Element;
                import org.jdom2.JDOMException;

                import javax.servlet.RequestDispatcher;
                import javax.servlet.ServletException;
                import javax.servlet.http.HttpServlet;
                import javax.servlet.http.HttpServletRequest;
                import javax.servlet.http.HttpServletResponse;

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

检查jdom-2.0.2.jar中是否有C:\eclipse\workspace1\BeamII\WebContent\WEB-INF\lib(或您正在使用的任何版本)。