cancel
Showing results for 
Search instead for 
Did you mean: 

How I can do UDF, to remove a specific string in a different position?

0 Kudos

Dear,

how I can do this with a UDF?

I need to extract, content of (Codigo y Mensaje, but will always have different positions in the xml,

Example1: Codigo: 0 y Mensaje: http://192.168.12.90/Facturacion/PDFServlet?docId=KC4uHbk9PbESoy8lcV(MaS)JrA&#60

Example1

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><!-- Request Message Mapping --> <ns:OnlineGenerationDteResponse xmlns:ns='http://webservices.online.webapp.paperless.cl' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><ns:return>&#60;?xml version="1.0" encoding="ISO-8859-1"?&#62;

&#60;Respuesta&#62;

                &#60;Codigo&#62;0&#60;/Codigo&#62;

                &#60;Mensaje&#62;http://192.168.12.90/Facturacion/PDFServlet?docId=KC4uHbk9PbESoy8lcV(MaS)JrA</Mensaje>

&#60;/Respuesta&#62;

  </ns:return></ns:OnlineGenerationDteResponse>

Example2: Codigo: -30 y Mensaje: Error, el Emisor informado en el XML, no corresponde a empresa Emisora.

Example2

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><!-- Request Message Mapping --> <ns:OnlineGenerationDteResponse xmlns:ns='http://webservices.online.webapp.paperless.cl' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><ns:return>&#60;?xml version="1.0" encoding="ISO-8859-1"?&#62;

&#60;Respuesta&#62;

                &#60;Codigo&#62;-30&#60;/Codigo&#62;

                &#60;Mensaje&#62;Error, el Emisor informado en el XML, no corresponde a empresa Emisora.&#60;/Mensaje&#62;

&#60;/Respuesta&#62;

  </ns:return></ns:OnlineGenerationDteResponse>

Regards,

Accepted Solutions (0)

Answers (3)

Answers (3)

iaki_vila
Active Contributor
0 Kudos

Hi Pablo,

You can use a simple XSL to disable the inside escaping characters:

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

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:Q-ENV="http://www.quadrem.com/soap/" xmlns:ns="http://webservices.online.webapp.paperless.cl" version="1.0">

    <xsl:output method="text" version="1.0" encoding="ISO-8859-1" indent="no" omit-xml-declaration="yes"/>

    <xsl:template match="/">

        <xsl:value-of select="ns:OnlineGenerationDteResponse/ns:return" disable-output-escaping="yes"/>

    </xsl:template>

</xsl:stylesheet>

If you test this example with your second XML:

Now, with the XML correct you can use a Message Mapping to extract the "codigo" tag and/or Mensaje" tag:

Operation Mapping:

Message Mapping (You can use the XML output for the source message):

Hope this helps.

(Spanish is my native language too but most people here doesn’t understand it, please write only in english)

Regards.

Message was edited by: Iñaki Vila

Former Member
0 Kudos

Hi. Pablo.

You can try with JavaMapping.

public void execute(InputStream in, OutputStream out) throws ParserConfigurationException  {

        try {

            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

            DocumentBuilder builderel=factory.newDocumentBuilder();

            Document docIn=builderel.parse(in);

            Document docOut=builderel.newDocument();

           

            Element MainRoot=docOut.createElement("ns0:Test");//

            MainRoot.setAttribute("xmlns:ns0","urn:sap.com.pe:test"); //

           

            String charSet[]={"&#60;","&#62;"};

            String replaceSet[]={"<",">"};

           

            NodeList nodeLst = docIn.getElementsByTagName("ns:return");

            Node n = null;

            Element child1 = null,child2 =null;

           

            for(int nRow=0; nRow < nodeLst.getLength();nRow++ ){

               

                if(nodeLst.item(nRow).hasChildNodes()){

                    n=nodeLst.item(nRow).getFirstChild();

                    if(n.getNodeType()== Node.TEXT_NODE ){

                        String sValor=n.getNodeValue();                       

                        for(int j=0;j<charSet.length;++j) {

                            sValor=sValor.replaceAll(charSet[j], replaceSet[j]);                           

                        }

                        ///System.out.println(sValor);                       

                        sValor = sValor.replaceAll("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>","");                        

                        org.xml.sax.InputSource inStream = new org.xml.sax.InputSource();                                             

                         inStream.setCharacterStream(new java.io.StringReader(sValor));                       

                        DocumentBuilder b=factory.newDocumentBuilder();

                        Document cdataSection=b.parse(inStream);

                        child2=(Element) docOut.importNode(cdataSection.getDocumentElement(),true);

                        MainRoot.appendChild(child2);

                    }

                }

            }

            docOut.appendChild(MainRoot);

            String sResult = convertDocument_String(docOut);

            out.write(sResult.toString().getBytes("UTF-8"));           

            out.close();

           

        } catch (Exception ex){

             this.getTrace().addDebugMessage(ex.toString());

        }

    }

Regards

Lucho.

0 Kudos

Luis gracias por tu ayuda, de momento el requerimiento no se ha concretado, y no se ha implementado ninguna solución, pero agradezco tu ayuda, Saludos.

drosadeoliveira
Explorer
0 Kudos

Hi Pablo,

You can try to parse the content of TAG "return" (using DOM or SAX) and get the value of "Codigo" and "Mensaje" using an UDF.

Take a look at the following thread... there is an UDF code example.

http://scn.sap.com/message/2229578#2229578

Regards,

Daniel Rosa.