Empty lines appear after the element is removed from the xml file

miniWorld

Some elements have been added to the xml file through the add method. But why does the xml file leave a blank line after deleting an element, and how to get rid of it? The main function is:

@Test
    public void test() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        OperateXml operateXml = new OperateXml();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ClassPathResource("chunkInfo/chunk.xml").getFile());
// operateXml.add(document);
            operateXml.deleteNodeById(document,"id_5");
            operateXml.saveDocument(document, new ClassPathResource("chunkInfo/chunk.xml").getFile());
} catch (Exception e) {
            System.out.println(e);
        }
}

The save function is:

public static void saveDocument(Document document, File xmlFile) {
        TransformerFactory tff = TransformerFactory.newInstance();
        try {
            Transformer tf = tff.newTransformer();
            tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "chunk.dtd");
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            tf.transform(new DOMSource(document), new StreamResult(xmlFile));
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

Before deleting the element, the xml file is:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE save SYSTEM "chunk.dtd">
<save>
    <chunk id="id_1">
    <name>test_6</name>
    <type>log</type>
    <item>2,3,4,5</item>
  </chunk>
    <chunk id="id_3">
    <name>test_6</name>
    <type>log</type>
    <item>2,3,4,5</item>
  </chunk>
    <chunk id="id_4">
    <name>test_6</name>
    <type>log</type>
    <item>2,3,4,5</item>
  </chunk>
<chunk id="id_5">
    <name>test_7</name>
    <type>log</type>
    <item>2,3,4,5</item>
  </chunk>
</save>

After deleting the element (id="id_5"), the xml file is:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE save SYSTEM "chunk.dtd">
<save>
    <chunk id="id_1">
    <name>test_6</name>
    <type>log</type>
    <item>2,3,4,5</item>
  </chunk>
    <chunk id="id_3">
    <name>test_6</name>
    <type>log</type>
    <item>2,3,4,5</item>
  </chunk>
    <chunk id="id_4">
    <name>test_6</name>
    <type>log</type>
    <item>2,3,4,5</item>
  </chunk>

</save>
xtratic

The reason is likely because there are invisible [text] nodes between your elements (in the example below '\n' is a newline and '\t' is a tab indent):

<save>[text '\n
\t']<chunk id="id_1">[text '\n
\t\t']<name>test_6</name>[text '\n
\t\t']<type>log</type>[text '\n
\t\t']<item>2,3,4,5</item>[text '\n
\t\t']</chunk>[text \n
\t']<chunk id="id_3">[text '\n
\t\t']<name>test_6</name>[text '\n
\t\t']<type>log</type>[text '\n
\t\t']<item>2,3,4,5</item>[text '\n
\t']</chunk>[text '\n
\t']<chunk id="id_4">[text '\n
\t\t']<name>test_6</name>[text '\n
\t\t']<type>log</type>[text '\n
\t\t']<item>2,3,4,5</item>[text '\n
\t']</chunk>[text '\n
\t']<chunk id="id_5">[text '\n
\t\t']<name>test_7</name>[text '\n
\t\t']<type>log</type>[text '\n
\t\t']<item>2,3,4,5</item>[text '\n
\t']</chunk>[text '\n']
</save>

When you remove everything under <chunk id="id_5"> it leaves the [text] nodes around it.

<save>[text '\n
\t']<chunk id="id_1">[text '\n
\t\t']<name>test_6</name>[text '\n
\t\t']<type>log</type>[text '\n
\t\t']<item>2,3,4,5</item>[text '\n
\t\t']</chunk>[text \n
\t']<chunk id="id_3">[text '\n
\t\t']<name>test_6</name>[text '\n
\t\t']<type>log</type>[text '\n
\t\t']<item>2,3,4,5</item>[text '\n
\t']</chunk>[text '\n
\t']<chunk id="id_4">[text '\n
\t\t']<name>test_6</name>[text '\n
\t\t']<type>log</type>[text '\n
\t\t']<item>2,3,4,5</item>[text '\n
\t']</chunk>[text '\n
\t'][..where chunk used to be..][text '\n']
</save>

One option to resolve this would be to re-format the document.

Another is to get the reference to the element you want to delete, check its preceding sibling node, and if it's a blank text node then remove it as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove an empty element from JSON file with Jackson

How to remove the lines which appear on file B from another file A?

How to remove extra empty lines from XML file?

Touchmove event stops triggering after any element is removed from dom

Restore Node/Element after it has been modified/removed from DOM

MutationObserver behavior after the element was removed

How to delete empty lines from a .txt file

Detect empty lines in a file

Reading lines from a file after writing to it

Iterating the bytes of a File is empty after iterating the lines of the same file

Angular: ngif animation jumps! How to animate ngif element after previous element is removed from DOM?

Detect empty lines while reading from file

strip a file from empty lines

How to remove empty lines/rows from file?

Spaces appear after file_get_contents from HTML file

Removed empty array element to json_encode after

Remove empty lines from a text file

Extract one element from lines of a text file

remove "empty" lines from a text file shell script

Why is my XML file empty after generating with XML::LibXML

SSIS - XML file empty after rename task

Is there a fast way in Python to read in data from a file, separated by empty lines?

selecting random line from text file after deleting empty lines

Remove comments and empty lines from a file using bash/shell command

Updating XML file with TransformerFactory adds empty lines between nodes

How to remove the lines which appear on file 1 from another file 2 KEEPING empty lines?

Remove certain lines (with ---- and empty lines) from txt file using readLines() or read_lines()

Extra empty lines appear when saving the XML file

Displaying the empty lines from loaded data in a P element

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive