cancel
Showing results for 
Search instead for 
Did you mean: 

Carriage Return in the end of each line of XML file.(Code Required)

Former Member
0 Kudos

Hi All,

I have a graphical mapping which does all the transformation.

I just need a java mapping that will be used along with the graphical mapping which will only append a carriage return to the end of each closeing tag.

ANy one have this piece of code written, then please share this with me.

Logic required:

Appending a carriage return to the end of each closeing tag..

this will be used as a java mapping.

Many thanks,

JGD.

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi All,

Source Text file when generated to a XML file generate one carriage return "enter" in the first line only and not at the closeing tag of each line.

how can i get a carriage return at the closeing tag of each line.

Aashish,

Thanks a lot for the help but looking for some workaround at this high time.

thanks all,

point will be assigned to you guys by any means.

JGD.

Former Member
0 Kudos

Thanks Aashish,

There is only one concern,

How can we use an UDF function when generating an XML file and that to where we have to put the carriage return at after the ending tag.

Source = text file..

Target = XML file.

target xml FIle XSD is provided by Client.

Not all the fileds are mapped.

I really didn't get you yaar..

If this would be possible then it wouldbe a great help for me.

Former Member
0 Kudos

i think u need to generate the XML file with the end separator at the end of every tag... may b try a xsl mapping and generate the xml file and in ur target strucuture have only on root element. y this way u generate the target XML file from ur maping

aashish_sinha
Active Contributor
0 Kudos

Hi,

>> Source = text file..

Target = XML file.

Suppose this is source structure and target structure you want

Source :

Java|http://www.rgagnon/javahowto.htm

PowerBuilder|http://www.rgagnon/pbhowto.htm

Javascript|http://www.rgagnon/jshowto.htm

VBScript|http://www.rgagnon/vbshowto.htm

Want to convert to XML format

<?xml version="1.0" encoding="ISO-8859-1"?>

<HOWTOS>

<TOPIC>

<TITLE>Java</TITLE>

<URL>http://www.rgagnon/javahowto.htm</URL>

</TOPIC>

<TOPIC>

<TITLE>PowerBuilder</TITLE>

<URL>http://www.rgagnon/pbhowto.htm</URL>

</TOPIC>

<TOPIC>

<TITLE>Javascript</TITLE>

<URL>http://www.rgagnon/jshowto.htm</URL>

</TOPIC>

<TOPIC>

<TITLE>VBScript</TITLE>

<URL>http://www.rgagnon/vbshowto.htm</URL>

</TOPIC>

</HOWTOS>

You need to use this java code. Write it in some editor make the jar file and import it. and use it

import java.io.*;

// SAX classes.

import org.xml.sax.*;

import org.xml.sax.helpers.*;

import javax.xml.parsers.*;

import javax.xml.transform.*;

import javax.xml.transform.stream.*;

import javax.xml.transform.sax.*;

public class ToXML {

BufferedReader in;

StreamResult out;

TransformerHandler th;

AttributesImpl atts;

public static void main (String args[]) {

new ToXML().doit();

}

public void doit () {

try{

in = new BufferedReader(new FileReader("data.txt"));

out = new StreamResult("data.xml");

initXML();

String str;

while ((str = in.readLine()) != null) {

process(str);

}

in.close();

closeXML();

}

catch (Exception e) { e.printStackTrace(); }

}

public void initXML() throws ParserConfigurationException,

TransformerConfigurationException, SAXException {

// JAXP + SAX

SAXTransformerFactory tf =

(SAXTransformerFactory) SAXTransformerFactory.newInstance();

th = tf.newTransformerHandler();

Transformer serializer = th.getTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");

// pretty XML output

serializer.setOutputProperty

("{http://xml.apache.org/xslt}indent-amount", "4");

serializer.setOutputProperty(OutputKeys.INDENT,"yes");

th.setResult(out);

th.startDocument();

atts = new AttributesImpl();

th.startElement("","","HOWTOS",atts);

}

public void process (String s) throws SAXException {

String [] elements = s.split("
|");

atts.clear();

th.startElement("","","TOPIC",atts);

th.startElement("","","TITLE",atts);

th.characters(elements[0].toCharArray(),0,elements[0].length());

th.endElement("","","TITLE");

th.startElement("","","URL",atts);

th.characters(elements[1].toCharArray(),0,elements[1].length());

th.endElement("","","URL");

th.endElement("","","TOPIC");

}

public void closeXML() throws SAXException {

th.endElement("","","HOWTOS");

th.endDocument(); }

}

If some error come, please try to debug it.

Hope this will help you. and also use that function wherevr you need it.

Regards

Aashish Sinha

PS : reward points ifhelpful

vijay_b4
Active Contributor
0 Kudos

Hi JGD,

The append method.

The StringBuffer class must be fully instantiated.

StringBuffer sb = new StringBuffer(u201Cmy jolly stringu201D);

sb = sb.append(u2018su2019);

The character u2018su2019 will be appended to the end of the string and the instance sb will now

contain the string u201Cmy jolly stringsu201D.

Bear in mind that you can append floats, doubles, integers and other data types. Have a

quick peep at the append methods in the APIs.

Of course java gives you the ability to insert a data type at a specified position instead of at the end.

Check this pdf for more info:

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/d86aaf90-0201-0010-9eb2-f3da5f44...

In this PDF,press ctl+f and search for append.Then it shows codes related to append key word.

Reward points if this helps

Regards

Pragathi.

aashish_sinha
Active Contributor
0 Kudos

Hi,

You can use the UDF something like this.

String New = "\n";

String res = a Newb;

return res;

Here you can take A as a string as of your online of ur xml and append the new line charcter there like this. i was using this UDF for new line between two fields. So just try to customize and you will get the result.

Regards

Aashish Sinha

PS : reward points if helpful