Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XML is written in 1 continuous line when viewed with notepad (newline ?)

843834Sep 19 2006 — edited Sep 20 2006
Hello all,
I have searched this forum for a solution to my problem...apparently this is a common problem but I am unable to come up with a solution.

I have an xml document that has the following:
<ServiceListing>
<ServiceName>dufus</ServiceName>
<ServiceHost>1.1.1.1</ServiceHost>
<ServicePort>12343</ServicePort>
</ServiceListing>

<ServiceListing>
<ServiceName>duffer</ServiceName>
<ServiceHost>1.1.1.1</ServiceHost>
<ServicePort>123/n</ServicePort>
</ServiceListing>
Now when I add a new "record" it is written in one continuous line like so:
<ServiceListing>
<ServiceName>dufus</ServiceName>
<ServiceHost>1.1.1.1</ServiceHost>
<ServicePort>12343</ServicePort>
</ServiceListing>
<ServiceListing>
<ServiceName>duffer</ServiceName>
<ServiceHost>1.1.1.1</ServiceHost>
<ServicePort>123/n</ServicePort>
</ServiceListing>
<ServiceListing><ServiceName>fgh</ServiceName><ServiceHost>2.2.2.2</ServiceHost><ServicePort>2211/n</ServicePort></ServiceListing>
My code that generates this is:
        Document document = this.getDocument();
        Text newLineText = document.createTextNode("/n");
        //CREATE THE ROOT ELEMENT FOR THIS NODE
        Element serviceListingRoot = document.createElement("ServiceListing");
       
        
        //CREATE THE FIRST CHILD ELEMENT SERVICENAME
        Element serviceNameElement = document.createElement("ServiceName");
        serviceListingRoot.appendChild(serviceNameElement);
        Text serviceNameText = document.createTextNode(serviceName);
        serviceNameElement.appendChild(serviceNameText);
        serviceNameElement.appendChild(newLineText);
        
        //CREATE THE SECOND CHILD ELEMENT HOSTNAME
        Element hostNameElement = document.createElement("ServiceHost");
        //APPEND THE ELEMENT TO THE ROOT NODE
        serviceListingRoot.appendChild(hostNameElement);
        Text hostNameText = document.createTextNode(serviceHost);
        //APPEND THE TEXT NODE TO THE ELEMENT NODE
        hostNameElement.appendChild(hostNameText);
        hostNameElement.appendChild(newLineText);
        
        //CREATE THE THIRD CHILD ELEMENT PORTNAME
        Element portNameElement = document.createElement("ServicePort");
        //APPEND THE ELEMENT TO THE ROOT NODE
        serviceListingRoot.appendChild(portNameElement);
        Text portNameText  = document.createTextNode(servicePort);
        //APPEND THE TEXT NODE TO THE ELEMENT NODE
        portNameElement.appendChild(portNameText);
        portNameElement.appendChild(newLineText);
        
        Element root = document.getDocumentElement();
        Node lastNode = root.appendChild(serviceListingRoot);
        this.serializeDOM(document); //THIS SEEMS TO IGNORE THE NEWLINETEXT(ZERO LENGHT STRING INSERT WHEN VIEWWED WITH WORDPAD

     }
Now for the following line I have tried the following:

Text newLineText = document.createTextNode("/n");
Text newLineText = document.createTextNode("");
Text newLineText = document.createTextNode("& # 1 0 ;");

None work.
SHould I be creating a new TextNode?
Is there a better way to insert a newline in my xml doc?
Perhaps it is in my serializeDOM method?

Here it is:
   public void serializeDOM(Document document) throws IOException
   {
       OutputFormat of = new OutputFormat(document);
       FileWriter writer = new FileWriter(GSM_FILE);
       XMLSerializer serializer = new XMLSerializer();
       of.setPreserveSpace(true);
       of.setLineSeparator(System.getProperty("line.separator"));
       of.setLineWidth(0);
       serializer.setOutputCharStream(writer);
       serializer.asDOMSerializer();
       serializer.serialize(document);
       writer.flush();
       writer.close();
       of = null;
       serializer = null;
   }
Thanks for any advice!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 18 2006
Added on Sep 19 2006
11 comments
520 views