繁体   English   中英

从servlet到达xml

[英]reach xml from servlet

尝试从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;

谢谢您的帮助

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

暂无
暂无

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

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