4 Replies Latest reply: Jul 11, 2012 9:45 AM by 928518 RSS

    xml encoding problem.

      Hi All,
      I am trying to embedd an xml response string as CData inside an XML tree. The main XML tree is not in my control to create or change. I just have a class object where i am just setting the content as String, where the string is my xml.

      The problem is when the mail xml tree appears, the content of my xml ( RawProfile ) are not properly encoded.
      <ns5:PotsdamCache xmlns:ns2="...">
                     <ns5:Content>&lt;![CDATA[ & l t ; RawProfile & g t ; & l t ; ROWSET type = &quot;AllLinkedParents&quot;& g t ; & l t ; /ROWSET&gt;&lt;ROWSET type = &quot;AllParents&quot;&gt;&lt;ROWSET type = &quot;organization&quot;&gt;&lt;ROW type = &quot;Raw&quot;&gt;PotsdamInstanceReference ..]&lt;/ROW&gt;&lt;ROW type = &quot;Name&quot;&gt;&lt;/ROW&gt;&lt;ROW tot;&gt;1006565004&lt;/ROW&gt;&lt;ROW type = &quot;ot;&gt;6&lt;/ROW&gt;&lt;ROW type = &quot;ParentId&quot;&gt;0&lt;/ROW&gt;&lt;/ROWSET&gt;&lt;ROWSET...&lt;/RawProfile&gt;]]&gt;</ns5:Content></ns5:PotsdamCache>
      You might not see the encode problem above, since this editor handles it, but i hope you got my point. I ma getting these & l t ; & g t ; signs.

      I am simply doing the following
      where my buildRawProfile method returns a string of my xml ..."<![CDATA[<RawProfile>"....."</RawProfile>]]>".

      I also tried another aproach:
        Document doc = parseRawProfile(buildRawProfile(userInstance), false);
           // Add a CDATA section to the root element
           Element element = doc.getDocumentElement();
           CDATASection cdata = doc.createCDATASection(buildRawProfile(userInstance));
      // Parses an XML file and returns a DOM document.
        // If validating is true, the contents is validated against the DTD
        // specified in the file.
        public Document parseRawProfile(String rawProfile, boolean validating) {
            try {
                // Create a builder factory
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                // Create the builder and parse the file
                Document doc = factory.newDocumentBuilder().parse(rawProfile);
                return doc;
            } catch (SAXException e) {
                // A parsing error occurred; the xml input is not valid
                 log.debug(" A parsing error occurred; the xml input is not valid ");
            } catch (ParserConfigurationException e) {
                 log.debug(" A parsing configuration error occurred; the xml input is not valid ");
            } catch (IOException e) {
                 log.debug(" A parsing IOException error occurred; the xml input is not valid ");
            return null;
      But then got this error:      
                <faultstring>javax.ejb.EJBTransactionRolledbackException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory</faultstring>
      The last approach, i didn't like so may be if someone could give a simple solution to this encoding problem.


      Edited by: 925515 on 11.07.2012 02:13
        • 1. Re: xml encoding problem.
          Is the problem because you get the encoded elements with spaces, or just that you get the encoded elements (and you added spaces so the forum doesn't swallow it)?

          Are you really sure that there is an actual problem? Did you try running the XML through an XML parser as it is now, for example by putting it in a file and trying to open it with internet explorer?
          • 2. Re: xml encoding problem.
            i put the spaces here myself so that it appears unencoded here on the forum. If i put the xml as it is then the editor shows it correctly.

            Whereas when the xml appears on my xml nova spy editor as Soap response, the part inside <Content> is unencoded that is with gt and lt signs in it. The main encapsulating xml elements "PotsdamCache --> Content" appear fine.
            • 3. Re: xml encoding problem.

              there is no problem?
              • 4. Re: xml encoding problem.
                there is a problem since it does not appear properly when received as a response on the xml nova spy editor. All other part of the xml appears fine except the part i mentioned earlier ..i.e. inside "Content".