krishna prasad
krishna prasad

Reputation: 1

How to remove blank line created in the XML file after deleting a particular atrribute

I have a XML document, for example

<document>
    <attribute id = 1 />
    <attribute id = 2 />
    <attribute id = 3 />
    <attribute id = 4 />
    <attribute id = 5 />
    <attribute id = 6 />
</document>

I am using DOM parser to parse the XML file in my C++ code. I am deleting a particular attribute, for eg id = 3.

Using the API's from Xerces library, I delete the attribute, but i am getting a blank line in the place where i deleted the attribute.

Deleting is done as follows. I will remove the required attribute from given file and copy the remaining contents to a temp file but a blank line created.

<document>
    <attribute id = 1 />
    <attribute id = 2 />

    <attribute id = 4 />
    <attribute id = 5 />
    <attribute id = 6 />
</document>

I need the output as follows, the blank line should not be present in the file after deleting

<document>
    <attribute id = 1 />
    <attribute id = 2 />
    <attribute id = 4 />
    <attribute id = 5 />
    <attribute id = 6 />
</document>

Upvotes: 0

Views: 1019

Answers (1)

Shalindra Singh
Shalindra Singh

Reputation: 11

Use this method to achieve desired ...

private static void formatWSDL(Document doc, String path) {
    try {
        OutputFormat format = new OutputFormat(doc);
        format.setIndenting(true);
        XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(path)), format);
        serializer.serialize(doc.getDocumentElement());
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}

Upvotes: 1

Related Questions