cancel
Showing results for 
Search instead for 
Did you mean: 

XML document - request string

Former Member
0 Kudos

Hi Experts,

Could you please tell me, how to pass XML document as a request string to the webservice scenario. Inside the service, I have to do the schema validation for the request . Please guide me, how to proceed further?

Regards

Sara

Accepted Solutions (1)

Accepted Solutions (1)

sunil_singh13
Active Contributor
0 Kudos

Hi Sara,

By request string you mean under one node every thing should come as a string then you have to use java mapping and there you can do xml Validation also.

Thanks

Sunil Singh

Former Member
0 Kudos

Yes, Sunil. What you have mentioned is correct. Could you please tell me, how to pass the XML document as a input string?

Regards

Sara

Former Member
0 Kudos

Sara,

Depending on how your XML document is "received" (inputsource ? DOM ? SAX ?) you will need to serialize it to a String object ...

It is easily feasible in a JAVA mapping, ie, in the execute() method of your mapping, read the inputsource object and collect each read byte into a String var and then use it to build your target document ...

In the aii_utilxi_misc.jar file, there is a class dedicated to string conversion : StringConversion that provides a lot of useful static methods, and one may interest you :

public static String xml2String(byte xmlBytes[]) {...}

It may be worth giving a try ?

Otherwise, please give us additional detail so we can help,

Rgds

Chris

Edited by: Christophe PFERTZEL on May 13, 2008 10:37 AM

prateek
Active Contributor
0 Kudos

See this

/people/michal.krawczyk2/blog/2005/11/01/xi-xml-node-into-a-string-with-graphical-mapping

U may also use java mapping and pass the whole input string with parsing to anu target node

Regards,

Prateek

former_member556603
Active Contributor
0 Kudos

Hello Sara,

how to pass the XML document as a input string?

you can change the XML message's tags into something else and later on chage it back .

you can also include the XML message inside a CDATA tag .

Take a look at this weblog: Frm Michal Krawczyk

XI: XML node into a string with graphical mapping?

/people/michal.krawczyk2/blog/2005/11/01/xi-xml-node-into-a-string-with-graphical-mapping

Thanks,

satya

GabrielSagaya
Active Contributor
0 Kudos

Create the Data type that has only one data type that has type xsd:string

|||ly create message type corresponding to that

Instead of using graphical mapping use XSLT mapping that converts the whole XML doc into a single string out.

<?xml version='1.0' ?>

<xsl:stylesheet version="1.0" xmlns='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

<xsl:template match="/">

</xsl:template>

</xsl:stylesheet>

use XSL:value-of select="node" in your element..

Thus create an xsl file and append it to winzip file. and copy it to imported Archives.

/people/michal.krawczyk2/blog/2005/11/01/xi-xml-node-into-a-string-with-graphical-mapping

sunil_singh13
Active Contributor
0 Kudos

Hi SARA,

use following Code written in SAX parsing .this will fulfil your requirment.This handle Special character also.

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.xml.parsers.FactoryConfigurationError;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.helpers.DefaultHandler;

import org.xml.sax.SAXException;

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

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

public class SimpleForwardMappingString extends DefaultHandler implements StreamTransformation

