Java Mapping (Part II)
public void startElement(String namespaceURI,
String sName, // simple name
String qName, // qualified name
Attributes attrs)
throws SAXException
{
echoText();
String eName = sName; // element name
if (“”.equals(eName))eName = qName; // not namespace-aware
if(eName.equals(“HEADER”))
iHeader = 1;
if(eName.equals(“TRANSACTION”)){
iTrans =1;
sTransValue = “”;
}
if((eName.startsWith(“E_”)==true) || eName.equals(“IDOC”)==true)
emitAttrs(“<“eName ” ” + attrs.getQName(0)+ “=”” + attrs.getValue(0) “”” “>”);
else
emit(“<“eName“>”);
}
This method will read the starting tag of the XML and sets the flag iHeader and iTrans whenever it reads the header and
transaction segments. I will explain the later part of this method later.
Add the following method endElement() to read the end tags of the XML document.
public void endElement(String namespaceURI,
String sName, // simple name
String qName // qualified name
)
throws SAXException
{
echoText();
String eName = sName; // element name
if (“”.equals(eName)) eName = qName; // not namespace-aware
if(eName.equals(“HEADER”))
iHeader = 0;
if(eName.equals(“TRANSACTION”))
iTrans =0;
emit(“</”eName“>”);
}
This method will read the ending tags and resets the iHeader and iTrans value again back to 0.
Add the following methods after endElements() method. (Same as that of SAX Parser)
public void characters(char buf[], int offset, int len) throws SAXException { String s = new String(buf, offset, len); if (textBuffer == null) { textBuffer = new StringBuffer(s); } else { textBuffer.append(s); } } private void echoText() throws SAXException { if (textBuffer == null) return; String s = “”+textBuffer; emit(s); textBuffer = null; } private void nl() throws SAXException { String lineEnd = System.getProperty(“line.separator”); try { Echo.out.write(lineEnd.getBytes()); for (int i=0; i < indentLevel; i++) Echo.out.write(indentString.getBytes()); }catch (IOException e) { throw new SAXException(“I/O error”, e); } }
Then comes the most important part. That is to print the document as per our needs.
private void emit(String s) throws SAXException { try { if(iHeader==1) sHeaderValue = sHeaderValue + s; if(iTrans ==1) sTransValue = sTransValue + s; if(s.equals(sRegDetStartElement)||s.equals(sAsstStartElement)||(s.equals(sMeterPntStartElement))||s.equals(sTransStartElement)|| s.equals(sRootEndElement)) { if(s.equals(sRegDetStartElement)){ iNoRegDet=iNoRegDet+1; if(iNoRegDet > 1)printOutPut(sRegDetEndElement); } if(s.equals(sAsstStartElement)){ iNoAsst=iNoAsst+1; if(iNoRegDet>=1)printOutPut(sRegDetEndElement); if(iNoAsst > 1)printOutPut(sAsstEndElement); iNoRegDet = 0; } if(s.equals(sMeterPntStartElement)){ iNoMtPt=iNoMtPt+1; if(iNoRegDet>=1)printOutPut(sRegDetEndElement); if(iNoAsst>=1)printOutPut(sAsstEndElement); if(iNoMtPt >
1)
{
printOutPut(sMeterPntEndElementsTransEndElementsHeaderEndElement);
printOutPut(sHeaderValue+sTransValue);
}
iNoAsst = 0;
iNoRegDet = 0;
}
if(s.equals(sTransStartElement))
{
iNoTrans=iNoTrans+1;
if(iNoRegDet>=1)printOutPut(sRegDetEndElement);
if(iNoAsst>=1)printOutPut(sAsstEndElement);
if(iNoMtPt>=1)printOutPut(sMeterPntEndElement);
if(iNoTrans > 1)
{
printOutPut((sTransEndElement+sHeaderEndElement));
printOutPut(sHeaderValue);
}
iNoMtPt = 0;
iNoAsst = 0;
iNoRegDet = 0;
}
if(s.equals(sRootEndElement))
{
if(iNoRegDet>=1)printOutPut(sRegDetEndElement);
if(iNoAsst>=1)printOutPut(sAsstEndElement);
printOutPut((sMeterPntEndElementsTransEndElementsHeaderEndElement));
iNoTrans = 0;
iNoMtPt = 0;
iNoAsst = 0;
iNoRegDet = 0;
}
}
if(s.equals(sRegDetEndElement)==false)
if(s.equals(sAsstEndElement)==false){
if(s.equals(sMeterPntEndElement)==false)
if(s.equals(sTransEndElement)==false)
if(s.equals(sHeaderEndElement)==false)
{
printOutPut(s);
}
}
Echo.out.flush();
} catch (IOException e) {
throw new SAXException(“I/O error”, e);
}
}
File to Multiple IDocs (XSLT Mapping)
Hence the scenario us some thing like File->XI->BPM->IDOC. While posting from BPM to IDOC, again the XI will call the same
mapping program in interface mapping. That’s why I have added the following code in startElement() method
if((eName.startsWith(“E_”)==true) || eName.equals(“IDOC”)==true)
emitAttrs(“<“eName ” ” + attrs.getQName(0)+ “=”” + attrs.getValue(0) “”” “>”);
else
emit(“<“eName“>”);
“);
nl();
}
public void endDocument() throws SAXException {
try {
nl();
Echo.out.flush();
} catch (IOException e) {
throw new SAXException(“I/O error”, e);
}
}
public void startElement(String namespaceURI,
String sName, // simple name
String qName, // qualified name
Attributes attrs)
throws SAXException
{
echoText();
String eName = sName; // element name
if (“”.equals(eName))eName = qName; // not namespace-aware
if(eName.equals(“HEADER”))
iHeader = 1;
if(eName.equals(“TRANSACTION”)){
iTrans =1;
sTransValue = “”;
}
if((eName.startsWith(“E_”)==true) || eName.equals(“IDOC”)==true)
emitAttrs(“<“eName ” ” + attrs.getQName(0)+ “=”” + attrs.getValue(0) “”” “>”);
else
emit(“<“eName“>”);
}
public void endElement(String namespaceURI, String sName, // simple name
String qName // qualified name
) throws SAXException {
echoText();
String eName = sName; // element name
if (“”.equals(eName))
eName = qName; // not namespace-aware
if (eName.equals(“HEADER”))
iHeader = 0;
if (eName.equals(“TRANSACTION”))
iTrans = 0;
emit(“
nice weblog structure:
comments, references, lots of code
and of course the content itself
I enjoy reading your latest blogs 🙂
Regards,
michal
Thank you for your comments. I always get inspired by your blogs. Your blogs are always a bench mark for the weblog starters like me. 🙂
Happy blogging,
Regards,
Prasad U
Craig
thanks Prasad...