Skip to Content
0
Former Member
Apr 19, 2011 at 10:21 AM

XML not well formed issue in SAX parsing

168 Views

Hi Team,

I have a source structure like:

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

<ns0:MT_User xmlns:ns0="urn:bp:xi:hr:edm:test:100">

<Users>

<ID/>

<UserName/>

<Street/>

<City/>

<State/>

<Country/>

</Users>

</ns0:MT_User>

and I want to have my target structure as below:

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

<ns0:MT_Customer xmlns:ns0="urn:bp:xi:hr:edm:test:100">

<Customers>

<CustomerID/>

<Name/>

<City/>

<State/>

<Country/>

</ns0:MT_Customer>

The code for this that I have written is as given below:

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import java.util.jar.Attributes;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

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

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

public class NewAPISAX extends DefaultHandler implements StreamTransformation{

private OutputStream out;

//private OutputStream out;

public void execute(InputStream in, OutputStream out)

throws StreamTransformationException {

DefaultHandler handler = this;

SAXParserFactory factory = SAXParserFactory.newInstance();

try {

SAXParser saxParser = factory.newSAXParser();

this.out = out;

saxParser.parse(in, handler);

}

catch (Throwable t)

{

t.printStackTrace();

}

}

public void setParameter(Map param) {

}

public void startDocument() throws SAXException

{

String fresult="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

fresult = fresult.concat("<ns0:MT_Customer xmlns:ns0=\"urn:bp:xi:hr:edm:test:100\">");

}

public void endDocument() throws SAXException

{

String fresult = "" ;

fresult =fresult .concat("</ns0:MT_Customer>");

}

public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException

{

String lName = localName;

if(lName.equals(""))

lName=qName;

String fresult="";

if(lName.equals("Users"))

fresult = fresult.concat("<Customers>");

if(lName.equals("ID"))

fresult= fresult.concat("<CustomerID>");

if(lName.equals("UserName"))

fresult=fresult.concat("<Name>");

if(lName.equals("City"))

fresult=fresult.concat("<City>");

if(lName.equals("State"))

fresult=fresult.concat("<State>");

if(lName.equals("Country"))

fresult=fresult.concat("<Country>");

}

public void endElement(String namespaceURI, String localName,String qName) throws SAXException

{

String lName = localName;

if(lName.equals(""))

lName=qName;

Object fresult = "";

String string = (String) fresult;

if(lName.equals("Users"))

fresult=string.concat("</Customers>");

if(lName.equals("ID"))

fresult=string.concat("</CustomerID>");

if(lName.equals("UserName"))

fresult=string.concat("</Name>");

if(lName.equals("City"))

fresult=string.concat("</City>");

if(lName.equals("State"))

fresult=string.concat("</State>");

if(lName.equals("Country"))

fresult=string.concat("</Country>");

}

public void characters(char[] ch, int start, int length) throws SAXException

{

String fresult = "";

String val= new String(ch,start, length);

fresult = fresult.concat(val);

}

public String getResult()

{

String fresult = null;

return fresult;

}

}

But everytime I am running this in XI I am getting the issue as"XML not well formed",I didn't get why I am facing this issue.

Please can you tell me what is the issue,where I have to correct the code.

Thanks and Regards

Atanu Mazumdar