cancel
Showing results for 
Search instead for 
Did you mean: 

How to delete <![CDATA[<?xml version="1.0" encoding="UTF-8"?>

Former Member
0 Kudos

Hi All,

I am wondering if anyone can help me please.

I have a requirement in which I have to remove <![CDATA[<?xml version="1.0" encoding="UTF-8"?> and </Invoice]]> from the XML.

I have tried JAVA Mapping but getting illegal index error 46

I am using the following in JAVA

bw.write(line.replaceAll("<!CDATA <?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""));

bw.write(line.replaceAll("</Invoice>]]>", "</Invoice>"));

Error in PI

java.util.regex.Pattern SyntaxException

Illegal character range near index 43

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

Regards,

Iqbal


Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hello,

I think u want to extract string inside cdata?

If yes then chk my reply in below link:

http://scn.sap.com/thread/3294546

Thanks

Amit Srivastava

Former Member
0 Kudos

Hi Amit,

Could you please tell me how to remove CDATA tag through xslt.

Here is my Source XML

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

<DocumentBundle xmlns="http://abc.com.com/api/1.0">


<Document type="invoice" id="f11cc168-8764-4fbb-a783-aa893f6b3e60"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>

<Invoice>

<cbc:EndpointID schemeID="GB:VAT">GB122222343</cbc:EndpointID>

<cac:PartyIdentification>

<cbc:ID schemeID="GB:CNO">11111111</cbc:ID>

</Invoice>]]>

I want the output to be like this

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

<DocumentBundle xmlns="http://abc.com.com/api/1.0">


<Document type="invoice" id="f11cc168-8764-4fbb-a783-aa893f6b3e60

<Invoice>

<cbc:EndpointID schemeID="GB:VAT">GB122222343</cbc:EndpointID>

<cac:PartyIdentification>

<cbc:ID schemeID="GB:CNO">11111111</cbc:ID>

</Invoice>

Regards,

Iqbal

Former Member
0 Kudos

Hi.

Try with this.

   Document docOut = null;

    private void execute(InputStream in, OutputStream out)  throws StreamTransformationException {

       

        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

        try {

            DocumentBuilder builderel=factory.newDocumentBuilder();

            Document docIn=builderel.parse(in);

            docOut=builderel.newDocument();

            String sResult = null;

           

            Node node;

            NodeList nListRec = docIn.getElementsByTagName("DocumentBundle");

            node = nListRec.item(0);

           

            Node clon = docOut.importNode(node,true);

            docOut.appendChild(clon);

           

            sResult = convertDocument_String(docOut);

            sResult = sResult.replace("<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>","");

            sResult =  sResult.replace("]]>","");

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

            out.close();

           

           

        } catch (Exception ex) {

            ex.printStackTrace();

            System.out.println("Error ---" + ex.getMessage().toString() );

        }

    }

public static String convertDocument_String(Document Doc){

        String sResult ="";

        StringWriter writer = new StringWriter();

        try {

            DOMSource domSource = new DOMSource(Doc);

            StreamResult result = new StreamResult(writer);

            TransformerFactory tf = TransformerFactory.newInstance();

            Transformer transformer = tf.newTransformer();

            transformer.transform(domSource, result);

        } catch (Exception ex) {

            ex.printStackTrace();

        }

        sResult = writer.toString();

        return sResult;

    }

Former Member
0 Kudos

This just isn't working for me. What I have is XML like below. But when I build the java map, I replace "DocumentBundle" with "Data", run this through the OM test it seems to crash on line 08: Document docIn=builderel.parse (in) with Premature end of file.  Any suggestions? FYI I am not a java developer but I know many other development languages. Thanks for any help.

<enab:ExportDataResponse xmlns:enab="enablon">

  <Data><![CDATA[<?xml version="1.0" encoding="UTF-16" standalone="yes" ?>

  <Document>

  <Record>

  <EmployeeID>ID000006150265</EmployeeID>

  <ApplicationUser>&lt;None&gt;</ApplicationUser>

  <LastName>Shi</LastName>

  <FirstName>Nansun</FirstName>

  </Record>

  </Document>]]>

  </Data>

</enab:ExportDataResponse>

Former Member
0 Kudos

Hi Iqbal,

I guess we can use XSLT mapping to fix this issue.

Can you please send the souorce XML and the resultant XML you are expecting.

Thanks,

Ashish

Former Member
0 Kudos

Hi.

Try with this. Use only replace instead replaceAll.

Regards

Lucho

Former Member
0 Kudos

Hi Luis,

Replace will only work for the case of CHAR and it won't work for String.

Regards,

Iqbal

Former Member
0 Kudos

Hi.

it works.

     line = "<!CDATA <?xml version=\"1.0\" encoding=\"UTF-8\"?>";

           

            System.out.println("Line 01 " + line);

            System.out.println("Line 02 " + line.replace("<!CDATA <?xml version=\"1.0\" encoding=\"UTF-8\"?>",""));       

Former Member
0 Kudos

strange it's not working for me.

Also there are spaces in between

The Actual string is

bw.write(line.replaceAll("<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""));

I have defined line as a String and when I am trying to change it to char it's giving me an error.

I am using eclipse.

here is the code which I am using

public void execute(InputStream arg0, OutputStream arg1)

         throws StreamTransformationException {

      String line = null;

      BufferedReader br = new BufferedReader(new InputStreamReader(arg0));

      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(arg1));

      // Get reference to mapping trace

      trace = (AbstractTrace)map.get(StreamTransformationConstants.MAPPING_TRACE);

      trace.addInfo("Processing message");

      try {

         //Read all lines of input stream arg0

         while ((line=br.readLine()) != null) {                   

       

                   bw.write(line.replaceAll("<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""));

                         

         }

         bw.flush();

         br.close();

         bw.close();

      } catch (IOException ioe) {

         trace.addWarning("Could not process source message");

         throw new RuntimeException(ioe.getMessage());

      }

      trace.addInfo("Processing completed successfully");

   }

}


anupam_ghosh2
Active Contributor
0 Kudos

Hi Iqbal,

              Please post the complete source XML and intended target XML. Please mention the version of PI you are working with.

Regards

Anupam