{

private List m_arlCurrent = new ArrayList();

private List m_arlBOList = new ArrayList();

private List m_arlObjectList = new ArrayList();

public static int counter = 0;

String listName = "newList";

public StringBuffer m_totalElementsBuffer = new StringBuffer();

String startData = "<inCanonStream>";

String endData = "</inCanonStream>";

private Map param = null;

/** Constant Strings used in Forward Mapping */

public static void main(String args[])

{

try {

InputStream in = new FileInputStream(new File("C:
Documents and Settings
281152
Desktop
Cannonical12321.xml"));

OutputStream out = new FileOutputStream(new File("C:
Documents and Settings
281152
Desktop
Cannonical21.xmlOut.xml"));

SimpleForwardMappingString myMapping = new SimpleForwardMappingString();

}

catch (Exception e)

{

e.printStackTrace();

}

}

public void execute(InputStream arg0, OutputStream arg1)

throws StreamTransformationException

{

Writer out;

try

{

out = new OutputStreamWriter(arg1, "UTF16");

/** Building the SAX parser */

getXMLDoc(arg0, arg1);

arg1.write(m_totalElementsBuffer.toString().getBytes("UTF16"));

}

catch (IOException e)

{

System.out.println("Error : io" + e.getMessage());

}

catch (Exception e)

{

System.out.println("Error :" + e.getMessage());

}

}

/* (non-Javadoc)

  • @see com.sap.aii.mapping.api.StreamTransformation#setParameter(java.util.Map)

*/

public void setParameter(Map arg0)

{

}

public void getXMLDoc(InputStream inputStream, OutputStream outputStream)

{

/**

  • Defines a factory API that enables applications to configure and

  • obtain a SAX based parser to parse XML documents. Once an application

  • has obtained a reference to a SAXParserFactory it can use the

  • factory to configure and obtain parser instances.

*/

SAXParserFactory factory = SAXParserFactory.newInstance();

try

{

factory.setNamespaceAware(true);

factory.setValidating(true);

SAXParser saxParser = factory.newSAXParser();

/**

  • Parse the content of the given {@link java.io.InputStream}

  • instance as XML using the specified

  • {@link org.xml.sax.helpers.DefaultHandler}.

*

  • @param inputstream InputStream containing the content to be parsed.

  • @param cobject The SAX DefaultHandler to use.

  • @exception IOException If any IO errors occur.

  • @exception IllegalArgumentException If the given InputStream is null.

  • @exception SAXException If the underlying parser throws a

  • SAXException while parsing.

*/

saxParser.parse(inputStream, this);

}

catch (FactoryConfigurationError e)

{

System.out.println("Error");

}

catch (ParserConfigurationException e)

{

e.printStackTrace();

}

catch (SAXException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

}

/** ===========================================================

Methods Overriding in SAX Default Handler

===========================================================

*/

/**

  • Receive notification of the beginning of the document.

  • By overriding this method in a subclass to take specific actions

  • at the beginning of a document (such as allocating the root node

  • of a tree or creating an output file)

*/

public void startDocument()throws SAXException

{

if(m_totalElementsBuffer.length()>0)

{

m_totalElementsBuffer = new StringBuffer();

counter = 0;

}

m_totalElementsBuffer.append(m_prologue);

m_totalElementsBuffer.append(m_nameSpace);

m_totalElementsBuffer.append(startData); }

/**

  • Receive notification of the end of the document.

  • By overriding this method in a subclass to take specific

  • actions at the end of a document (such as finalising a tree

  • or closing an output file)

*/

public void endDocument()throws SAXException

{

m_totalElementsBuffer.append(endData);

m_totalElementsBuffer.append(m_rootElementEnd);

}

/**

  • Receive notification of the start of an element.

  • By overriding this method in a subclass to take specific

  • actions at the start of each element (such as allocating

  • a new tree node or writing output to a file)

*/

public void startElement(String namespaceURI, String name, String qName, Attributes attrs)

throws SAXException

{

/** If current List is not empty then append node to StringBuffer */

{

m_totalElementsBuffer.append("&lt;" + name + "&gt;");

}

}

/**

  • Receive notification of the end of an element.

  • By overriding this method in a subclass to take specific

  • actions at the end of each element (such as finalising

  • a tree node or writing output to a file)

*/

public void endElement(String uri, String name, String qName) throws SAXException

{

m_totalElementsBuffer.append("&lt;/" + name + "&gt;");

}

/**

  • Receive notification of character data inside an element.

  • By overriding this method to take specific actions for each

  • chunk of character data (such as adding the data to a node

  • or buffer, or printing it to a file)

*/

public void characters(char buf[], int offset, int len)

throws SAXException

{

String s = new String(buf, offset, len);

if (null != s && s.length() >0)

{

if(s.equalsIgnoreCase("&"))

{

//System.out.println("FOUND AND SYMBOL");

String s1 = s.replaceAll("&","_-#_");

//System.out.println("s= " +s1);

s = s1;

}

if(s.equalsIgnoreCase("<"))

{

//System.out.println("FOUND AND SYMBOL");

String s1 = s.replaceAll("<","_#-_");

//System.out.println("s= " +s1);

s = s1;

}

m_totalElementsBuffer.append(s);

}

}

}

Thanks

Sunil Singh

Answers (0)