简体   繁体   中英

reach xml from servlet

Trying to write from a servlet to a local xml file located within the project. When the same code is running from another java class, no error occurs but when the code is running form the servlet, I get the following error:

java.lang.ClassNotFoundException: org.jdom2.JDOMException

The part of the code that is pasted into the servlet at the end of the doGet method of the servletis as follow:

            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();
          }

my imports are as follow:

                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;

Thank you for your help

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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