Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: need some help with transmitting and receiving xml via java urls

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need some help with transmitting and receiving xml via java urls

    hello

    I'd like some suggestions on a development project I'm working on.
    I don't think it's very complicated but I'd like a good design and implementation.

    I need to transmit a defined xml notification message to an already implemented server
    the server encrypts files - but how it does it is not a concern to my client

    my client needs to get file listing from a database
    then create an xml notification message and http post it to server
    then i get an xml response back synchronously i believe
    when the server is done encrypting i get called asychronously with an xml message

    all xml is well defined by the server api

    i think i just need to transmit using a java URL class and a URLConnection class

    so should i create a java class that has some strings with all the xml elements and attributes and then append values to the strings once i know the values from the database ? use a hashtable for attributes and values ? or just create a class with like 20 or so strings and keep appending values to them and then just use a stream connected to the URLConnection ?

    the xml would be something like
    <notify file="file1" location="file///c:\files"
    <file types>
    file one="mpg"
    file two="jpeg"
    </file types>
    </notify>
    --- but there would be alot more xml elements and tags than that - not tons though

    so my class would just have a bunch of string with those element names and attributes ? and then i need to consider how they are nested ? seems like it could look messy.

    then just stream one big xml string via Urlconnection


    i think that may be the way to go for that portion
    suggestions ?

    but more importantly how do i set up the asynchronous listener ?
    i guess i need a web server with some object listening behind it ? a bean or just some java class ?

    also need to consider error handling
    and how the posting thread and receiving thread would be updating the database for info on those files processed

    thanks for the help


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: need some help with transmitting and receiving xml via java urls

    i think i just need to transmit using a java URL class and a URLConnection class
    Yes, it is possible to use URLConnection with POST to send your data along.


    so should i create a java class that has some strings with all the xml elements and attributes and then append values to the strings once i know the values from the database ? use a hashtable for attributes and values ? or just create a class with like 20 or so strings and keep appending values to them and then just use a stream connected to the URLConnection ?

    the xml would be something like
    <notify file="file1" location="file///c:\files"
    <file types>
    file one="mpg"
    file two="jpeg"
    </file types>
    </notify>
    --- but there would be alot more xml elements and tags than that - not tons though

    so my class would just have a bunch of string with those element names and attributes ? and then i need to consider how they are nested ? seems like it could look messy.
    My suggested solution is to use org.w3c.dom packages to build the XML document structure then convert it to String representation.

    This example build the XML document:

                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.newDocument();
     
                Element root = document.createElement("ULM");
                addSender(document, root, agent);
     
                Element messageElement = document.createElement("MESSAGE");
                messageElement.setAttribute("type", Constants.AGENT_ALIVE);
     
                Element paramElement = document.createElement("PARAM");
                paramElement.setAttribute("type", "useridletime");
                paramElement.setTextContent(String.valueOf(userIdleTime));
     
                messageElement.appendChild(paramElement);
     
                paramElement = document.createElement("PARAM");
                paramElement.setAttribute("type", "projectid");
                paramElement.setTextContent(String.valueOf(agent.getProjectId()));
     
                messageElement.appendChild(paramElement);
     
                if (!"".equals(localVariableProjectName)) {
                    paramElement = document.createElement("PARAM");
                    paramElement.setAttribute("type", "localvariableprojectname");
                    paramElement.setTextContent(localVariableProjectName);
                }
     
                messageElement.appendChild(paramElement);
     
                root.appendChild(messageElement);
                document.appendChild(root);
     
                return documentToString(document);

    The method documentToString() will output a String representation for the XML document:

            String xml = "";
            try {
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                StreamResult result = new StreamResult(new StringWriter());
                DOMSource source = new DOMSource(doc);
                transformer.transform(source, result);
                xml = result.getWriter().toString();
            } catch (TransformerConfigurationException ex) {
                logger.error(ex.getMessage(), ex);
                throw new XMLParsingException(ex);
            } catch (TransformerException ex) {
                logger.error(ex.getMessage(), ex);
                throw new XMLParsingException(ex);
            }
     
            return xml;

    You then would use this String as a parameter to be sent along with POST message via URLConnection.

    but more importantly how do i set up the asynchronous listener ?
    What do you mean by async listener? I do not get this point?


    and how the posting thread and receiving thread would be updating the database for info on those files processed
    Use JDBC and a RDBMS like MySQL.

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:28 PM.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need some help with transmitting and receiving xml via java urls

    thanks for the help
    I'm not sure our team leader is going to go for using the org.w3 dom library if we don't already have it in source control
    around here it's just pretty much standard java se - but agree with you that it is cleaning that

    when I'm receiving xml data back I could probably use a sax parser

    to answer you question on asynch - what I mean it this

    at anytime my client can receive an xml transmission over http from the server
    this would happen after it processes a file - which could take some time
    once it's done I get the notification
    so I need some object listening for the http post
    I'm guessing I'd need to config an web server ( such as apache ) ...or a servlet container
    would i use a servlet to do the listening ? or a message listener type bean ? - or hopefully just something simple like a java class I develop myself
    which can someone grab the incoming xml stream via htpp - parse it - and handle the result - such as "file is done and it's located here" or "there was an error and the error is ..."

    hope that clarifies a bit

    in other worlds - I'll have my front end java program looping and checking a database - when there is data there i create xml notify and http post to server and immediately get some type of "got your notification response back" xml message

    BUT there would be another object, thread or something listening for xml result message which could come in at any time from the server
    and since the server is sending xml via http - then i think my object would be a web service or some type ? correct ? or doesn't it have to be ?

    thanks for the help appreciate it

Similar Threads

  1. Testing whether a list of URLs are active or not in java?
    By aybeeryu in forum Java Networking
    Replies: 1
    Last Post: June 3rd, 2011, 09:17 AM
  2. Parsing urls
    By Riddhi Sharma in forum Java Theory & Questions
    Replies: 2
    Last Post: January 25th, 2011, 10:06 AM
  3. Transmitting from server to all clients.
    By newbie in forum Java Networking
    Replies: 0
    Last Post: December 19th, 2010, 05:07 PM
  4. URLs in browsers
    By subhvi in forum Java Networking
    Replies: 3
    Last Post: October 8th, 2010, 03:06 AM
  5. How to read URLs from a web page
    By abitha in forum Java Theory & Questions
    Replies: 1
    Last Post: September 17th, 2009, 12:36 PM