Home > Back-end >  remove carriage return in xml & split xml version from rest of xml
remove carriage return in xml & split xml version from rest of xml

Time:10-01

I am using the below code to convert my DOM Node to XML file. I am seeing two issues with my file

  1. The first line should actually be two lines with the version on line 1 and <tb:tradeBuilder on line 2. But and <tb:tradeBuilder are on the same line how can i enforce they show up on two lines?

    <?xml version=\"1.0\" encoding=\"UTF-8\"?><tb:tradeBuilder xmlns:tb=\"urn:or-tradeBuilder\" datatype=\"tradebuilder\">\r
     <tb:tradeField>\r
    
  2. How do I remove the carriage return \r from every line?

Java code to print xml to file

     public static void writeNode(final Node node, final Writer out) {
       try {       
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(node);
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            StreamResult result = new StreamResult(out);
            transformer.transform(source, result);
        }catch (TransformerConfigurationException tce ){

        }catch(TransformerException te){

        }

CodePudding user response:

Solution is to set below property to either "yes" or "no"

transformer.setOutputProperty(OutputKeys.STANDALONE, "no");

Complete Code:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.OutputStream;


import org.w3c.dom.*;

public class SO_XML {
private static Document xmlDoc;

public static void main(String[] args) throws ParserConfigurationException {

  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  docFactory.setIgnoringElementContentWhitespace(true);

  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  Document xmlDoc = docBuilder.newDocument();
  xmlDoc.setXmlStandalone(false);

  Element rootElement = xmlDoc.createElement("companies");
  xmlDoc.appendChild(rootElement);
  Element item = xmlDoc.createElement("company");
  item.setAttribute("name", "code4passion");
  rootElement.appendChild(item);
  writeNode(rootElement, System.out);
}


public static void writeNode(final Node node, final OutputStream out) {
  try {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    DOMSource source = new DOMSource(node);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.STANDALONE, "no");

    StreamResult result = new StreamResult(out);
    transformer.transform(source, result);
  } catch (TransformerConfigurationException tce) {

  } catch (TransformerException te) {

  }
 }
}
  • Related