2

I have a Problem that when I'm removing a child-node from an xml there will be an empty line in the xml. The node is removed correctly but i don't want an empty line, and I have no idea where that empty line comes from as I'm not writing the xml, but Javas Transformer is doing it for me:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t;
DocumentBuilder db;
db = dbf.newDocumentBuilder();
Document doc = db.parse(myXml);

//get the node I want to remove
Node n = getMyNodeToRemove(doc);
n.getParentNode().removeChild(n);

//write the xml
t = tf.newTransformer();
DOMSource src = new DOMSource(doc);
StreamResult result = new StreamResult(xml);
t.transform(src, result);

The xml i want to edit is in fact very simple and looks like this:

<parent>
  <childnode 1>
  <childnode i want to remove>
  <childnode 2>
  <childnode 3>
</parent>

after removing the child it looks like this:

<parent>
  <childnode 1>

  <childnode 2>
  <childnode 3>
</parent>

But what I want is this:

<parent>
  <childnode 1>
  <childnode 2>
  <childnode 3>
</parent>

Is there some function to call on the parent-node i'm missing ? or is there a explicit pretty-print function for xmls ?

AntiqueZamba
  • 155
  • 2
  • 4
  • 11
  • 2
    sample of your xml and the node you are trying to remove will be helpful – shyam Oct 16 '12 at 08:51
  • *"..i don't want an empty line"* is a programmer's futzing. – Andrew Thompson Oct 16 '12 at 09:03
  • 2
    Have a look at http://stackoverflow.com/questions/12669686/java-how-to-remove-extra-empty-lines-from-xml-file and/or http://stackoverflow.com/questions/978810/how-to-strip-whitespace-only-text-nodes-from-a-dom-before-serialization and/or http://techxplorer.com/2010/05/24/deleting-nodes-and-empty-lines-in-xml-using-java/ – MadProgrammer Oct 16 '12 at 09:22
  • Andrew: Ok I wrote it wrong: I dont care about the empty line, but somebody else does for whatever reason... – AntiqueZamba Oct 16 '12 at 09:25

1 Answers1

0

MadProgrammer: Thanks that helped. I just remove the empty textnodes now and it works - But i don't understand why they are created in the first place seems like bad parsing to me.

AntiqueZamba
  • 155
  • 2
  • 4
  • 11