[jdom-interest] xmlOutputter question

Rick Avlonitis rick.softly at gmail.com
Mon Jan 19 10:27:48 PST 2009


Thank you for the link (although it's in php).  That root *entry* 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 :(

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.

On Mon, Jan 19, 2009 at 7:58 PM, Joe Bowbeer <joe.bowbeer at gmail.com> wrote:

> What you're putting doesn't like like a valid atom post.
>
> The root should be a *feed* instead of an *entry*, right?
>
> And that empty *entry* in the first element doesn't look right.
>
> See sample feed here:
>
> http://www.ibm.com/developerworks/library/x-tipatom2/
>
> Yes, I'd recommend using ROME, which itself uses JDOM.
>
> Joe
>
> On Mon, Jan 19, 2009 at 9:45 AM, Rick Avlonitis wrote:
>
>> 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?
>>
>>
>> On Mon, Jan 19, 2009 at 7:11 PM, Joe Bowbeer wrote:
>>
>>> On Mon, Jan 19, 2009 at 9:08 AM, Joe Bowbeer wrote:
>>>
>>>> What's the diff between the two documents created in the two different
>>>> ways?
>>>>
>>>> Also, what's your root element?
>>>>
>>>> The snippet below looks like nothing more than an empty "entry" element:
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <entry><entry /> ...ignored...
>>>
>>>
>>>
>>>
>>> Update: Nope, I got that wrong.  The document itself is an "entry" and
>>> the first element is an empty "entry".
>>>
>>> Joe
>>>
>>>
>>>
>>>>
>>>>
>>>> On Mon, Jan 19, 2009 at 1:15 AM, Rick Avlonitis wrote:
>>>>
>>>>> Thank you for the inputs.  However, I have to disagree.  My problem can
>>>>> be distilled like so:
>>>>>
>>>>> If I create a document from an ArrayList<Element> and try to post, the
>>>>> server doesn't update.
>>>>> If I create a document by parsing the file made from a document that
>>>>> was made from ArrayList<Element> and post, the server updates.
>>>>>
>>>>> 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>.
>>>>>
>>>>> So the above makes me wonder if ArrayList<Element> is a suitable list
>>>>> to work with.  What other container could I use?
>>>>>
>>>>> Regards,
>>>>> Rick
>>>>>
>>>>> On Sat, Jan 17, 2009 at 1:49 AM, Joe Bowbeer wrote:
>>>>>
>>>>>>  I agree with Jason that you should close the output stream (which
>>>>>> will also flush) before getting the response code.
>>>>>>
>>>>>> 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.
>>>>>>
>>>>>>
>>>>>> On Fri, Jan 16, 2009 at 5:26 AM, Rick Avlonitis wrote:
>>>>>>
>>>>>>> When I call xmlOutputter.output(docForPosting,
>>>>>>> http.getOutputStream()), this fails to update the data  but I get: (Response
>>>>>>> Code: 200, Response Message: OK).
>>>>>>> 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).
>>>>>>>
>>>>>>> Why can I not put data successfully on the server with
>>>>>>> xmlOutputter.output(docForPosting, http.getOutputStream())?
>>>>>>>
>>>>>>> Your help will be appreciated.  My code is below:
>>>>>>>
>>>>>>>
>>>>>>> package myJDom;
>>>>>>>>
>>>>>>>> import org.jdom.*;
>>>>>>>> import org.jdom.input.DOMBuilder;
>>>>>>>> import org.jdom.input.SAXBuilder;
>>>>>>>> import org.jdom.output.XMLOutputter;
>>>>>>>> import org.xml.sax.SAXException;
>>>>>>>>
>>>>>>>> import java.io.File;
>>>>>>>> import java.io.FileWriter;
>>>>>>>> import java.io.IOException;
>>>>>>>> import java.net.Authenticator;
>>>>>>>> import java.net.HttpURLConnection;
>>>>>>>> import java.net.URISyntaxException;
>>>>>>>> import java.net.URL;
>>>>>>>> import java.net.URLConnection;
>>>>>>>> import java.util.ArrayList;
>>>>>>>> import java.util.List;
>>>>>>>>
>>>>>>>> import javax.xml.parsers.DocumentBuilder;
>>>>>>>> import javax.xml.parsers.DocumentBuilderFactory;
>>>>>>>> import javax.xml.parsers.ParserConfigurationException;
>>>>>>>>
>>>>>>>> public class MyJDomParser {
>>>>>>>>     protected List<Element> rawElementList = new
>>>>>>>> ArrayList<Element>();
>>>>>>>>     protected List<Element> scrubbedElementList = new
>>>>>>>> ArrayList<Element>();
>>>>>>>>     protected List<Element> newElementList = new
>>>>>>>> ArrayList<Element>();
>>>>>>>>     protected SAXBuilder saxBuilder = new SAXBuilder();
>>>>>>>>     protected DOMBuilder domBuilder = new DOMBuilder();
>>>>>>>>     protected Document rawDoc = new Document();
>>>>>>>>     protected Element element;
>>>>>>>>
>>>>>>>>     @SuppressWarnings("unchecked")
>>>>>>>>     public void parseUrl() throws JDOMException, IOException {
>>>>>>>>         System.out.println("preparing to parse the elements...");
>>>>>>>>         URL url = new URL(
>>>>>>>>                 "
>>>>>>>> http://192.168.1.199:3334/sdata/slx/dynamic/accounts(A6UJ9A000036)<http://192.168.1.199:3334/sdata/slx/dynamic/accounts%28A6UJ9A000036%29>
>>>>>>>> ");
>>>>>>>>         saxBuilder.setIgnoringElementContentWhitespace(true);
>>>>>>>>         rawDoc = saxBuilder.build(url);
>>>>>>>>
>>>>>>>>         element = rawDoc.getRootElement();
>>>>>>>>         rawElementList.addAll(element.getChildren());
>>>>>>>>
>>>>>>>>         for (int i = 0; i < 3; i++) {// remove 2 useless elements
>>>>>>>>             rawElementList.remove(0);
>>>>>>>>         }
>>>>>>>>
>>>>>>>>         for (int i = 0; i < rawElementList.size(); i++) {
>>>>>>>>             String s = rawElementList.get(i).getText();
>>>>>>>>             if (s != null && s != "") {
>>>>>>>>                 scrubbedElementList.add(rawElementList.get(i));
>>>>>>>>             }
>>>>>>>>         }
>>>>>>>>
>>>>>>>>         for (Element e : scrubbedElementList) {
>>>>>>>>             System.out.println(e.getText());
>>>>>>>>         }
>>>>>>>>         // for (Element e : rawElementList) {
>>>>>>>>         // System.out.println(e.getText());
>>>>>>>>         // }
>>>>>>>>     }
>>>>>>>>
>>>>>>>>     public void getAllChangedElements() throws URISyntaxException {
>>>>>>>>         System.out.println("preparing to change the data...");
>>>>>>>>         Element entry = new Element("entry");
>>>>>>>>         entry.addNamespaceDeclaration(Namespace.getNamespace(""));
>>>>>>>>         newElementList.add(entry);
>>>>>>>>         // rawElementList.add(entry);
>>>>>>>>
>>>>>>>>         for (int i = 0; i < scrubbedElementList.size(); i++) {
>>>>>>>>             String s = scrubbedElementList.get(i).getText();
>>>>>>>>             String name = scrubbedElementList.get(i).getName();
>>>>>>>>             if (s != null && s != "") {
>>>>>>>>                 if (name == "AccountName") {
>>>>>>>>                     s = "****55555******";
>>>>>>>>                 }
>>>>>>>>                 Element e = new Element(name);
>>>>>>>>                 e.setText(s);
>>>>>>>>                 newElementList.add(e);
>>>>>>>>             }
>>>>>>>>         }
>>>>>>>>         // return newData;
>>>>>>>>         // for (Element e : newElementList) {
>>>>>>>>         // System.out.println(e.getText());
>>>>>>>>         // }
>>>>>>>>     }
>>>>>>>>
>>>>>>>>     public void postData() throws IOException, JDOMException,
>>>>>>>>             ParserConfigurationException, SAXException {
>>>>>>>>         // building a document from DOM
>>>>>>>>         DocumentBuilderFactory dbfac =
>>>>>>>> DocumentBuilderFactory.newInstance();
>>>>>>>>         DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
>>>>>>>>         org.w3c.dom.Document docPrepareToPost =
>>>>>>>> docBuilder.newDocument();
>>>>>>>>
>>>>>>>>         // create my root element
>>>>>>>>         org.w3c.dom.Element root =
>>>>>>>> docPrepareToPost.createElement("entry");
>>>>>>>>         docPrepareToPost.appendChild(root);
>>>>>>>>
>>>>>>>>         // add elements to my dom doc
>>>>>>>>         for (int i = 0; i < newElementList.size(); i++) {
>>>>>>>>             org.w3c.dom.Element child = docPrepareToPost
>>>>>>>>                     .createElement(newElementList.get(i).getName());
>>>>>>>>             child.setTextContent(newElementList.get(i).getText());
>>>>>>>>             root.appendChild(child);
>>>>>>>>         }
>>>>>>>>
>>>>>>>>         // parsing my doc from the file allows me a successful post
>>>>>>>>         // FileWriter fs = new
>>>>>>>> FileWriter("/home/rick/deleteme/crap2");
>>>>>>>>         // //posting is successful from this parsed file
>>>>>>>>         // xmlOutputter.output(docForPosting, fs);
>>>>>>>>
>>>>>>>>         DocumentBuilderFactory factory =
>>>>>>>> DocumentBuilderFactory.newInstance();
>>>>>>>>         docPrepareToPost = factory.newDocumentBuilder().parse(
>>>>>>>>                 new File("/home/rick/deleteme/crap2"));
>>>>>>>>
>>>>>>>>         // setting up the stream
>>>>>>>>         URL url = new URL(
>>>>>>>>                 "
>>>>>>>> http://192.168.1.199:3334/sdata/slx/dynamic/accounts(A6UJ9A000036)<http://192.168.1.199:3334/sdata/slx/dynamic/accounts%28A6UJ9A000036%29>
>>>>>>>> ");
>>>>>>>>         URLConnection connection = url.openConnection();
>>>>>>>>         connection.setDoOutput(true);
>>>>>>>>         java.net.HttpURLConnection http = (HttpURLConnection)
>>>>>>>> connection;
>>>>>>>>         http.setRequestMethod("PUT");
>>>>>>>>         HttpURLConnection.setFollowRedirects(true);
>>>>>>>>         http.setRequestProperty("Content-type",
>>>>>>>>                 "application/atom+xml;type=entry");
>>>>>>>>
>>>>>>>>         // creating a JDom doc from the DOM doc to facilitate
>>>>>>>> streaming to the
>>>>>>>>         // server
>>>>>>>>         org.jdom.Document docForPosting =
>>>>>>>> domBuilder.build(docPrepareToPost);
>>>>>>>>
>>>>>>>>         XMLOutputter xmlOutputter = new XMLOutputter();
>>>>>>>>         //this fails to update the data (Response Code: 200,
>>>>>>>> Response Message: OK)
>>>>>>>>         xmlOutputter.output(docForPosting, http.getOutputStream());
>>>>>>>>         //output seems ok
>>>>>>>>         xmlOutputter.output(docForPosting, System.out);
>>>>>>>>         //the printout seems ok
>>>>>>>>         System.out.println("doctype = " +
>>>>>>>> docForPosting.getDocType()); //=null
>>>>>>>>         FileWriter file = new
>>>>>>>> FileWriter("/home/rick/deleteme/crap2");
>>>>>>>>         //this successfully updates the data (Response Code: 200,
>>>>>>>> Response Message: OK)
>>>>>>>>         xmlOutputter.output(docForPosting, file);
>>>>>>>>
>>>>>>>>         System.out.println("Response Code: " +
>>>>>>>> http.getResponseCode());
>>>>>>>>         System.out.println("Response Message: " +
>>>>>>>> http.getResponseMessage());
>>>>>>>>     }
>>>>>>>>
>>>>>>>>     public static void main(String[] args) {
>>>>>>>>         MyJDomParser parser = new MyJDomParser();
>>>>>>>>         Authenticator.setDefault(new MyAuthenticator());
>>>>>>>>         try {
>>>>>>>>             parser.parseUrl();
>>>>>>>>             parser.getAllChangedElements();
>>>>>>>>             parser.postData();
>>>>>>>>         } catch (JDOMException e) {
>>>>>>>>             e.printStackTrace();
>>>>>>>>         } catch (IOException e) {
>>>>>>>>             e.printStackTrace();
>>>>>>>>         } catch (URISyntaxException e) {
>>>>>>>>             e.printStackTrace();
>>>>>>>>         } catch (ParserConfigurationException e) {
>>>>>>>>             e.printStackTrace();
>>>>>>>>         } catch (SAXException e) {
>>>>>>>>             e.printStackTrace();
>>>>>>>>         }
>>>>>>>>     }
>>>>>>>> }
>>>>>>>>
>>>>>>>
>>>>>>> *Output (snipped):*
>>>>>>>
>>>>>>> preparing to change the data...
>>>>>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>>>>>> <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>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> To control your jdom-interest membership:
>>>>>>
>>>>>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>> _______________________________________________
>>> To control your jdom-interest membership:
>>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>>>
>>
>>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20090119/1e7808cb/attachment.htm


More information about the jdom-interest mailing list