View RSS Feed

sbutt

xml encoding problem.

Rate this Entry
by , 07-11-2012 at 10:52 AM (2682 Views)
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.

Java Code:
<ns5:PotsdamCache xmlns:ns2="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:ns3="http://www.opentravel.org/OTA/2003/05" xmlns:ns4="http://www.opentravel.org/OTA/2002/11" xmlns:ns5="http://www.PotsdamCache.com">
			<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 [id=546565, type=organization, code=6565, name=KID for BE Requests, parentId=0, linkedParentIds=null]&lt;/ROW&gt;&lt;ROW type = &quot;Name&quot;&gt;KID for BE Requests&lt;/ROW&gt;&lt;ROW type = &quot;Code&quot;&gt;1006565004&lt;/ROW&gt;&lt;ROW type = &quot;Id&quot;&gt;6565&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

Java Code:
potsdamCacheRS.setContent(buildRawProfile(userInstance));
where my buildRawProfile method returns a string of my xml ..."<![CDATA[<RawProfile>"....."</RawProfile>]]>".


I also tried another aproach:

Java Code:
  Document doc = parseRawProfile(buildRawProfile(userInstance), false);

	// Add a CDATA section to the root element
	Element element = doc.getDocumentElement();
	CDATASection cdata = doc.createCDATASection(buildRawProfile(userInstance));
	element.appendChild(cdata);

...

// 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();
          factory.setValidating(validating);

          // 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:
Java Code:
		<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.

Thanks.

Submit "xml encoding problem." to Facebook Submit "xml encoding problem." to Digg Submit "xml encoding problem." to del.icio.us Submit "xml encoding problem." to StumbleUpon Submit "xml encoding problem." to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments