[jdom-interest] xmlOutputter question

Joe Bowbeer joe.bowbeer at gmail.com
Fri Jan 16 15:49:43 PST 2009


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>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20090116/594165ad/attachment.htm


More information about the jdom-interest mailing list