I agree with Jason that you should close the output stream (which will also flush) before getting the response code.<br><br>Also, if you're going to be doing a lot of http processing I recommend using Apache's httpclient instead of dealing with Java's HttpConnection directly.<br>
<br><div class="gmail_quote">On Fri, Jan 16, 2009 at 5:26 AM, Rick Avlonitis <span dir="ltr"></span>wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
When I call xmlOutputter.output(docForPosting, http.getOutputStream()), this fails to update the data but I get: (Response Code: 200, Response Message: OK).<br>But when I write the docForPosting to a file and then create a new document from the parsed file, I can successfully update the data (Response Code: 200, Response Message: OK).<br>
<br>Why can I not put data successfully on the server with xmlOutputter.output(docForPosting, http.getOutputStream())? <br><br>Your help will be appreciated. My code is below:<br><br><br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
package myJDom;<br><br>import org.jdom.*;<br>import org.jdom.input.DOMBuilder;<br>import org.jdom.input.SAXBuilder;<br>import org.jdom.output.XMLOutputter;<br>import org.xml.sax.SAXException;<br><br>import java.io.File;<br>
import java.io.FileWriter;<br>import java.io.IOException;<br>import java.net.Authenticator;<br>import java.net.HttpURLConnection;<br>import java.net.URISyntaxException;<br>import java.net.URL;<br>import java.net.URLConnection;<br>
import java.util.ArrayList;<br>import java.util.List;<br><br>import javax.xml.parsers.DocumentBuilder;<br>import javax.xml.parsers.DocumentBuilderFactory;<br>import javax.xml.parsers.ParserConfigurationException;<br><br>
public class MyJDomParser {<br>
protected List<Element> rawElementList = new ArrayList<Element>();<br> protected List<Element> scrubbedElementList = new ArrayList<Element>();<br> protected List<Element> newElementList = new ArrayList<Element>();<br>
protected SAXBuilder saxBuilder = new SAXBuilder();<br> protected DOMBuilder domBuilder = new DOMBuilder();<br> protected Document rawDoc = new Document();<br> protected Element element;<br><br> @SuppressWarnings("unchecked")<br>
public void parseUrl() throws JDOMException, IOException {<br> System.out.println("preparing to parse the elements...");<br> URL url = new URL(<br> "<a href="http://192.168.1.199:3334/sdata/slx/dynamic/accounts%28A6UJ9A000036%29" target="_blank">http://192.168.1.199:3334/sdata/slx/dynamic/accounts(A6UJ9A000036)</a>");<br>
saxBuilder.setIgnoringElementContentWhitespace(true);<br> rawDoc = saxBuilder.build(url);<br><br> element = rawDoc.getRootElement();<br> rawElementList.addAll(element.getChildren());<br><br> for (int i = 0; i < 3; i++) {// remove 2 useless elements<br>
rawElementList.remove(0);<br> }<br><br> for (int i = 0; i < rawElementList.size(); i++) {<br> String s = rawElementList.get(i).getText();<br> if (s != null && s != "") {<br>
scrubbedElementList.add(rawElementList.get(i));<br> }<br> }<br><br> for (Element e : scrubbedElementList) {<br> System.out.println(e.getText());<br> }<br> // for (Element e : rawElementList) {<br>
// System.out.println(e.getText());<br> // }<br> }<br><br> public void getAllChangedElements() throws URISyntaxException {<br> System.out.println("preparing to change the data...");<br>
Element entry = new Element("entry");<br> entry.addNamespaceDeclaration(Namespace.getNamespace(""));<br> newElementList.add(entry);<br> // rawElementList.add(entry);<br><br>
for (int i = 0; i < scrubbedElementList.size(); i++) {<br> String s = scrubbedElementList.get(i).getText();<br> String name = scrubbedElementList.get(i).getName();<br> if (s != null && s != "") {<br>
if (name == "AccountName") {<br> s = "****55555******";<br> }<br> Element e = new Element(name);<br> e.setText(s);<br> newElementList.add(e);<br>
}<br> }<br> // return newData;<br> // for (Element e : newElementList) {<br> // System.out.println(e.getText());<br> // }<br> }<br><br> public void postData() throws IOException, JDOMException,<br>
ParserConfigurationException, SAXException {<br> // building a document from DOM<br> DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();<br> DocumentBuilder docBuilder = dbfac.newDocumentBuilder();<br>
org.w3c.dom.Document docPrepareToPost = docBuilder.newDocument();<br><br> // create my root element<br> org.w3c.dom.Element root = docPrepareToPost.createElement("entry");<br> docPrepareToPost.appendChild(root);<br>
<br> // add elements to my dom doc<br> for (int i = 0; i < newElementList.size(); i++) {<br> org.w3c.dom.Element child = docPrepareToPost<br> .createElement(newElementList.get(i).getName());<br>
child.setTextContent(newElementList.get(i).getText());<br> root.appendChild(child);<br> }<br><br> // parsing my doc from the file allows me a successful post<br> // FileWriter fs = new FileWriter("/home/rick/deleteme/crap2");<br>
// //posting is successful from this parsed file<br> // xmlOutputter.output(docForPosting, fs);<br><br> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();<br> docPrepareToPost = factory.newDocumentBuilder().parse(<br>
new File("/home/rick/deleteme/crap2"));<br><br> // setting up the stream<br> URL url = new URL(<br> "<a href="http://192.168.1.199:3334/sdata/slx/dynamic/accounts%28A6UJ9A000036%29" target="_blank">http://192.168.1.199:3334/sdata/slx/dynamic/accounts(A6UJ9A000036)</a>");<br>
URLConnection connection = url.openConnection();<br> connection.setDoOutput(true);<br> java.net.HttpURLConnection http = (HttpURLConnection) connection;<br> http.setRequestMethod("PUT");<br>
HttpURLConnection.setFollowRedirects(true);<br> http.setRequestProperty("Content-type",<br> "application/atom+xml;type=entry");<br><br> // creating a JDom doc from the DOM doc to facilitate streaming to the<br>
// server<br> org.jdom.Document docForPosting = domBuilder.build(docPrepareToPost);<br><br> XMLOutputter xmlOutputter = new XMLOutputter();<br> //this fails to update the data (Response Code: 200, Response Message: OK)<br>
xmlOutputter.output(docForPosting, http.getOutputStream()); <br> //output seems ok<br> xmlOutputter.output(docForPosting, System.out); <br> //the printout seems ok<br> System.out.println("doctype = " + docForPosting.getDocType()); //=null<br>
FileWriter file = new FileWriter("/home/rick/deleteme/crap2"); <br> //this successfully updates the data (Response Code: 200, Response Message: OK)<br> xmlOutputter.output(docForPosting, file);<br>
<br> System.out.println("Response Code: " + http.getResponseCode());<br> System.out.println("Response Message: " + http.getResponseMessage());<br> }<br><br> public static void main(String[] args) {<br>
MyJDomParser parser = new MyJDomParser();<br> Authenticator.setDefault(new MyAuthenticator());<br> try {<br> parser.parseUrl();<br> parser.getAllChangedElements();<br> parser.postData();<br>
} catch (JDOMException e) {<br> e.printStackTrace();<br> } catch (IOException e) {<br> e.printStackTrace();<br> } catch (URISyntaxException e) {<br> e.printStackTrace();<br>
} catch (ParserConfigurationException e) {<br> e.printStackTrace();<br> } catch (SAXException e) {<br> e.printStackTrace();<br> }<br> }<br>}<br></blockquote><div><br><b>Output (snipped):</b><br>
<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">preparing to change the data...<br><?xml version="1.0" encoding="UTF-8"?><br>
<entry><entry /><published>0001-01-01T00:00:00+00:00</published><title>**------**</title><updated>2009-01-16T12:38:38+00:00</updated><AccountName>****55555******</AccountName><AccountNameUpper>**SHORTENED**</AccountNameUpper><CreateDate>2008-10-21T09:01:41+00:00</CreateDate><CreateUser>U6UJ9A000009</CreateUser><LastHistoryBy>ADMIN </LastHistoryBy><LastHistoryDate>2008-10-31T13:25:58+00:00</LastHistoryDate><ModifyDate>2009-01-16T12:38:38+00:00</ModifyDate><ModifyUser>ADMIN </ModifyUser><Status>Active</Status><Type>Corporate</Type><Prefix>Mr.</Prefix><Name>Bob</Name><Surname>Smith</Surname><Gender>Male</Gender><Idtype>I.D.</Idtype><Idpassportno>7904055766543</Idpassportno><Citizen>RSA</Citizen></entry> <br>
</blockquote></div>
<br></blockquote></div><br>