cancel
Showing results for 
Search instead for 
Did you mean: 

XML-2-TXT conversion

Former Member
0 Kudos

All,

We have a small, annoying problem with xml-2-text conversion. During the conversion, the "&: in the XML file is not translated to "&" (ampersand).

We encounter this issue with the xml-2-txt convertor of the file adapter of the standalone J2SE adapter.

Anyone having a simple workaround for this small but annoying issue?

Kind regards, Guy Crets

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

I have tried your scenario personally in SP14 and I found no problem at all. When using transformations you don’t need to escape the & as "&amp;". I tried a constant mapping field with data "<&test>", the mapping resulted in "&lt;&amp;test&gt;" and finally the file adapter with content conversion translated correctly to "<&test>".

Best regards,

Javier Porras

Former Member
0 Kudos

All,

Indeed, the problem was not the file adapter but a preceding step in the integration server. The xml-2-txt conversion works ok in the adapter.

Otto Frost,

Thanks for your Java code.

Kind regards, Guy

Answers (2)

Answers (2)

Former Member
0 Kudos

hi,

Hope this Link Wil be useful

==>http://help.sap.com/saphelp_nw04s/helpdata/en/2c/181077dd7d6b4ea6a8029b20bf7e55/content.htm

Hope Using one of the enclosure series tag will help you...:)

Regards,

Sundararamaprasad

Message was edited by: Sundararamaprasad

Message was edited by: Sundararamaprasad

Former Member
0 Kudos

I have not used the J2Se adapter, however I wrote a java mapping program that converts XML2TXT, it is highly experimental. You may use it as the last mapping in the mapping program "chain". The XSLT is at the end.

I have tested this with

XML -> fileadapter -> SAP XI javamapping -> http adapter -> MS IIS.

Good luck /Otto

/*

  • Created on 2005-dec-05

*

  • To change the template for this generated file go to

  • Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

*/

/**

  • @author 01ottfro Otto Frost Capgemini

*

  • To change the template for this generated type comment go to

  • Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

*/

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import java.util.HashMap;

import com.sap.aii.mapping.api. AbstractTrace;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.StreamTransformationConstants;

//import java.io.*;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

//import javax.xml.parsers.FactoryConfigurationError;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import org.xml.sax.SAXParseException;

//import java.io.File;

import java.io.IOException;

import java.io.ByteArrayInputStream;

//import java.io.ByteArrayOutputStream;

import java.io.StringReader;

//import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import org.w3c.dom.Document;

//import org.w3c.dom.DOMException;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

//import javax.xml.parsers.*;

import org.w3c.dom.NodeList;

import org.w3c.dom.NamedNodeMap;

