1

The following dom manipulation should result in a different output, but it doesn't. The println indicates it has worked, but the root node.toString() or using a Transformer fails with equal results.

  private void markNewLinesInTextNodes(Node node) {
    for(int i = 0 ; i < node.getChildNodes().getLength() ; i++) {
      Node child = node.getChildNodes().item(i);
      markNewLinesInTextNodes(child);
    }
    if (node instanceof Element) {
      Element el = (Element) node;
      if (el.getTagName().toLowerCase().equals("text")) {
        el.setTextContent(el.getTextContent().replaceAll("\n", "_LINEBREAK_"));
        System.out.println(el.getTextContent());
      }
    }
  }

1 Answer 1

0

this works:

  if (el.getTagName().toLowerCase().equals("text")) {
    el.setTextContent(el.getTextContent().replaceAll("\n", "_LINEBREAK_"));
    System.out.println(el.getTextContent());

Then :

  • are you sure of the selection ?

  • how do you output it ?

try that:

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(document),
      new StreamResult(buffer));
String result= buffer.toString();
System.out.println(result);
1
  • Thanks for verifying for me, so I'll mark this as correct given the information I provided. The real reason was because of the way the jcabi api returns the node, but you couldn't have known that. Thanks again. Dec 17, 2015 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.