Java-DOM-Parser: Exception on Transformer.transform

徘徊边缘 提交于 2021-02-19 08:01:41

问题


I'm trying to solve a problem with the domparser since hour. I wrote a simple application to load a xml-file, modify them and write the changes back. It works until I switched converted the project to a maven-project. I'm not sure if thats the problem. I can't understand the exception

Here my Code-Part for the DOM-parser:

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

}

And this is the Exception I got:

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)

I didn't add any dependencies to my pom.xml for the dom-parser. The SAME CODE is running in an other project, the only difference is, that the project, from which I got the exception is a maven-project. If I comment out the line, I got the exception on an other class which saves an other xml-file. So I think is is not a problem with tomcat? Has anyone an idea? :/

Thanks in advance!


回答1:


In your standalone project without maven did you include any jars/imports? If the same exact code is working there, but then not in maven it would lead me to believe you have a dependency error.

Try adding this dependency to your pom.xml: xercesImpl.jar

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


来源:https://stackoverflow.com/questions/19026951/java-dom-parser-exception-on-transformer-transform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!