Results 1 to 2 of 2
  1. #1
    sathyaxml is offline Member
    Join Date
    Sep 2016
    Posts
    1
    Rep Power
    0

    Lightbulb why xml file does not aligned properly after append the string in beginning and end o

    I have added the string in beginning and end of the XML File.but the alignment is not proper after getting the result.

    My XML File:

    <?xml version="1.0" encoding="UTF-8"?>
    <reRoot>
    <reNode> world</reNode>
    </reRoot>

    java program used to append the text at beginning and end of the file.

    Java Code:
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.xml.sax.SAXException;
    
    public class RWText {
    
    
        public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
    
            String directory = "C:\\Users\\n444479\\Desktop\\SA";
            int test = new File("C:\\Users\\n444479\\Desktop\\SA").listFiles().length;
            File[] files = new File(directory).listFiles();
    
            // Loop the file to run all the XML files
            for (int j = 0; j < test; j++) 
            {
    
                System.out.println(files[j]);
    
                //Append new line in existing file.
                FileInputStream fr = new FileInputStream(files[j]);
                DataInputStream dr = new DataInputStream(fr);
    
                String startStr = "{% from lxml import etree %}{% from StringIO import StringIO %}{% set tree = parse_xml(request_text) %}{% set namespaces = {'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',  'wbs': 'http://xml.ama.com/ws/2009/01/WBS_Session-2.0.xsd'}%}<soap:Envelope xmlns:xalan=\"http://xml.apache.org/xalan\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n  <soap:Header>\r\n    <Session>\r\n      <SessionId>{{tree.xpath('//soapenv:Header/wbs:Session/wbs:SessionId', namespaces=namespaces)[0].text}}</SessionId>\r\n      <SequenceNumber>{{int(tree.xpath('//soapenv:Header/wbs:Session/wbs:SequenceNumber', namespaces=namespaces)[0].text)+1}}</SequenceNumber>\r\n      <SecurityToken>{{tree.xpath('//soapenv:Header/wbs:Session/wbs:SecurityToken', namespaces=namespaces)[0].text}}</SecurityToken>\r\n    </Session>\r\n  </soap:Header>\r\n  <soap:Body>";
                //String startStr;
    
                while (dr.available() > 0) 
                {
                    startStr += dr.readLine();
                    System.out.println(startStr);
                }
                dr.close();
                fr.close();
    
                FileOutputStream writer = new FileOutputStream(files[j]);
                writer.write((new String()).getBytes());
                writer.close();
    
                PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(files[j], true)));
                out.println(startStr);
                out.close();
                try{
    
    
                FileWriter fstream  = new FileWriter(files[j], true);
                BufferedWriter fbw = new BufferedWriter(fstream);
                fbw.write("</soap:Body>\r\n</soap:Envelope>");
                fbw.close();
                }
                catch (Exception e) {
                    System.out.println("Error: " + e.getMessage());
                }
    
            }
    
        }
    
    
    }

    Once the program executed the string get appended properly but the XML File alignment changed.

    The output i am getting as below

    Java Code:
    {% from lxml import etree %}{% from StringIO import StringIO %}{% set tree = parse_xml(request_text) %}{% set namespaces = {'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',  'wbs': 'http://xml.ama.com/ws/2009/01/WBS_Session-2.0.xsd'}%}<soap:Envelope xmlns:xalan="http://xml.apache.org/xalan" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Header>
        <Session>
          <SessionId>{{tree.xpath('//soapenv:Header/wbs:Session/wbs:SessionId', namespaces=namespaces)[0].text}}</SessionId>
          <SequenceNumber>{{int(tree.xpath('//soapenv:Header/wbs:Session/wbs:SequenceNumber', namespaces=namespaces)[0].text)+1}}</SequenceNumber>
          <SecurityToken>{{tree.xpath('//soapenv:Header/wbs:Session/wbs:SecurityToken', namespaces=namespaces)[0].text}}</SecurityToken>
        </Session>
      </soap:Header>
      <soap:Body><?xml version="1.0" encoding="UTF-8"?><reRoot> <reNode> world</reNode></reRoot>
    </soap:Body>
    </soap:Envelope>

    kindly help me in solution for this..i am struggled lot..

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: why xml file does not aligned properly after append the string in beginning and e

    What do you expect the output to be?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How to append characters to string
    By maas in forum New To Java
    Replies: 4
    Last Post: 04-29-2016, 02:51 AM
  2. Replies: 2
    Last Post: 04-02-2012, 01:59 PM
  3. append character in string '\'???
    By jspexception in forum New To Java
    Replies: 3
    Last Post: 02-22-2012, 11:51 PM
  4. Reset a Scanner to the beginning of a file
    By Nosrettap in forum New To Java
    Replies: 2
    Last Post: 02-06-2011, 03:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •