How To Implement Java Mapping: Document Information
How To Implement Java Mapping: Document Information
Document Information
XI Area:
Release:
Keywords:
Design
XI 2.0
Java Mapping
Problem Description
You want to use Java classes for mapping.
Procedure
Target XML
<Person>
<Gender>1</Gender>
<Surname>Miller</Surname>
</Person>
<Passenger>
<Title>Mr.</Title>
<Name>Miller</Name>
</Passenger>
com.sap.aii.mapping.api.StreamTransformation;
java.io.*;
java.util.Map;
javax.xml.parsers.*;
org.xml.sax.*;
org.xml.sax.helpers.*;
/**
* An easy example in which one XML file is scanned for entries in specified
tags.
*
* Input: Person - Gender
*
- Surname
*
* Output: Passenger - Title
*
- Name
*/
public class SampleWithSaxParser
extends DefaultHandler
implements StreamTransformation
{
private Map map;
private OutputStream out;
/* need some constants to know, which tag we have now */
private int tag = 0;
private final int GENDER_TAG = 1;
private final int SURNAME_TAG = 2;
private final int PERSON_TAG = 3;
private final int OTHER_TAG = 0;
/**
* method setParamters is required, but we do not anything with it
*/
public void setParameter (Map param) {
map = param;
}
/**
* method execute is called by the XI mapping program
*/
public void execute (InputStream in, OutputStream out)
throws com.sap.aii.mapping.api.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();
}
}
//===========================================================
// SAX DocumentHandler methods
//===========================================================
/**
* startDocument is called at the beginning, so we write the xml header
*/
public void startDocument ()
throws SAXException {
write("<?xml version='1.0' encoding='UTF-8'?>");
}
/**
* endDocument is called at the end, so we clear the buffer
*/
public void endDocument ()
throws SAXException {
try {
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
/**
* startElement is called when a new tag appears. We store the information,
which tag we have now.
* Each beginning tag in the input creates a different tag in the output
*/
public void startElement (String namespaceURI, String lName, String qName,
Attributes attrs)
throws SAXException {
String eName = lName; // element name
if ("".equals(eName)) {
eName = qName; // namespaceAware = false
}
if (eName.equals("Gender")) {
tag = GENDER_TAG;
write("<Title>");
} else if (eName.equals("Surname")) {
tag = SURNAME_TAG;
write("<Name>");
} else if (eName.equals("Person")) {
tag = PERSON_TAG;
write("<Passenger>");
} else {
tag = OTHER_TAG;
}
}
/**
com.sap.aii.mapping.api.*;
java.io.*;
java.util.Map;
javax.xml.parsers.*;
javax.xml.transform.*;
javax.xml.transform.dom.DOMSource;
javax.xml.transform.stream.StreamResult;
org.w3c.dom.*;
org.xml.sax.*;
/**
* An easy example in which one XML file is scanned for entries in specified
tags.
*
* Input: Person - Gender
*
- Surname
*
* Output: Passenger - Title
*
- Name
*/
public class SampleWithDomParser
implements StreamTransformation
{
private Map map;
private Document document;
private String title = "";
private String name = "";
/**
* method setParamters is required, but we do not anything with it
*/
public void setParameter (Map param) {
map = param;
}
/**
* method execute is called by the XI mapping program
*/
public void execute (InputStream in, OutputStream out)
throws com.sap.aii.mapping.api.StreamTransformationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// create DOM structure from input XML
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(in);
// look for the tag 'Gender'
NodeList list = document.getElementsByTagName("Gender");
} catch (Throwable t) {
throw new StreamTransformationException("error", t);
}
}
}
Further Information
A tutorial for the methods of SAX and DOM you find here:
http://java.sun.com/webservices/docs/1.1/tutorial/doc/