简体   繁体   中英

Java XML parsing using Transformer.transform, works fine on its own, does not work when using Maven

The only Question i found that seemed similiar but sadly this didnt help

Hi so ive been stuck on this Problem for a few hours now. I am trying to write new Data into an preexisting XML file (/creating it when it doesnt exist yet) for Data Collection for a small game i am programming for uni. Ive created a DataRecorder class for this that works fine when testing with a main in that class

private Transformer transformer;
    private Document documentWriter;
    private DocumentBuilder documentBuilder;
    private Element eventList;
    private static final String RECORDED_DATA = "Game/src/main/resources/recordedData.xml";

public DataRecorder() {
        recordedData = new ArrayList<Data>();
        this.username = System.getProperty("user.name");
        this.newData = false;

        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            this.transformer = transformerFactory.newTransformer();
        } catch (Exception e) {
            System.out.println("Error when creating DataRecorder: Transformer");
        }
        File tempFile = new File(RECORDED_DATA);
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            this.documentBuilder = dbf.newDocumentBuilder();
            if (!tempFile.exists()) {
                //Creates new Document if it doesnt exist
                this.documentWriter = this.documentBuilder.newDocument();
            } else {
                this.documentWriter = this.documentBuilder.parse(RECORDED_DATA);
            }
        } catch (Exception e) {
            System.out.println("Error when creating DataRecorder: Document Writer");
        }

        Element root;
        if (!tempFile.exists()) {
            //Creates new Document if it doesnt exist
            root = this.documentWriter.createElement("recordedData");
            this.documentWriter.appendChild(root);
        } else {
            root = this.documentWriter.getDocumentElement();
        }

        Element player = this.documentWriter.createElement("Player");
        player.setAttribute("name", this.username);
        root.appendChild(player);


        this.eventList = this.documentWriter.createElement("EventList");
        this.eventList.setAttribute("date", Date.from(java.time.ZonedDateTime.now().toInstant()).toString());
        this.eventList.setAttribute("timezone", java.time.ZonedDateTime.now().getZone().toString());
        player.appendChild(this.eventList);

        DOMSource source = new DOMSource(this.documentWriter);
        StreamResult result = new StreamResult(System.getProperty("user.dir")  + "/src/main/resources/recordedData.xml");

        try {
            this.transformer.transform(source, result);
        } catch (Exception e) {
            System.out.println("Error when creating DataRecorder: Could not write System Name and Date");
            System.out.println(e);
        }
    }

This creates an XML file that looks like this

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<recordedData>
    <Player name="Name">
        <EventList date="Wed Jun 24 21:34:46 CEST 2020" timezone="Europe/Berlin"/>
    </Player>
</recordedData>

The problem is that when i execute this Class with its own Main it just adds some new Lines to (which is what i want it to do) while when executing with maven it completly rewrites the xml file.

The path you have is your resources folder, the resources folder is a maven convenience folder, that folder doesn't exist when you run maven build. The contents of that folder are copied to target/classes folder when maven build runs.

Try using a folder that is outside your source code repository

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