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&#39;re going to be doing a lot of http processing I recommend using Apache&#39;s httpclient instead of dealing with Java&#39;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&nbsp; 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.&nbsp; 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>
&nbsp;&nbsp;&nbsp; protected List&lt;Element&gt; rawElementList = new ArrayList&lt;Element&gt;();<br>&nbsp;&nbsp;&nbsp; protected List&lt;Element&gt; scrubbedElementList = new ArrayList&lt;Element&gt;();<br>&nbsp;&nbsp;&nbsp; protected List&lt;Element&gt; newElementList = new ArrayList&lt;Element&gt;();<br>

&nbsp;&nbsp;&nbsp; protected SAXBuilder saxBuilder = new SAXBuilder();<br>&nbsp;&nbsp;&nbsp; protected DOMBuilder domBuilder = new DOMBuilder();<br>&nbsp;&nbsp;&nbsp; protected Document rawDoc = new Document();<br>&nbsp;&nbsp;&nbsp; protected Element element;<br><br>&nbsp;&nbsp;&nbsp; @SuppressWarnings(&quot;unchecked&quot;)<br>

&nbsp;&nbsp;&nbsp; public void parseUrl() throws JDOMException, IOException {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(&quot;preparing to parse the elements...&quot;);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; URL url = new URL(<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &quot;<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>&quot;);<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; saxBuilder.setIgnoringElementContentWhitespace(true);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; rawDoc = saxBuilder.build(url);<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; element = rawDoc.getRootElement();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; rawElementList.addAll(element.getChildren());<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 3; i++) {// remove 2 useless elements<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; rawElementList.remove(0);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; rawElementList.size(); i++) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String s = rawElementList.get(i).getText();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (s != null &amp;&amp; s != &quot;&quot;) {<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; scrubbedElementList.add(rawElementList.get(i));<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (Element e : scrubbedElementList) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(e.getText());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // for (Element e : rawElementList) {<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // System.out.println(e.getText());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // }<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public void getAllChangedElements() throws URISyntaxException {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(&quot;preparing to change the data...&quot;);<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Element entry = new Element(&quot;entry&quot;);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; entry.addNamespaceDeclaration(Namespace.getNamespace(&quot;&quot;));<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; newElementList.add(entry);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // rawElementList.add(entry);<br><br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; scrubbedElementList.size(); i++) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String s = scrubbedElementList.get(i).getText();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String name = scrubbedElementList.get(i).getName();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (s != null &amp;&amp; s != &quot;&quot;) {<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (name == &quot;AccountName&quot;) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s = &quot;****55555******&quot;;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Element e = new Element(name);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.setText(s);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; newElementList.add(e);<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // return newData;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // for (Element e : newElementList) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // System.out.println(e.getText());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // }<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public void postData() throws IOException, JDOMException,<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ParserConfigurationException, SAXException {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // building a document from DOM<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DocumentBuilder docBuilder = dbfac.newDocumentBuilder();<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; org.w3c.dom.Document docPrepareToPost = docBuilder.newDocument();<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // create my root element<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; org.w3c.dom.Element root = docPrepareToPost.createElement(&quot;entry&quot;);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; docPrepareToPost.appendChild(root);<br>

<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // add elements to my dom doc<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; newElementList.size(); i++) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; org.w3c.dom.Element child = docPrepareToPost<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .createElement(newElementList.get(i).getName());<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; child.setTextContent(newElementList.get(i).getText());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; root.appendChild(child);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // parsing my doc from the file allows me a successful post<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // FileWriter fs = new FileWriter(&quot;/home/rick/deleteme/crap2&quot;);<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // //posting is successful from this parsed file<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // xmlOutputter.output(docForPosting, fs);<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; docPrepareToPost = factory.newDocumentBuilder().parse(<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; new File(&quot;/home/rick/deleteme/crap2&quot;));<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // setting up the stream<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; URL url = new URL(<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &quot;<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>&quot;);<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; URLConnection connection = url.openConnection();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; connection.setDoOutput(true);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; java.net.HttpURLConnection http = (HttpURLConnection) connection;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; http.setRequestMethod(&quot;PUT&quot;);<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HttpURLConnection.setFollowRedirects(true);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; http.setRequestProperty(&quot;Content-type&quot;,<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &quot;application/atom+xml;type=entry&quot;);<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // creating a JDom doc from the DOM doc to facilitate streaming to the<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // server<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; org.jdom.Document docForPosting = domBuilder.build(docPrepareToPost);<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; XMLOutputter xmlOutputter = new XMLOutputter();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //this fails to update the data (Response Code: 200, Response Message: OK)<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; xmlOutputter.output(docForPosting, http.getOutputStream()); <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //output seems ok<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; xmlOutputter.output(docForPosting, System.out); <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //the printout seems ok<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(&quot;doctype = &quot; + docForPosting.getDocType()); //=null<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; FileWriter file = new FileWriter(&quot;/home/rick/deleteme/crap2&quot;); <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //this successfully updates the data (Response Code: 200, Response Message: OK)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; xmlOutputter.output(docForPosting, file);<br>

<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(&quot;Response Code: &quot; + http.getResponseCode());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(&quot;Response Message: &quot; + http.getResponseMessage());<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MyJDomParser parser = new MyJDomParser();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Authenticator.setDefault(new MyAuthenticator());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; parser.parseUrl();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; parser.getAllChangedElements();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; parser.postData();<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch (JDOMException e) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.printStackTrace();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch (IOException e) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.printStackTrace();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch (URISyntaxException e) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.printStackTrace();<br>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch (ParserConfigurationException e) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.printStackTrace();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch (SAXException e) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.printStackTrace();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<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>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>

&lt;entry&gt;&lt;entry /&gt;&lt;published&gt;0001-01-01T00:00:00+00:00&lt;/published&gt;&lt;title&gt;**------**&lt;/title&gt;&lt;updated&gt;2009-01-16T12:38:38+00:00&lt;/updated&gt;&lt;AccountName&gt;****55555******&lt;/AccountName&gt;&lt;AccountNameUpper&gt;**SHORTENED**&lt;/AccountNameUpper&gt;&lt;CreateDate&gt;2008-10-21T09:01:41+00:00&lt;/CreateDate&gt;&lt;CreateUser&gt;U6UJ9A000009&lt;/CreateUser&gt;&lt;LastHistoryBy&gt;ADMIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/LastHistoryBy&gt;&lt;LastHistoryDate&gt;2008-10-31T13:25:58+00:00&lt;/LastHistoryDate&gt;&lt;ModifyDate&gt;2009-01-16T12:38:38+00:00&lt;/ModifyDate&gt;&lt;ModifyUser&gt;ADMIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/ModifyUser&gt;&lt;Status&gt;Active&lt;/Status&gt;&lt;Type&gt;Corporate&lt;/Type&gt;&lt;Prefix&gt;Mr.&lt;/Prefix&gt;&lt;Name&gt;Bob&lt;/Name&gt;&lt;Surname&gt;Smith&lt;/Surname&gt;&lt;Gender&gt;Male&lt;/Gender&gt;&lt;Idtype&gt;I.D.&lt;/Idtype&gt;&lt;Idpassportno&gt;7904055766543&lt;/Idpassportno&gt;&lt;Citizen&gt;RSA&lt;/Citizen&gt;&lt;/entry&gt; <br>

</blockquote></div>
<br></blockquote></div><br>