How to customize the xml format in an xml file in java

Solution for How to customize the xml format in an xml file in java
is Given Below:

I am using the Jaxb Marshaller to convert java object to xml and also setting Marshaller.JAXB_FORMATTED_OUTPUT to true. This is giving me xml in below format:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
    <time>20210716124226</time>
    <details>
        <employee id="100" time="20210716124212">
            <name>asd</name>
            <empid>1</empid>           
            <jobCategory>Contract</jobCategory>            
        </employee>
        <employee id="102" time="20210716124212">
            <name>ative</name>
            <empid>2</empid>            
            <jobCategory>Permanent</jobCategory>                    
        </employee>
    </details>
</xml>

But the requirement is to generate the xml in below format with no indentation:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<time>20210716124226</time>
<details>
<employee id="100" time="20210716124212"><name>xyz</name><empid>1</empid><jobCategory>Contract</jobCategory></employee>
<employee id="102" time="20210716124212"><name>abc</name><empid>2</empid><jobCategory>Permanent</jobCategory></employee>
</details>
</xml>

How can i achieve this result using java?

It’s pretty exasperating, the number of times we have to tell people consuming XML to parse it properly, and the number of people who are stuck with the problem of generating XML for people who don’t parse it properly. But that’s life – people don’t realise the value of standards conformance until they are stuck with the task of fitting a round plug into a square hole. To do this, and adapter is needed — and it’s always better to put the adapter on the non-conformant side of the interface, which in this case is the consumer. But there’s no telling people.

In this instance, writing the adapter isn’t difficult. Both the Xalan and Saxon XML serializers have an option to indent with indent-spaces=0, which should give the format you want, so you just have to put your XML through an identity transformation with this option set.