Ok, the problem is solved. It seems that the app on the server is acting in a non-standard way. Even though it apparently supports atom and rss, I couldn't post to it successfully using jdom or RoME. My only hope lay in constructing the correct string and parsing it with jdom to post to the server. It worked after I reverse engineered the string. A tip for those going down this path is to strip the prologue from the xml string before parsing it with a SAXBuilder. The SAXBuilder will add the correct prologue before posting.<br>
<br><div class="gmail_quote">On Mon, Jan 19, 2009 at 8:27 PM, Rick Avlonitis <span dir="ltr"><<a href="mailto:rick.softly@gmail.com">rick.softly@gmail.com</a>></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;">
Thank you for the link (although it's in php). That root <b>entry</b> I got from some C# example. I'm trying to implement a form of ReSTful webservice, where I call a URI (discovery will be through ROME, I suspect) and parse the xml with jdom (at least it does that well ;). In order to change the text values of the elements, I will have to iterate through them (jdom). Then I put them back together in a document and PUT them back on the server. That was the theory :(<br>
<br>You are right in noticing the empty first element - that slipped past my eye. Although I did get this damnable code PUTting to the server with a document parsed from a file (the file being the result of a jdom document output). That there made me think the jdom api was fishy ;) I'll try ROME and report back.<div>
<div></div><div class="Wj3C7c"><br>
<br><div class="gmail_quote">On Mon, Jan 19, 2009 at 7:58 PM, Joe Bowbeer <span dir="ltr"><<a href="mailto:joe.bowbeer@gmail.com" target="_blank">joe.bowbeer@gmail.com</a>></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;">
What you're putting doesn't like like a valid atom post.<br><br>The root should be a <b>feed</b> instead of an <b>entry</b>, right?<br><br>And that empty <b>entry</b> in the first element doesn't look right.<br>
<br>See sample feed here:<br><br><a href="http://www.ibm.com/developerworks/library/x-tipatom2/" target="_blank">http://www.ibm.com/developerworks/library/x-tipatom2/</a><br><br>Yes, I'd recommend using ROME, which itself uses JDOM.<br>
<font color="#888888">
<br>Joe<br><br></font><div class="gmail_quote"><div>On Mon, Jan 19, 2009 at 9:45 AM, Rick Avlonitis <span dir="ltr"></span>wrote:<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
I may be wrong here, but it seems that JDom is perhaps not the choice to use when posting to an atom feed. I say this because I can output a document to a file with no problems. However, I can't out the document to an http stream. I've wrapped the stream in a buffer and called flush on it to no avail. I know that there isn't a problem with the stream, because when I create a new document from the file where I output to originally, it works. But I don't want to have to write to a file (or even a string). I want to find a solution that works. My solution is fine until I want to post the results back to the server :(. Perhaps ROME is the way to go?</div>
<div><div></div><div><div>
<div></div><div><br>
<br><div class="gmail_quote">On Mon, Jan 19, 2009 at 7:11 PM, Joe Bowbeer <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;">
<div class="gmail_quote"><div>On Mon, Jan 19, 2009 at 9:08 AM, Joe Bowbeer <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;">
What's the diff between the two documents created in the two different ways?<br><br>Also, what's your root element?<br><br>The snippet below looks like nothing more than an empty "entry" element:<div>
<br><br><?xml version="1.0" encoding="UTF-8"?><br></div>
<entry><entry /> ...ignored...</blockquote></div><div><br><br><br>Update: Nope, I got that wrong. The document itself is an "entry" and the first element is an empty "entry".<br><font color="#888888"><br>
Joe<br><br>
</font></div><div><div></div><div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br><br><div class="gmail_quote"><div>On Mon, Jan 19, 2009 at 1:15 AM, Rick Avlonitis <span dir="ltr"></span>wrote:<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div>
Thank you for the inputs. However, I have to disagree. My problem can be distilled like so:<br>
<br>
If I create a document from an ArrayList<Element> and try to post, the server doesn't update.<br>
If I create a document by parsing the file made from a document that
was made from ArrayList<Element> and post, the server updates.<br>
<br>
This makes me think that the http output stream is not at fault, since
it works for the case for the document built from a parsed file. (I
have also since wrapped the httpOutputStream in a bufferedStreamReader
and called flush(). Still nothing). It makes me think that there is a
setting which is read from the parsed file which is not read from the
ArrayList<Element>.<br>
<br>
So the above makes me wonder if ArrayList<Element> is a suitable list to work with. What other container could I use?<br>
<br>
Regards,<br><font color="#888888">
Rick<br><br></font></div><div><div></div><div><div class="gmail_quote"><div><div></div><div>On Sat, Jan 17, 2009 at 1:49 AM, Joe Bowbeer wrote:<br></div></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div>
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.<div>
<div></div><div><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>
</div></div><br></div></div><div>_______________________________________________<br>
To control your jdom-interest membership:<br>
<a href="http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com" target="_blank">http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com</a><br></div></blockquote></div><br>
</div></div></blockquote></div><br>
</blockquote></div></div></div><br>
<br>_______________________________________________<br>
To control your jdom-interest membership:<br>
<a href="http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com" target="_blank">http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com</a><br></blockquote></div><br>
</div></div></div></div></blockquote></div><br>
<br>_______________________________________________<br>
To control your jdom-interest membership:<br>
<a href="http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com" target="_blank">http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com</a><br></blockquote></div><br>
</div></div></blockquote></div><br>