使用SAX Parser编辑xml内容

时间:2016-06-23 06:38:43

标签: java xml

当我使用main方法在单个java文件中运行它时,代码工作正常,但我的问题是当我将它放在我的spring mvc项目中然后它给我在转换时找不到错误文件。我无法理解究竟是什么问题,相同的代码在一个单独的类中运行但在我的项目中没有 请一些身体帮助我。感谢...

 File xmlFile = new File("d:\\file1.xml");
     Reader fileReader = new FileReader(xmlFile);
        BufferedReader bufReader = new BufferedReader(fileReader);

        StringBuilder sb = new StringBuilder();
        String line = bufReader.readLine();
        while( line != null){
            sb.append(line).append("\n");
            line = bufReader.readLine();
        }
        String xml = sb.toString();

    //String xml = "<users><user><name>user1</name></user></users>";
    XMLReader xr = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
        private String tagName = "";

        @Override
        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
            tagName = qName;
            super.startElement(uri, localName, qName, atts);
        }

        public void endElement(String uri, String localName, String qName) throws SAXException {
            tagName = "";
            super.endElement(uri, localName, qName);
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (tagName.equals("author")) {
                ch = uploadXmlBean.getAuthor().toCharArray();
                start = 0;
                length = ch.length;
            }
            if (tagName.equals("currency")) {
                ch = uploadXmlBean.getCurrency().toCharArray();
                start = 0;
                length = ch.length;
            }

            if (tagName.equals("name")) {
                ch = uploadXmlBean.getName().toCharArray();
                start = 0;
                length = ch.length;
            }

            if (tagName.equals("logicalHeading")) {
                ch = uploadXmlBean.getLogicalHeading().toCharArray();
                start = 0;
                length = ch.length;
            }

            super.characters(ch, start, length);
        }
    };
    Source src = new SAXSource(xr, new InputSource(new StringReader(xml)));

    Result res = new StreamResult(xmlFile);
    TransformerFactory.newInstance().newTransformer().transform(src, res);

在我的项目类中实现的代码是

@RequestMapping(value = "/xmlEditor", method = RequestMethod.POST)
public String xmlEditor(@ModelAttribute("UploadXmlBean") final UploadXmlBean uploadXmlBean) throws Exception {
    System.out.println("xmlEditor");
    employeeService.addXml(uploadXmlBean);
    //xmlAppService.addXml(uploadXmlBean);


    ///////////////

     File xmlFile = new File("d:\\file1.xml");
     Reader fileReader = new FileReader(xmlFile);
        BufferedReader bufReader = new BufferedReader(fileReader);

        StringBuilder sb = new StringBuilder();
        String line = bufReader.readLine();
        while( line != null){
            sb.append(line).append("\n");
            line = bufReader.readLine();
        }
        String xml = sb.toString();

    //String xml = "<users><user><name>user1</name></user></users>";
    XMLReader xr = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
        private String tagName = "";

        @Override
        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
            tagName = qName;
            super.startElement(uri, localName, qName, atts);
        }

        public void endElement(String uri, String localName, String qName) throws SAXException {
            tagName = "";
            super.endElement(uri, localName, qName);
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (tagName.equals("author")) {
                ch = uploadXmlBean.getAuthor().toCharArray();
                start = 0;
                length = ch.length;
            }
            if (tagName.equals("currency")) {
                ch = uploadXmlBean.getCurrency().toCharArray();
                start = 0;
                length = ch.length;
            }

            if (tagName.equals("name")) {
                ch = uploadXmlBean.getName().toCharArray();
                start = 0;
                length = ch.length;
            }

            if (tagName.equals("logicalHeading")) {
                ch = uploadXmlBean.getLogicalHeading().toCharArray();
                start = 0;
                length = ch.length;
            }

            super.characters(ch, start, length);
        }
    };
    Source src = new SAXSource(xr, new InputSource(new StringReader(xml)));

    Result res = new StreamResult(xmlFile);
    TransformerFactory.newInstance().newTransformer().transform(src, res);
        return "index";
}

代码错误抛出

java.io.FileNotFoundException: file:\d:\file1.xml (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:235)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:278)
at com.dineshonjava.controller.EmployeeController.xmlEditor(EmployeeController.java:158)
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.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

请尝试以下更改并尝试一次:

File xmlFile = new File("d:\\file1.xml").getAbsoluteFile()