cancel
Showing results for 
Search instead for 
Did you mean: 

XML not well formed issue in SAX parsing

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

maciej_jarecki
Contributor
0 Kudos

Hi

Use test in NWDS.

Other solution: in your mapping write to output your target structure

"<?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> "

See result. If it is fine than, replace one by one line by your code.

BR

MAciej

Answers (1)

Answers (1)

subhro_de
Active Participant
0 Kudos

Hi Atanu,

This code seems to be incomplete - things to note -the code does not write the result of the mapping into the OutputStream . Also the variable fresult is reinitialized to null or "" in a number of methods (like characters,endDocument) you have used - you will loose the output that is being built. This should be a class level variable ( not declared in startDocument)

It seems you are trying to execute the code similiar to the following wiki :

http://wiki.sdn.sap.com/wiki/display/XI/JavamappingwithDOMandSAXparsersinnewmappingAPI%28PI+7.1%29

Going through the code above may help.

Former Member
0 Kudos

Hi Subhro,

Yes I have taken the example from the wiki but I tried to code it somewhat differently from what has been given in the wiki.

fresult should be initiallized to "".But the result that I am getting is when I am viewing the target structure in "Source Text view " mode then I am getting the result perfectly but when I am viewing it under "Tabulat tree view" it is showing as XML not well formed.

Thanks and Regards

Atanu Mazumdar

Former Member
0 Kudos

Hi Team,

The issue is resolved now.I modified the code in some places and it is now running fine in XI.

I am providing the change code below:

//import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

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

{

String messages = "";

public void setParameter (Map param)

{

// Map map = param;

}

public void execute (InputStream in, OutputStream out)

throws com.sap.aii.mapping.api.StreamTransformationException

{

try {

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser saxParser = factory.newSAXParser();

saxParser.parse(in, this);

out.write(messages.getBytes("UTF-8"));

}

catch (Exception e) {

throw new StreamTransformationException("Exception: ", e);

}

}

public void startDocument () throws SAXException{

messages = messages.concat("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:MT_Customer xmlns:ns0=\"urn:bp:xi:hr:edm:test:100\">");

}

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

throws SAXException {

String lName = localName;

if(lName.equals(""))

lName=qName;

if(lName.equals("Users"))

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

if(lName.equals("ID"))

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

if(lName.equals("UserName"))

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

if(lName.equals("City"))

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

if(lName.equals("State"))

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

if(lName.equals("Country"))

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

}

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

{

messages = messages.concat(new String(ch,start,length));

}

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

{

String lName = localName;

if(lName.equals(""))

lName=qName;

if(lName.equals("ID"))

messages = messages.concat("</CustomerID>");

if(lName.equals("UserName"))

messages = messages.concat("</Name>");

if(lName.equals("City"))

messages = messages.concat("</City>");

if(lName.equals("State"))

messages = messages.concat("</State>");

if(lName.equals("Country"))

messages = messages.concat("</Country>");

if(lName.equals("Users"))

messages = messages.concat("</Customers>");

}

public void endDocument() throws SAXException

{

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

}

}

Thanks and Regards

Atanu Mazumdar