2013-09-26 23 views
1

我試圖從小時以來解決domparser的問題。我編寫了一個簡單的應用程序來加載一個xml文件,修改它們並將修改寫回去。它工作,直到我切換到Maven項目轉換項目。我不確定那是否是問題所在。我無法理解的例外Java-DOM-Parser:Transformer.transform的例外

這裏我的代碼部分的DOM解析器:

import java.io.File; 
import java.io.IOException; 

import javafx.beans.property.BooleanProperty; 
import javafx.beans.property.SimpleBooleanProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerConfigurationException; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 

import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import **.***.Notification.*; 

public class TomcatVM implements ViewModel{ 


Document tomcatDoc; 
Node shutdownPortNode = null; 
Node portNode = null; 

private StringProperty port = new SimpleStringProperty(); 
private StringProperty shutdownPort = new SimpleStringProperty(); 
private BooleanProperty tomcatService = new SimpleBooleanProperty(); 
private String fullTomcatPropPath = new String(); 
private StringProperty path = new SimpleStringProperty(); 


public void init() { 

    NotificationCenter.getDefaultNotificationCenter().addObserverForName(Notification.LOAD_NOTIFICATION.toString(), new NotificationObserver() { 

     @Override 
     public void receivedNotification(final String arg0, final Object... arg1) { 
      // TODO Auto-generated method stub 
      File tomcatXmlFile = new File(getFullTomcatPropPath()); 
      if(tomcatXmlFile != null){ 

       try{ 

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
        System.out.println("got: "+getFullTomcatPropPath()); 
        System.out.println("path: "+tomcatXmlFile.getPath()); 
        tomcatDoc = dBuilder.parse(tomcatXmlFile); 
        tomcatDoc.getDocumentElement().normalize(); 

        if(tomcatDoc != null){ 

         shutdownPortNode = tomcatDoc.getElementsByTagName("Server").item(0).getAttributes().getNamedItem("port"); 
         portNode = tomcatDoc.getElementsByTagName("Connector").item(0).getAttributes().getNamedItem("port"); 

         //     tomcatShutdownPortTextField.setText(shutdownPortNode.getTextContent()); 
         setPort(portNode.getTextContent()); 
         setShutdownPort(shutdownPortNode.getTextContent()); 

        } 

       }catch(Exception e){ 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 


    NotificationCenter.getDefaultNotificationCenter().addObserverForName(Notification.SAVE_NOTIFICATION.toString(), new NotificationObserver() { 

     /* 
     * write changes from the interface to the doc file for 
     * the tomcat-xml file and overwrite the old tomcat-xml 
     */ 

     @Override 
     public void receivedNotification(final String arg0, final Object... arg1) { 

      portNode.setTextContent(getPort().toString()); 
      shutdownPortNode.setTextContent(getShutdownPort().toString()); 

      TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
      Transformer transformer; 

      try { 
       transformer = transformerFactory.newTransformer(); 
       DOMSource source = new DOMSource(tomcatDoc); 
       StreamResult result = new StreamResult(new File(getFullTomcatPropPath())); 
       enter code here // ******here is the exception: ******* 
       transformer.transform(source, result); 
      } catch (TransformerConfigurationException e) { 
       e.printStackTrace(); 
      } catch (TransformerException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 


} 



public String getFullTomcatPropPath() { 
    return fullTomcatPropPath; 
} 

public void setFullTomcatPropPath(final String fullTomcatPropPath) { 
    this.fullTomcatPropPath = fullTomcatPropPath; 
    } 

} 

這是我得到的異常:

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Node.fireEvent(Unknown Source) 
    at javafx.scene.control.Button.fire(Unknown Source) 
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source) 
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source) 
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.process(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.process(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source) 
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.notifyMouse(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.reflect.InvocationTargetException 
    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 sun.reflect.misc.Trampoline.invoke(Unknown Source) 
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source) 
    ... 53 more 
Caused by: java.lang.AbstractMethodError: org.apache.xerces.dom.DeferredDocumentImpl.getXmlStandalone()Z 
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(Unknown Source) 
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source) 
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source) 
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source) 
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source) 
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source) 
    at **.*****.***.***.database.DatabaseVM$3.receivedNotification(DatabaseVM.java:238) 

我沒加任何依賴到我的pom.xml dom解析器。 SAME CODE在另一個項目中運行,唯一的區別是,我從中得到例外的項目是一個maven項目。如果我註釋掉該行,則會在另一個保存其他xml文件的類上得到異常。所以我認爲是不是與tomcat的問題? 有沒有人有想法? :/

在此先感謝!

回答

2

在沒有maven的獨立項目中是否包含任何罐子/進口?如果相同的確切代碼在那裏工作,但不是maven它會導致我相信你有依賴性錯誤。

嘗試加入這個依賴於你的pom.xml:xercesImpl.jar

<dependency> 
    <groupId>xerces</groupId> 
    <artifactId>xercesImpl</artifactId> 
    <version>2.11.0</version> 
</dependency> 
+0

該死的工作:-D!我真的認爲我已經嘗試了所有可能的依賴關係。你節省了我的一天......第二天;-)非常感謝! –

+0

很高興聽到它的作品! – pinkdevelops

+0

謝謝@pinkdevelops。在我的情況下,這是由於依賴於使用xercesImpl依賴項構建的外部jar。在子項目中添加這個依賴關係 - 修復它! –