public class XML2TEXT implements StreamTransformation {

private Map param = null;

private AbstractTrace trace = null;

private StringBuffer sbTrace = new StringBuffer();

public void setParameter (Map param) {

this.param = param;

//if (param == zero) {

if ((param == null)) {

this.param = new HashMap();

}

}

public void execute(InputStream in, OutputStream out) {

try {

//javax.xml.parsers.DocumentBuilderFactory = "";

trace = (AbstractTrace)param.get(

StreamTransformationConstants.MAPPING_TRACE);

trace.addInfo("Start");

trace.addInfo(System.getProperty("javax.xml.parsers.DocumentBuilderFactory "));

InputStreamReader isr = new InputStreamReader(in,"UTF-8");

in.reset();

StringBuffer sb2 = new StringBuffer();

char c;

int i = isr.read();

while (i != -1) {

c = (char) i;

sb2.append(c);

i = isr.read();

}

String ss = new String(sb2);

trace.addInfo(ss);

in.reset();

// ...

String receiverName = (String)param.get(

StreamTransformationConstants.RECEIVER_NAME);

// ...

//static Document document;

Document document;

StringBuffer sb = new StringBuffer();

StringBuffer sbTrace = new StringBuffer();

sb.append("");

sb.append("<?xml version=\"1.0\"?>");

sb.append("<DOC>");

// sb.append("<greeter>An XSLT Programmer</greeter>");

// sb.append("<greeting>Hello, World!</greeting>");

sb.append("<TEXT>x_content_x</TEXT>");

sb.append("</DOC>");

//String s = sb.ToString();

StringReader sr = new StringReader(sb.toString());

/*

DocumentBuilderFactory dbf = new DocumentBuilderFactory();

dbf.setCoalescing(true);

dbf.setExpandEntityReferences(true);

dbf.setIgnoringComments(true);

dbf.setIgnoringElementContentWhitespace(false);

dbf.setNamespaceAware(true);

dbf.setValidating(true);

*/

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setCoalescing(true);

factory.setExpandEntityReferences(true);

factory.setIgnoringComments(true);

factory.setIgnoringElementContentWhitespace(false);

factory.setNamespaceAware(true);

factory.setValidating(false);

try {

DocumentBuilder builder = factory.newDocumentBuilder();

builder.setErrorHandler(

new org.xml.sax.ErrorHandler() { // ignore fatal errors (an exception is guaranteed)

public void fatalError(SAXParseException exception)

throws SAXException {

}

// treat validation errors as fatal

public void error(SAXParseException e)

throws SAXParseException {

throw e;

}

// dump warnings too

public void warning(SAXParseException err)

throws SAXParseException {

System.out.println("** Warning"

+ ", line " + err.getLineNumber()

+ ", uri " + err.getSystemId());

trace.addInfo("** Warning"

+ ", line " + err.getLineNumber()

+ ", uri " + err.getSystemId());

System.out.println(" " + err.getMessage());

trace.addInfo(" " + err.getMessage());

}

}

);

//document = builder.parse( new File(args[0]) );

ByteArrayInputStream bais = new

ByteArrayInputStream(sb.toString().getBytes("UTF-8"));

//System.out.println (bais.toString());

//document = builder.parse(bais);

document = builder.parse(in);

Element documentRootElement = document.getDocumentElement();

trace.addInfo("root" + documentRootElement.getNodeValue());

trace.addInfo("root" + Short.toString(documentRootElement.getNodeType()));

trace.addInfo("root" + documentRootElement.getNodeName());

// Element textElement = (Element) documentRootElement.getFirstChild();

NodeList nodeList = documentRootElement.getElementsByTagName("TEXT");

StringBuffer sbText = new StringBuffer();

for(i = 0; i<nodeList.getLength() ;i++) {

Element textElement = (Element) nodeList.item(i);

Node text = textElement.getFirstChild();

sbText.append(text.getNodeValue());

};

nodeList = documentRootElement.getElementsByTagName("CHARSETNAME");

StringBuffer sbCharset = new StringBuffer();

for(i = 0; i<nodeList.getLength() ;i++) {

Element textElement = (Element) nodeList.item(i);

Node text = textElement.getFirstChild();

sbCharset.append(text.getNodeValue());

};

// System.out.println(textElement.getNodeValue());

// System.out.println(textElement.getNodeType());

// System.out.println(textElement.getNodeName());

/*

trace.addInfo("te" + textElement.getNodeValue());

short nodeType = textElement.getNodeType();

trace.addInfo("te" + Short.toString(nodeType));

trace.addInfo("te" + textElement.getNodeName());

Node textNode = textElement.getFirstChild();

Node mt_textdocument = document.getFirstChild();

Node text = mt_textdocument.getFirstChild();

Node hashtext = text.getFirstChild();

textNode = hashtext;

String s = new String();

if (textNode != null) {

trace.addInfo(textNode.getNodeValue());

trace.addInfo(Short.toString(textNode.getNodeType()));

trace.addInfo(textNode.getNodeName());

s = new String(textNode.getNodeValue());

}

else {

s = "ERROR";

trace.addInfo(s);

}

*/

// traverse1(document);

// trace.addInfo(sbTrace.toString());

// OutputStream baos = new ByteArrayOutputStream();

String s = sbText.toString();

String charset = sbCharset.toString();

byte[] bytes = s.getBytes(charset);

// baos.write(bytes,0,bytes.length);

out.write(bytes,0,bytes.length);

} catch (SAXException sxe) {

// Error generated during parsing

Exception x = sxe;

if (sxe.getException() != null)

x = sxe.getException();

x.printStackTrace();

} catch (ParserConfigurationException pce) {

// Parser with specified options can't be built

pce.printStackTrace();

} catch (IOException ioe) {

// I/O error

ioe.printStackTrace();

}

}

catch (UnsupportedEncodingException uee) {

trace.addInfo("UnsupportedEncodingException");

}

catch (IOException ioe) {

// I/O error

// ioe.printStackTrace();

trace.addInfo("IOException");

}

finally {

trace.addInfo("finally");

}

}

public void traverse1(Node node) {

do {

System.out.println("Node: name=" + node.getNodeName() +

", value=" + node.getNodeValue() + ", type=" +

node.getNodeType());

sbTrace.append("Node: name=" + node.getNodeName() +

", value=" + node.getNodeValue() + ", type=" +

node.getNodeType() + "\n");

trace.addInfo("Node: name=" + node.getNodeName() +

", value=" + node.getNodeValue() + ", type=" +

node.getNodeType() + "\n");

if (node.getFirstChild() != null) {

System.out.println("Processing children:");

sbTrace.append("Processing children:"+"\n");

trace.addInfo("Processing children:"+"\n");

traverse1(node.getFirstChild());

}

} while ((node = node.getNextSibling()) != null);

}

public void traverse2(Node node) {

//Get the children of this Node

NodeList children = node.getChildNodes();

//go through all the children of the node

for (int i=0; i<children.getLength(); i++) {

//get the next child

Node child = children.item(i);

//get the type of the child

short childType = child.getNodeType();

if (childType == Node.ELEMENT_NODE) {

//if the child is an Element then print the start and end

//tags and recurse the content

String nodeName = child.getNodeName();

System.out.print("<" + nodeName + ">");

sbTrace.append("<" + nodeName + ">");

trace.addInfo("<" + nodeName + ">");

traverse2(child);

System.out.print("</" + nodeName + ">");

sbTrace.append("</" + nodeName + ">");

trace.addInfo("</" + nodeName + ">");

} else if (childType == Node.TEXT_NODE) {

//if the child is a Text node just print the text content

String data = child.getNodeValue();

System.out.print(data);

sbTrace.append(data);

trace.addInfo(data);

}

}

}

public void traverse3(Node node, int indent) {

for (int i = 0; i < indent; i++) {

System.out.print(" ");

sbTrace.append(" ");

trace.addInfo(" ");

}

int type = node.getNodeType();

switch (type) {

case Node.ATTRIBUTE_NODE:

System.out.println("ATTRIBUTE_NODE");

sbTrace.append("ATTRIBUTE_NODE"+"\n");

trace.addInfo("ATTRIBUTE_NODE"+"\n");

break;

case Node.CDATA_SECTION_NODE:

System.out.println("CDATA_SECTION_NODE");

sbTrace.append("CDATA_SECTION_NODE"+"\n");

trace.addInfo("CDATA_SECTION_NODE"+"\n");

break;

case Node.COMMENT_NODE:

System.out.println("COMMENT_NODE");

sbTrace.append("COMMENT_NODE"+"\n");

trace.addInfo("COMMENT_NODE"+"\n");

break;

case Node.DOCUMENT_FRAGMENT_NODE:

System.out.println("DOCUMENT_FRAGMENT_NODE");

sbTrace.append("DOCUMENT_FRAGMENT_NODE"+"\n");

trace.addInfo("DOCUMENT_FRAGMENT_NODE"+"\n");

break;

case Node.DOCUMENT_NODE:

System.out.println("DOCUMENT_NODE");

sbTrace.append("DOCUMENT_NODE"+"\n");

trace.addInfo("DOCUMENT_NODE"+"\n");

break;

case Node.DOCUMENT_TYPE_NODE:

System.out.println("DOCUMENT_TYPE_NODE");

sbTrace.append("DOCUMENT_TYPE_NODE"+"\n");

trace.addInfo("DOCUMENT_TYPE_NODE"+"\n");

break;

case Node.ELEMENT_NODE:

System.out.println("ELEMENT_NODE");

sbTrace.append("ELEMENT_NODE"+"\n");

trace.addInfo("ELEMENT_NODE"+"\n");

NamedNodeMap atts = node.getAttributes();

for (int i = 0; i < atts.getLength(); i++) {

Node att = atts.item(i);

traverse3(att, indent + 1);

}

break;

case Node.ENTITY_NODE:

System.out.println("ENTITY_NODE");

sbTrace.append("ENTITY_NODE"+"\n");

trace.addInfo("ENTITY_NODE"+"\n");

break;

case Node.ENTITY_REFERENCE_NODE:

System.out.println("ENTITY_REFERENCE_NODE");

sbTrace.append("ENTITY_REFERENCE_NODE"+"\n");

trace.addInfo("ENTITY_REFERENCE_NODE"+"\n");

break;

case Node.NOTATION_NODE:

System.out.println("NOTATION_NODE");

sbTrace.append("NOTATION_NODE"+"\n");

trace.addInfo("NOTATION_NODE"+"\n");

break;

case Node.PROCESSING_INSTRUCTION_NODE:

System.out.println("PROCESSING_INSTRUCTION_NODE");

sbTrace.append("PROCESSING_INSTRUCTION_NODE"+"\n");

trace.addInfo("PROCESSING_INSTRUCTION_NODE"+"\n");

break;

case Node.TEXT_NODE:

System.out.println("TEXT");

sbTrace.append("TEXT"+"\n");

trace.addInfo("TEXT"+"\n");

break;

default:

System.out.println("???");

sbTrace.append("???"+"\n");

trace.addInfo("???"+"\n");

break;

}

for (Node c = node.getFirstChild(); c != null; c =

c.getNextSibling()) {

traverse3(c, indent + 1);

}

}

}

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

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:trema-com:trema:trema" targetNamespace="urn:trema-com:trema:trema">

<xsd:element name="MT_TEXTDOCUMENT" type="DT_TEXTDOCUMENT" />

<xsd:complexType name="DT_TEXTDOCUMENT">

<xsd:annotation>

<xsd:appinfo source="http://sap.com/xi/TextID">

70d8cec1675c11da822400132107c3b7

</xsd:appinfo>

</xsd:annotation>

<xsd:sequence>

<xsd:element name="TEXT" type="xsd:string">

<xsd:annotation>

<xsd:appinfo source="http://sap.com/xi/TextID">

cc2dc340659311dab38eeb8f0ace6159

</xsd:appinfo>

</xsd:annotation>

</xsd:element>

<xsd:element name="CHARSETNAME" type="xsd:string">

<xsd:annotation>

<xsd:appinfo source="http://sap.com/xi/TextID">

bf0f424065c211da893ceb8f0ace6159

</xsd:appinfo>

</xsd:annotation>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

</xsd:schema>

example xml

<?xml version="1.0" encoding="utf-8"?>

<MT_TEXTDOCUMENT><TEXT>your text</TEXT><CHARSETNAME>UTF-8</CHARSETNAME></MT_TEXTDOCUMENT>