I am using Java to read an RSS feed from a URL, parse the DOM tree using javax.xml.parsers.DocumentBuilder.parse(InputStream), make some changes to it, then serialize and output the result using org.w3c.dom.ls.LSSerializer.write(Node,LSOutput).
The feed I’m reading is http://www.collaborationblueprint.com.au/blog/rss.xml.
The feed is well-formed XML, but the serialization result is not.
On every attempt so far, CData sections have been broken by the removal of one pair of square brackets.
E.g. If the source contains the following element:
<description><![CDATA[<p>some text</p>]]></description>
The serialized result looks like this, and is not well-formed:
<description><![CDATA<p>some text</p>]></description>
My code is below. It’s contained in a Lotus Domino agent.
How do I fix this problem?
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
org.w3c.dom.Document newDoc;
DocumentBuilderFactory builderFactory;
DocumentBuilder builder;
Element docElem,tmpElem;
Node tmpNode;
Session session=getSession();
AgentContext agentContext=session.getAgentContext();
// Put URL arguments into a HashMap.
Document doc=agentContext.getDocumentContext();
String[] query=doc.getItemValueString("Query_String").split("&");
HashMap<String,String> queryMap=new HashMap<String,String>(query.length);
for (int i=0; i<query.length; i++) {
int j=query[i].indexOf('=');
if (j<0) queryMap.put(query[i],"");
else queryMap.put(query[i].substring(0,j),URLDecoder.decode(query[i].substring(j+1),"UTF-8"));
}
// Get the "src" URL argument - this is the URL we're reading the feed from.
String urlStr=queryMap.get("src");
if (urlStr==null || urlStr.length()==0) {
System.err.println("Error: source URL not specified.");
return;
}
URL url;
try {
url=new URL(urlStr);
} catch (Exception e) {
System.err.println("Error: invalid source URL.");
return;
}
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
InputStream is=conn.getInputStream();
builderFactory=DocumentBuilderFactory.newInstance();
builder=builderFactory.newDocumentBuilder();
// Create a DocumentBuilder and parse the XML.
builder=builderFactory.newDocumentBuilder();
try {
newDoc=builder.parse(is);
is.close();
conn.disconnect();
} catch (Exception e) {
is.close();
conn.disconnect();
System.err.println("XML parse exception: "+e.toString());
return;
}
docElem=newDoc.getDocumentElement();
docElem.setAttribute("xmlns:ibmwcm","http://purl.org/net/ibmfeedsvc/wcm/1.0");
PrintWriter pw=getAgentOutput();
pw.println("Content-type: text/xml");
DOMImplementationRegistry registry=DOMImplementationRegistry
.newInstance();
DOMImplementationLS impl=(DOMImplementationLS)registry
.getDOMImplementation("LS");
LSOutput lso=impl.createLSOutput();
lso.setCharacterStream(pw);
LSSerializer writer=impl.createLSSerializer();
writer.write(newDoc,lso);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I’ve determined the problem is nothing to do with the serializer.
Even if I do something as simple as:
pw.print("<description><![CDATA[<p>Some text</p>]]></description>");the inner pair of square brackets gets stripped.
However, if I encode less-than signs in the CData, the problem goes away. E.g.:
pw.print("<description><![CDATA[<p>Some text</p>]]></description>");I have not yet determined whether the cause is the PrintWriter class or Lotus Domino, but regardless I should be able to modify the XML between parsing and serialization to fix it.