package org.javarosa.services.transport;

import java.io.Reader;

import org.kxml2.io.KXmlParser;
import org.kxml2.kdom.Document;
import org.xmlpull.v1.XmlPullParser;

import de.enough.polish.io.StringReader;

public class CommUtil {

	public static Document getXMLResponse (String response) {
		return getXMLResponse(new StringReader(response));
	}

	public static Document getXMLResponse (Reader reader) {
    	Document doc = new Document();
		
    	try{
    		KXmlParser parser = new KXmlParser();
    		parser.setInput(reader);
    		parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
		                       
    		doc.parse(parser);
    	} catch (Exception e) {
    		System.err.println("couldn't process response payload from server!!");
    		doc = null;
    	}
			
		try {
			doc.getRootElement();
		} catch (RuntimeException re) {
			doc = null; //work around kxml bug where it should have failed to parse xml (doc == null)
						//but instead returned an empty doc that throws an exception when you try to
						//get its root element
		}
    	
    	return doc;
    }
	
}

@@ -213,16 +213,220 @@
 der;%0D%0A%0D%0A
+/**%0D%0A * utility class for extracting an xml document out of a string or bytestream, typically used for%0D%0A * processing xml payloads sent in response to server requests%0D%0A * %0D%0A * @author Drew Roos%0D%0A *%0D%0A */%0D%0A
 public c

add doc to commutil