0

In java I have read an xml file, add an attribue and value and write and xml back to a file.

The problem I am running into is that the doc transformer is re-arranging my xml.

Example:

Original

   <Valve lassName="AccessLogValveAdapter" directory="access_logs"  prefix="localhost." suffix=".log" pattern='%v %h %t "%r" %s %B %T "%{User-Agent}i"'/>

Modified

   <Valve className="AccessLogValveAdapter" directory="access_logs" pattern="%v %h %t "%r" %s %B %T "%{User-Agent}i"" prefix="localhost." suffix=".log"/>

Here is my code:

    boolean returnValue = false;
    String methodName = "docTransFormer() ";
    File file = new File(filePath);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try
    {
        transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(file.getPath());
        transformer.transform(source, result);
        returnValue = true;
    }
    catch (TransformerConfigurationException e)
    {
        logger.info(EXCEPTIONCAUGHT + methodName + e.getMessage());
    }
    catch (TransformerException e)
    {
        logger.info(EXCEPTIONCAUGHT + methodName + e.getMessage());
    }
    return returnValue;
}

Any ideas how i can keep this in the original format?

user1158745
  • 2,402
  • 9
  • 41
  • 60
  • Why does it matter in which order the attributes are listed? Also, please add the original full XML code and the resulting new XML code, because there are some problems with the quotes of the attribute values. – Progman Feb 25 '19 at 20:02
  • It's kinda hard to compare the files after I have modified them. Any way around this? – user1158745 Feb 28 '19 at 19:16

0 Answers0